|
|
#1 |
|
Solid State Member
Join Date: Sep 2010
Posts: 17
|
How would I go about initializing the following variables
String[] playerNames; int[] playerGuess; int awayFromTheNumberArray; so that they will accept the user input. |
|
|
|
|
|
#2 |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
What do you mean by "accept the user input"? Do you want to read from the console when the user types the values in?
__________________
Save the whales, feed the hungry, free the mallocs. |
|
|
|
|
|
#3 |
|
Solid State Member
Join Date: Sep 2010
Posts: 17
|
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(JellyBeansHe lper.java:23) at JellyBeans.main(JellyBeans.java:56) |
|
|
|
|
|
#4 | |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
Quote:
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.
__________________
Save the whales, feed the hungry, free the mallocs. |
|
|
|
|
|
|
#5 |
|
Solid State Member
Join Date: Sep 2010
Posts: 17
|
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! |
|
|
|
|
|
#6 |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
A good one is Sun's (well, now Oracle's) trail:
http://download.oracle.com/javase/tu...ava/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!
__________________
Save the whales, feed the hungry, free the mallocs. |
|
|
|
|
|
#7 |
|
In Runtime
|
Also you should check out the Java API for reference sometimes.
http://download.oracle.com/javase/6/docs/api/
__________________
PC: Intel Core 2 Quad Q8200@ 2.6ghz 8 GB(4x2GB DDR2 PC 6400) BFG Tech GTX 280 Maxtor 250GB SATA HD Laptop:Intel Mobile Core Duo 2 1.8ghzGHZ 4GB DDR2 RAM SLI 8800M GTX |
|
|
|
|
|
#8 |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
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.
__________________
Save the whales, feed the hungry, free the mallocs. |
|
|
|
|
|
#9 |
|
Beta Member
Join Date: Dec 2010
Posts: 1
|
I found a java tutorial and it has been updated http://download.oracle.com/javase/tutorial
|
|
|
|
|
|
#10 | |
|
Site Team
Join Date: Jul 2009
Posts: 2,627
|
Quote:
__________________
Save the whales, feed the hungry, free the mallocs. |
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|