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/...troductory/console/console-input-scanner.html
 
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.
 
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.
 
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?
 
im new to java. we were told to use the scanner class lol.
ill see if this works.
 
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.
 
aww how can such a simple thing confuse me lol. ill try again in the morning. cheers for the help :)
 
Back
Top Bottom