Results 1 to 8 of 8
- 08-18-2009, 11:04 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Why doesnt my code wait for input?
Hi, Im making a phone number app.
But the problem is that when u press a to add a contact, after doing that
U get the message "Name: "
and then your suppose to wright the name
But it doesnt work cause as soon as that message pops up java assums I'v clicked enter and says that the string is ""
Heres my code:
And still I have the do while name == "" but the proccess ends before I get a chanse of typing anything..PHP Code:import static java.lang.System.out; import java.util.Scanner; public class PhoneNumber { public static void main(String[] args) { // TODO, add your application code char mykey; Scanner cont = new Scanner(System.in); char reply; do { out.println("Wellcome to Andreas PhoneNumber manager!"); out.println("Choose an option:"); out.println("a: Add contact"); out.println("b: Find contact"); out.println("c: Remove contact"); out.println("d: exit"); reply = cont.findWithinHorizon(".",0).charAt(0); } while (reply != 'a' && reply != 'b' && reply != 'c' && reply != 'd' ); if (reply == 'a'){ out.print("Name: "); String name = ""; do{ name = cont.nextLine(); }while (name==""); out.println(name); } } }
Why? It have not occured before..
-
Where did you get this construct?
Myself, I'd use this:Java Code:reply = cont.findWithinHorizon(".", 0).charAt(0);
also this is no good:Java Code:reply = cont.nextLine().charAt(0);
as you almost never want to use == to compare String contents (the == compares String objects which is an entirely different thing). You'd want to do insteadJava Code:while (name == "");
orJava Code:while (name.trim().equals(""));
Java Code:while (name.trim().isEmpty());
-
or perhaps better, to take into account upper case letters and cases where the returned line is empty:
Java Code:String stringReply = cont.nextLine().trim(); if (!stringReply.isEmpty()) { reply = stringReply.toLowerCase().charAt(0); }
- 08-18-2009, 11:16 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
AHHHHH!!
Lawls Im a previus python programmer so I tend to forget that == is not actually equal in java XD
Thanks man =)
And the first one I found in my java for dummies XD
-
I've not used the Scanner#findWithinHorizon method so I really can't comment on it, good or bad.
The problem with == is that it checks to see if one String object is the exact same object as another String object, and you really don't care about this. You want to check that the contents of the String objects (the text itself) is the same, and that's where equals() or equalsIgnoreCase() comes in very handy.
-
I've not used the Scanner#findWithinHorizon method so I really can't comment on it, good or bad.
The problem with == is that it checks to see if one String object is the exact same object as another String object, and you really don't care about this. You want to check that the contents of the String objects (the text itself) is the same, and that's where equals() or equalsIgnoreCase() comes in very handy.
- 08-19-2009, 10:19 AM #7
One more suggestion..
For making ur code wait for the user input u should place the cursor properly.
So,before do u add this below.
cont.nextLine();
do{
name = cont.nextLine();
}while (name.trim().equals(""));Ramya:cool:
- 08-19-2009, 10:25 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
The Scanner.findWithinHorizon only pulls out the character typed, but leaves the rest of the line (the newline character(s)) in the buffer. When you then call Scanner.nextLine, you get the rest of the line (the newline character(s)). If you call Scanner.nextLine once more, it will wait for your input.
Basically, you're using the wrong Scanner method for your requirements. Use Scanner.nextLine().charAt(0) as Fubarable suggested.
Similar Threads
-
My blackjack games random generator doesnt work!
By Addez in forum New To JavaReplies: 16Last Post: 08-17-2009, 05:29 AM -
Pressing ENTER when Button is selected doesnt fire buttonActionPerformed...
By l245c4l in forum Advanced JavaReplies: 2Last Post: 04-12-2009, 10:39 AM -
JTable doesnt show columun names!
By phil128 in forum AWT / SwingReplies: 3Last Post: 03-08-2009, 10:39 PM -
java doesnt allow vista to work
By 10rosas in forum New To JavaReplies: 5Last Post: 12-22-2008, 04:23 PM -
My program doesnt display anything
By Bojevnik in forum AWT / SwingReplies: 2Last Post: 10-19-2007, 02:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks