Java Programming Help

copelandtml

Baseband Member
Messages
45
How would I go about initializing the following variables
String[] playerNames;
int[] playerGuess;
int awayFromTheNumberArray;
so that they will accept the user input.
 
What do you mean by "accept the user input"? Do you want to read from the console when the user types the values in?
 
Yes, I want the variables to hold the data the user inputs.

Also can you tell me what is wrong with this line of code

awayFromTheNumberArray = JellyBeansHelper.findLowestDifference(playerGuess, jeallyBeans);

This is the error I am getting when I run my program

Jellybean Guess Game!
You can now guess, your guess must be between 1000 and 2000 exclusively
------------------------------------------------------------------------
How many players will be making guesses today? 3
Please enter name # 1: Craig
Craig, Enter your guess between 1000 and 2000: 1222
Please enter name # 2: Brian
Brian, Enter your guess between 1000 and 2000: 1333
Please enter name # 3: Steve
Steve, Enter your guess between 1000 and 2000: 1444
Exception in thread "main" java.lang.NullPointerException
at JellyBeansHelper.findLowestDifference(JellyBeansHelper.java:23)
at JellyBeans.main(JellyBeans.java:56)
 
Also can you tell me what is wrong with this line of code

awayFromTheNumberArray = JellyBeansHelper.findLowestDifference(playerGuess, jeallyBeans);
I'd need to see the source, but you get an NPE when you're dereferencing (usually calling a method) on something that's null. So:

String s = null;
s.inern();

...would throw an NPE.

In terms of getting a string from the console, I presume you know how to do that already since it's being done in the above output? To convert that string to an integer and therefore store it in an int variable, use Integer.parseInt(string).

As for the array, it's really the wrong structure to use here, have you used arraylists? They grow dynamically so can store any number of values the user inputs, without you specifying the number first. You'd use a while loop, check for some character being inputted that marked the end of the user input for that array (such as "." or an empty string) and then in the while loop, read from the console and add it to the arraylist.
 
Thanks for the help, I haven't used the arraylists yet still in the early stages of learning java and programming. Any useful sites you could refer me to? Or any tips that helped you learn.
Thanks again!
 
A good one is Sun's (well, now Oracle's) trail:

http://download.oracle.com/javase/tutorial/java/index.html

Bear in mind though that some of the topics presented there aren't always that easy to grasp, especially if you haven't done OO programming before. Take your time working through them and make sure you really understand each one (ask for help if you need it) otherwise it could really come back to bite you later!
 
The Java API is invaluable for checking what various methods and classes do when you know the language - just to clarify though don't try and learn the language by looking at it! Use it as a reference manual after you've learnt the syntax and you're confident in using it.
 
Back
Top Bottom