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


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 10-09-2008, 05:19 PM   #11
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: i need some java help

try a bit of troubleshooting.
After the:
Code:
System.out.println("Enter a name:  ");
phrase = kybd.nextString();
System.out.println("'" + phrase + "'"); //This will print out what is contained
                                                  //in your variable.
If it now errors on this line, chances are your variable is null and has not been set the way you think it is.

Update:
Looks like you may want to have something like:
Code:
kybd.nextLine();      // Read one line from the console.
http://mrbernal.freeshell.org/java/n...t-scanner.html
__________________

xtremezx2 is offline   Reply With Quote
Old 10-09-2008, 05:58 PM   #12
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: i need some java help

cant get it to work. how can such a simple thing confuse me lol
__________________

__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 10-09-2008, 06:24 PM   #13
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: i need some java help

What does it give you now, is it a compile error like before or does it run, if it runs did you try what I said to do above, did it print a value?

What does your code look like now.
xtremezx2 is offline   Reply With Quote
Old 10-09-2008, 07:31 PM   #14
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: i need some java help

it works fine. it just always says invalid. the codes the same as the last one i posted, but with the "nextline" fix you gave me.
__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 10-09-2008, 07:51 PM   #15
Fully Optimized
 
UK31337's Avatar
 
Join Date: Feb 2005
Posts: 2,776
Default Re: i need some java help

Have you tried:

Code:
if (testString.Equals("Cathy"))
{
// blah
}
as opposed to the == you had before? The .Equals("") method is the String class way of doing things and I think this is what you're needing in order to validate the input. I don't have time to test this or to properly test your existing code but try that instead. If you still can't get it, I've probably got time over the weekend to bash together some simple code to get this working.

Why are you using the Scanner class? Can't you just do an infinite while loop and keep on reading lines from the keyboard until the user chooses to quit?
__________________
Master of common sense. If you don't like it, stop reading.
UK31337 is offline   Reply With Quote
Old 10-09-2008, 08:07 PM   #16
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: i need some java help

im new to java. we were told to use the scanner class lol.
ill see if this works.
__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se is offline   Reply With Quote
Old 10-09-2008, 08:39 PM   #17
Daemon Poster
 
xtremezx2's Avatar
 
Join Date: Sep 2006
Posts: 915
Default Re: i need some java help

Did you add in a line to have it print out what your capturing for input?
Code:
System.out.println("'" + phrase + "'"); //This will print out what is contained
                                                  //in your variable.
The only way that it will match the way you have it is to type it in the proper case, another cause could be that it is picking up the carriage return at the end of the line when you hit enter. I know this is the case in C++ console apps so it seems quite feasible for it to be the same in java. If adding the above line the second single quote is on a different line then this would be the case. Put that line it and it will give you back what you are reading in between 2 single quotes so you know what is going wrong. I don't have any java stuff on my computer so I can't really compile and go through it for you, but debugging is the hard part about programming most of the time, hopefully this helps.
xtremezx2 is offline   Reply With Quote
Old 10-09-2008, 08:48 PM   #18
Golden Master
 
dude_se's Avatar
 
Join Date: Nov 2004
Posts: 8,632
Send a message via AIM to dude_se Send a message via MSN to dude_se
Default Re: i need some java help

aww how can such a simple thing confuse me lol. ill try again in the morning. cheers for the help
__________________

__________________
Laptop spec: ASUS X53E, i5 2430m 2.4ghz, 3gb ram, 320gb hdd, intel hd graphics, usb 3.0
dude_se 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 12:34 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