Results 1 to 8 of 8
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default 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:

    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);
        	}
        }
    }
    And still I have the do while name == "" but the proccess ends before I get a chanse of typing anything..
    Why? It have not occured before..

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Where did you get this construct?
    Java Code:
    reply = cont.findWithinHorizon(".", 0).charAt(0);
    Myself, I'd use this:
    Java Code:
    reply = cont.nextLine().charAt(0);
    also this is no good:
    Java Code:
    while (name == "");
    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 instead
    Java Code:
    while (name.trim().equals(""));
    or
    Java Code:
    while (name.trim().isEmpty());

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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);
          }

  4. #4
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    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:

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Addez View Post
    .. the proccess ends before I get a chanse of typing anything..
    Why? It have not occured before..
    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

  1. My blackjack games random generator doesnt work!
    By Addez in forum New To Java
    Replies: 16
    Last Post: 08-17-2009, 05:29 AM
  2. Replies: 2
    Last Post: 04-12-2009, 10:39 AM
  3. JTable doesnt show columun names!
    By phil128 in forum AWT / Swing
    Replies: 3
    Last Post: 03-08-2009, 10:39 PM
  4. java doesnt allow vista to work
    By 10rosas in forum New To Java
    Replies: 5
    Last Post: 12-22-2008, 04:23 PM
  5. My program doesnt display anything
    By Bojevnik in forum AWT / Swing
    Replies: 2
    Last Post: 10-19-2007, 02:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •