Author Topic: Java Issues (Need Para's Help)  (Read 3766 times)

liquidfired

  • Dragon Tamer
  • Raiding Silph Co.
  • *****
  • Posts: 823
  • Friendliness: -4
  • Unregistered Member (Error 114: Needs Activation)
    • Liquidfired's Channel of Awesomeness
Java Issues (Need Para's Help)
« on: August 03, 2012, 05:15:51 am »
I'm using this tutorial right now to learn Java http://www.java-made-easy.com

But I'm not sure how do Hash Maps work.

I made a HashMap Variable here:
Code: [Select]
HashMap<String, String> map = new HashMap<String, String>();but I'm not sure how to use the Key Set Method as shown in the tutorial
Code: [Select]
HashMap<String, String> map = new HashMap<String, String>();
String[] keys = map.keySet().toArray(new String[map.size()]);

Anyone elaborate?




Ad...

Paradox

  • Professor
  • Fighting Giovanni
  • *****
  • Posts: 966
  • Friendliness: 39
  • Registered User
Re: Java Issues (Need Para's Help)
« Reply #1 on: August 03, 2012, 04:44:16 pm »
That code works exactly as it should. What don't you understand?

liquidfired

  • Dragon Tamer
  • Raiding Silph Co.
  • *****
  • Posts: 823
  • Friendliness: -4
  • Unregistered Member (Error 114: Needs Activation)
    • Liquidfired's Channel of Awesomeness
Re: Java Issues (Need Para's Help)
« Reply #2 on: August 03, 2012, 06:17:48 pm »
I dont get what does that specific  code do.

Paradox

  • Professor
  • Fighting Giovanni
  • *****
  • Posts: 966
  • Friendliness: 39
  • Registered User
Re: Java Issues (Need Para's Help)
« Reply #3 on: August 03, 2012, 06:33:08 pm »
Want me to explain both?


First piece of code:
Creates a hasmap with the input and output holders as Strings. It's called map.


Second piece of code:
First line:
Does exactly what I said above.
Second line:
Creates an array of Strings called keys which gets all the keys from "map" (which is the hashmap you made) and saves them. The way it works is by using a command called keySet which retrieves all the keys in a set and turns it into an array with to toArray method.


Kapesh? Ask any question and I'll explain more in-depth.

liquidfired

  • Dragon Tamer
  • Raiding Silph Co.
  • *****
  • Posts: 823
  • Friendliness: -4
  • Unregistered Member (Error 114: Needs Activation)
    • Liquidfired's Channel of Awesomeness
Re: Java Issues (Need Para's Help)
« Reply #4 on: August 03, 2012, 06:43:11 pm »
Oh ok I get it now, thanks :)


I just started yesterday, took about 5 hours to make a Word Unscrambler Game with score.
You can check it out, but the coding is very novice and out of order.
Code: [Select]
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;




public class WordUnscrambler {


public static void main(String[] args) {
/*
*1. Make an Array for a WordBank
*2. Make an Array for each letter in the word bank
*3. Scramble the words with a random generator
*4.
*
*
*/
Scanner input = new Scanner(System.in);
int score = 0;
int points = 0;
String guess;
Random rand = new Random ();
String[] wordbank = new String[4];

wordbank[0] = ("Boat");
wordbank[1] = ("Walk");
wordbank[2] = ("Mouse");
wordbank[3] = ("Liquid");



int numberOfTries = 0;
String wordToGuess = wordbank[rand.nextInt(wordbank.length)];
boolean win = false;
boolean overallwin = false;




char[] boat = {'B','O','A','T'};
char[] walk = {'W','A','L','K'};
char[] mouse = {'M','O','U','S','E'};
char[] liquid = {'L','I','Q','U','I','D'};




ArrayList<Integer> number = new ArrayList<Integer>();


while (win == true){

for (int i = -1; i < wordToGuess.length() -1; i++){
number.add(i+1);
}


Collections.shuffle(number);
if (number.get(0) == 1 && number.get(1) == 2 && number.get(2) == 3 && number.get(3) == 4){
Collections.shuffle(number);
}
}



while (overallwin == false){



for (int i = -1; i < wordToGuess.length() -1; i++){
number.add(i+1);
}


Collections.shuffle(number);
if (number.get(0) == 1 && number.get(1) == 2 && number.get(2) == 3 && number.get(3) == 4){
Collections.shuffle(number);
}

System.out.println("Unscramble the Word!");
//numberOfTries++;


if (wordToGuess == "Boat"){
System.out.println(boat[number.get(0)] + "" + boat[number.get(1)] + "" + boat[number.get(2)] + "" + boat[number.get(3)]);
guess = input.nextLine();
if (guess.equals(wordToGuess)){
win = true;
points = 4;
}
}
if(wordToGuess == "Walk"){
System.out.println(walk[number.get(0)] + "" + walk[number.get(1)] + "" + walk[number.get(2)] + "" + walk[number.get(3)]);
guess = input.nextLine();
if (guess.equals(wordToGuess)){
win = true;
points = 4;
}
}

else if(wordToGuess == "Mouse"){
System.out.println(mouse[number.get(0)] + "" + mouse[number.get(1)] + "" + mouse[number.get(2)] + "" + mouse[number.get(3)] + "" + mouse[number.get(4)]);
guess = input.nextLine();
if (guess.equals(wordToGuess)){
win = true;
points = 5;
}
}
else if(wordToGuess == "Liquid"){
System.out.println(liquid[number.get(0)] + "" + liquid[number.get(1)] + "" + liquid[number.get(2)] + "" + liquid[number.get(3)] + "" + liquid[number.get(4)] + "" + liquid[number.get(5)]);
guess = input.nextLine();
if (guess.equals(wordToGuess)){
win = true;
points = 6;
}
}
if (win == true){
score = score + points;
System.out.println("You Win! " + points + " points has been added to your Score. Your score is now " + score);
for (int j = 0; j < points; j++){
number.remove(0);
}
wordToGuess = wordbank[rand.nextInt(wordbank.length)];
win = false;

}
else {
overallwin = true;
System.out.println("You Lose! Your final score was " + score );
}
}






}




}







Paradox

  • Professor
  • Fighting Giovanni
  • *****
  • Posts: 966
  • Friendliness: 39
  • Registered User
Re: Java Issues (Need Para's Help)
« Reply #5 on: August 03, 2012, 07:18:25 pm »
Nice job! If you'd like, I could show you how to make that code more effective, smaller, and more versatile. I can type the code so you can check it out.

liquidfired

  • Dragon Tamer
  • Raiding Silph Co.
  • *****
  • Posts: 823
  • Friendliness: -4
  • Unregistered Member (Error 114: Needs Activation)
    • Liquidfired's Channel of Awesomeness
Re: Java Issues (Need Para's Help)
« Reply #6 on: August 03, 2012, 07:30:04 pm »
sure! I was thinking of a way to make it so it doesnt to need to do
Code: [Select]
if (wordToGuess = "Boat")


but I'm still a noob, I dont know all the functions of Java yet.

Paradox

  • Professor
  • Fighting Giovanni
  • *****
  • Posts: 966
  • Friendliness: 39
  • Registered User
Re: Java Issues (Need Para's Help)
« Reply #7 on: August 03, 2012, 10:56:40 pm »
Yeah, there's a lot of ways. Do you know what methods are?

liquidfired

  • Dragon Tamer
  • Raiding Silph Co.
  • *****
  • Posts: 823
  • Friendliness: -4
  • Unregistered Member (Error 114: Needs Activation)
    • Liquidfired's Channel of Awesomeness
Re: Java Issues (Need Para's Help)
« Reply #8 on: August 04, 2012, 12:05:31 am »
Yeah, methods do seem easier to use. I was just too lazy to add perameters

Paradox

  • Professor
  • Fighting Giovanni
  • *****
  • Posts: 966
  • Friendliness: 39
  • Registered User
Re: Java Issues (Need Para's Help)
« Reply #9 on: August 04, 2012, 12:31:37 am »
Well, it would had been 10x easier that way lol.

liquidfired

  • Dragon Tamer
  • Raiding Silph Co.
  • *****
  • Posts: 823
  • Friendliness: -4
  • Unregistered Member (Error 114: Needs Activation)
    • Liquidfired's Channel of Awesomeness
Re: Java Issues (Need Para's Help)
« Reply #10 on: August 06, 2012, 07:42:38 am »
You know anything about Java Swing and Java Graphics. It's just adds a whole new dimension of confusion to Java.

Paradox

  • Professor
  • Fighting Giovanni
  • *****
  • Posts: 966
  • Friendliness: 39
  • Registered User
Re: Java Issues (Need Para's Help)
« Reply #11 on: August 06, 2012, 06:33:25 pm »
I know a lot about it but there's one thing you really should know about it: It's not worth learning. I suggest learning other languages that are more suited for GUI's over Java. Python is similar to Java in syntax so you can pick it up quickly and it's a lot easier to do graphics with.

liquidfired

  • Dragon Tamer
  • Raiding Silph Co.
  • *****
  • Posts: 823
  • Friendliness: -4
  • Unregistered Member (Error 114: Needs Activation)
    • Liquidfired's Channel of Awesomeness
Re: Java Issues (Need Para's Help)
« Reply #12 on: August 06, 2012, 07:41:54 pm »
I guess I'll brush up on my AS3 Too.