Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How to loop a main method

  1. #1
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default How to loop a main method

    I have to make the main method of the following program loop until a blank is entered, how would I change this to do so? thanks in advance

    public static void main(String[] args) {
    Scanner reader = new Scanner (System.in);
    System.out.println("What is the name you want to test? ");
    String name = reader.nextLine();
    convertName(name);

    }
    public static boolean hasComma(String name){
    boolean oName;
    int x= name.indexOf(',');
    if (x == -1) {
    oName= false;
    }else{
    oName= (true);
    }
    return (oName);
    }
    public static boolean convertName(String name) {
    boolean convert = false;
    hasComma(name);
    if (hasComma(name)== true) {
    convert= true;
    }else{
    String s = name;
    String[] arr = s.split(" ");
    System.out.println (arr[1]+ ","+ arr[0]);
    }
    return (convert);
    }
    }

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

    Default Re: How to loop a main method

    You don't want to loop the main method itself, and you really can't do this in a straight-forward fashion anyway (let's avoid recursion, OK?). Instead you probably want to loop a block of code *inside* the main method, and to do this, use a loop such as a for loop or while loop.
    joshdgreen likes this.

  3. #3
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default Re: How to loop a main method

    I was messing around with the code and I found that a while loop worked best for me during testing.
    Sincerely, Joshua Green
    Please REP if I help :)

  4. #4
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: How to loop a main method

    First off, you should really wrap up your code in [code] tags so that it is much easier to read.

    Java Code:
    do{
    
    ...code...
    
    
    }while(condition);
    This will loop the ...code... until the condition is evaluated as false. The condition in your case is if stringVariable is equal to 'blank'. To compare a string you need to use the .equals() method; and you can use the '!' to not the condition. Just make this loop around where you get the variable name, and it will continue to loop as long as !name.equals("").


    If your condition, in any case, is a boolean, you do not need to compare it to anything; as it is automatically evaluated as true or false. In your code, you have:
    Java Code:
    if (hasComma(name)== true) {
    As hasComma() returns a boolean variable, if it is true, it will be evaluated as true, and if it is false, it will be evaluated as false. This means you just need to have:
    Java Code:
    if (hasComma(name)) {
    The default value of any boolean variable is automatically false, so you don't need:
    Java Code:
    boolean convert = false;
    You could just use:
    Java Code:
    boolean convert;
    As it does the same things.
    Last edited by penguinCoder; 10-17-2012 at 06:15 AM.

Similar Threads

  1. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 09:30 AM
  2. Calling The main method from another method
    By SwissR in forum New To Java
    Replies: 3
    Last Post: 07-27-2010, 12:03 PM
  3. Help with main method
    By eliCanzee in forum JDBC
    Replies: 4
    Last Post: 01-06-2010, 10:12 AM
  4. calling method from main method
    By bob_bee in forum New To Java
    Replies: 4
    Last Post: 10-02-2009, 06:30 PM
  5. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 10:25 AM

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
  •