Results 1 to 4 of 4
  1. #1
    LoViNgHeArTy is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default Please show an example of a String in a while loop.

    Would someone show me an example of a string in a while loop please? I have no idea how so I can't show you what I really meant but this is what I actually wanted to do.

    Basically what I wanted was while a user input (which is a name) is not equal to a string. So if someone enter a integer or a number, it prints out "Please enter a name. If the user enter a string, the program continues. I'm terribly sorry if I did not really explain well.

    Thank You.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by LoViNgHeArTy View Post
    Would someone show me an example of a string in a while loop please?
    Sure, no problems
    Java Code:
    int count = 0;
    while(count < 5) {
        System.out.println("hello");
        count++;
    }
    Basically what I wanted was while a user input (which is a name) is not equal to a string. So if someone enter a integer or a number, it prints out "Please enter a name. If the user enter a string, the program continues. I'm terribly sorry if I did not really explain well.

    Thank You.
    Ahhh! more details.
    Java Code:
    declare boolean
    while boolean is true {
        get user input
        if input is invalid {
            boolean is false
        }
    }
    That is a general idea of what to do. Now all you need to do is flesh it out.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    When you get user input it's always a string.

    The best way to approach this would be to get some input and try to parse it.
    Java Code:
    get input
    try{
      Integer.parseInt(input)
    } catch(NumberFormatException nfe){
      //continue with the program
    }
    otherwise ask for input again
    The parse int method will throw the exception when it tries parsing something that isn't a string representation of an integer, if this happens you know that a non integer was entered and you can break out of the loop. You can repeat this process in the loop until the exception is caught and thrown.

    There may be other approaches, but they may take more logic.

    You can create a method called
    Java Code:
    public static boolean isWord(String s)
    which loops through each character and tests if they are letters. Check the character API to see if there is a good method for this.

  4. #4
    LoViNgHeArTy is offline Member
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks. I'll try my best to digest it. I'll get back if I face any problems.

Similar Threads

  1. String in While loop
    By nebulared in forum New To Java
    Replies: 4
    Last Post: 02-20-2011, 04:19 PM
  2. string loop
    By durdanto in forum New To Java
    Replies: 4
    Last Post: 02-18-2011, 09:06 AM
  3. String and while loop
    By Exception in forum Java Applets
    Replies: 5
    Last Post: 09-24-2009, 12:32 PM
  4. String array for Choice, JOptionPane doesn't show
    By themburu in forum Java Applets
    Replies: 5
    Last Post: 05-29-2008, 01:10 PM
  5. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM

Posting Permissions

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