Results 1 to 5 of 5
  1. #1
    dhanh90 is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default do-while loop doest work here? help me

    why the do-while couldnt return the main body when i did once.when I input n<2 and press enter, it stops and turns to error.
    i dont know why?could i any one told me why and how to change it and is there another code to do this loop( instead do-while).thanks alot.
    Here the code:

    Help with Code Tags



    Java Code:
      /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package beffer;
    
    import java.util.Scanner;
    
    /**
     *
     * @author lamborghini
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Scanner in= new Scanner(System.in);
            int Words=0;
            int n=0;
           
          do{
             String Name=in.nextLine();
          
            StringBuffer name= new StringBuffer(Name);
            
            
            name.setCharAt(0,Character.toUpperCase(name.charAt(0)));
            for(int i=0;i<name.length();i++){
                if(name.charAt(i)==' '){
                    name.setCharAt(i+1, Character.toUpperCase(name.charAt(i+1)));
                    Words++;
                }
    
            }
    
            String MiddleName= name.substring(Name.indexOf(' ')+1,Name.lastIndexOf(' '));
            String FirstName=name.substring(0, Name.indexOf(' '));
            String LastName=name.substring(Name.lastIndexOf(' '),name.length());
            System.out.println(name);
            System.out.print(MiddleName+"\n");
            System.out.print(FirstName+"\n");
            System.out.print(LastName+"\n");
            System.out.println("Words :"+ (Words+1));
            System.out.print("Want more");
            n=in.nextInt();
            //System.exit(0);
          } while  (n<2);
            // TODO code application logic here
        }
    
    }
    Last edited by dhanh90; 10-17-2009 at 09:41 AM.

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

  3. #3
    dhanh90 is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    when i input n=1 or 2,it doesnt continue working( doesnt way back to the main).So i just input the name once and can not do for the next.

  4. #4
    Willi's Avatar
    Willi is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    for(int i=0;i<name.length();i++){
    if(name.charAt(i)==' '){
    name.setCharAt(i+1, Character.toUpperCase(name.charAt(i+1)));
    Words++;
    }
    If you pay attention at the stack trace after you run the program, it throws a StringIndexOutOfBoundsException, not the problem of n variable. I think the problem lies at the for loop above. I am not sure, but you should take a try. :)
    Write a program to achieve anything you want in your life!

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    The rule for writing any loop or recursion is that there must be an end condition, and that the loop must make progression towards that end condition.

    Examine the end condition in that light, maybe print out the value of n in the loop.

    The common error for a new programmer is that the loop variable is not updated. Endless loop every time. Quite typically, an index variable goes off into never-never land pointed to by null.

Similar Threads

  1. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 10:33 PM
  2. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  3. Why won't this while loop work?
    By trueblue in forum New To Java
    Replies: 6
    Last Post: 05-23-2009, 08:10 PM
  4. my loop doesn't work.. pls help???
    By ashton in forum New To Java
    Replies: 5
    Last Post: 01-16-2009, 08:24 AM
  5. Why doesn't my loop work?
    By d0nmin0 in forum Advanced Java
    Replies: 8
    Last Post: 05-26-2008, 06:56 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
  •