Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 11-29-2010, 11:31 PM   #1
Solid State Member
 
Join Date: Sep 2010
Posts: 17
Default Java Programming Help

How would I go about initializing the following variables
String[] playerNames;
int[] playerGuess;
int awayFromTheNumberArray;
so that they will accept the user input.
__________________

copelandtml is offline   Reply With Quote
Old 11-30-2010, 07:12 AM   #2
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Programming Help

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.
berry120 is offline   Reply With Quote
Old 11-30-2010, 09:27 AM   #3
Solid State Member
 
Join Date: Sep 2010
Posts: 17
Default Re: Java Programming Help

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)
copelandtml is offline   Reply With Quote
Old 11-30-2010, 11:53 AM   #4
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Programming Help

Quote:
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.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 11-30-2010, 09:56 PM   #5
Solid State Member
 
Join Date: Sep 2010
Posts: 17
Default Re: Java Programming Help

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!
copelandtml is offline   Reply With Quote
Old 12-01-2010, 06:35 AM   #6
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Programming Help

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.
berry120 is offline   Reply With Quote
Old 12-01-2010, 10:20 PM   #7
In Runtime
 
ohnoes's Avatar
 
Join Date: Nov 2007
Posts: 406
Send a message via AIM to ohnoes Send a message via MSN to ohnoes
Default Re: Java Programming Help

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
ohnoes is offline   Reply With Quote
Old 12-02-2010, 12:01 PM   #8
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Programming Help

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.
berry120 is offline   Reply With Quote
Old 12-02-2010, 09:00 PM   #9
Beta Member
 
Join Date: Dec 2010
Posts: 1
Default Re: Java Programming Help

I found a java tutorial and it has been updated http://download.oracle.com/javase/tutorial
jandrenievs is offline   Reply With Quote
Old 12-03-2010, 05:23 AM   #10
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Java Programming Help

Quote:
I found a java tutorial and it has been updated http://download.oracle.com/javase/tutorial
That's just the index of all the tutorials, the link I put in earlier points to the "getting started" one, which is what you'd want if you're starting out (the others will just get confusing!)
__________________

__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 06:10 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0