Results 1 to 6 of 6
  1. #1
    ashton is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default my loop doesn't work.. pls help???

    hi guys! I'm new to java and trying some basic things with input and output..

    I tried to write a program that will ask for user's name and age, but, if the
    user doesn't enter anything, the program must ask for the user to enter
    something... That's where i tried to use the 'while(condition)' loop...
    It should keep going 'round and round until the user enters something significant...

    for my newbie eyes, everything seems to be quite right.. but there's something fishy here.. Please check my code coz i really can't find out what's making it not work... here's my code:

    Java Code:
    /*
     * this program will:
     * ->ask the user's name;
     * ->ask the user's age;
     * ...and display it on the
     *    output window...
     * BUT CHECK FOR ERRORS...
     */
    package iogetinput;
    
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    /**
     *
     * @author Kent
     */
    public class basicIO {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            getUserInfo();
        }
    
        public static void getUserInfo() {
            String name = null;
            boolean x = true;
            int age = 0;
    
            try {
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(isr);
                System.out.print("Type in your name: ");
                name = br.readLine();
    [COLOR="Sienna"]//this is where the problem starts, if a null value is entered, it jumps to
    //the next loop, and doesn't display that the user hasn't entered anything..[/COLOR]
                if (name == null) {
                    x = false;
                    while (x == false) {
                        System.out.println("Whooops! You entered Nothing!" +
                                "\n Please, enter a name: ");
                        name = br.readLine();
                        if (name != null) {
                            x = true;
                        }
                    }
                    System.out.print("Hi " + name + " how old are you?: ");
                    age = br.read();
    
                    if (age == 0) {
                        x = false;
                        while (x == false) {
                            System.out.println("Entered nothing again!" +
                                    "\n Please, enter your age: ");
                            age = br.read();
                            if (age > 0) {
                                x = true;
                            }
                        }
                    }
                }
                System.out.println("Your name is: " + name + "\r\nYour age is: " + age);
            } catch (IOException ex) {
                Logger.getLogger(basicIO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    Last edited by ashton; 01-16-2009 at 07:21 AM. Reason: super big

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    When you press enter key in dos, there's some special character entered. So its not null. I've forgot what it was. But I've found a working solution for you.

    if (name.equals("")) {
    *there's no space between the quotes.

    now you have another problem. what happens when the user enters another null the second time, and third time, so forth...

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    In all my experience with Java, I've never tried to read from the prompt. However, I'm under the impression that if the input is nothing, it will be an empty string NOT a null object. These two are very different things that are one of the common confusion points of Java.

    String s; // s is null, s is not empty.
    String t = ""; // t is empty, t is not null.

    When the user inputs nothing, the latter case should occur.

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    oh yes, that jogged my human ram. thanks.

  5. #5
    ashton is offline Member
    Join Date
    Jan 2009
    Posts
    31
    Rep Power
    0

    Default ...

    well, i guess the difference between null and empty is the one i forgot!
    harharhar!! tnx very much, i'll try this...

    hmm.. so far, is my code alryt then???

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    small minor stuff, i'm sure you'll manage.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. Why doesn't my loop work?
    By d0nmin0 in forum Advanced Java
    Replies: 8
    Last Post: 05-26-2008, 06:56 PM
  2. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 02:58 AM
  3. Work On Lucene
    By peiceonly in forum Lucene
    Replies: 1
    Last Post: 08-07-2007, 05:47 PM
  4. what do I have to install to work with JSP
    By boy22 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 04:08 AM
  5. Program don't work
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-04-2007, 09:51 PM

Posting Permissions

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