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:

public static void main(String[] args) {
Scanner in= new Scanner(System.in);
int Words=0;
int n=0;

do{{/color}
String Name=in.nextLine();
StringBuffer name= new StringBuffer(Name);
name.setCharAt(0,Character.toUpperCase…
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);

The code doesn't compile. Post the correct 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
    }

}

Its the full code.The problem is it could not return the main body when i press n=0 or 1 and press enter.

The code is correct. You think it's wrong because the code waits for you to enter more values and while it does that it displays nothing. You should always have print messaged before reading from the console:

do{
   [B]System.out.println("Enter you Name: ");[/B]
   String Name=in.nextLine();

......

[B]System.out.println("Enter 0 or 1 to continue: ");[/B]
n=in.nextInt();
} while (n<2);
[B]System.out.println("Finished!");[/B]

No.It still doesnt work.hix..Here what the NetBeans said when i press n=0:

Enter you Name: 
do hoang anh
Do Hoang Anh
Hoang
Do
 Anh
Words :3
Enter 0 or 1 to continue: 
0
Enter you Name: 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.StringBuffer.charAt(StringBuffer.java:162)
        at beffer.Main.main(Main.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 10 seconds)

I believe it's because you didn't enter a name the second. Your code is very poor for handling error inputs. Try this:

do{
   System.out.println("Enter you Name: ");
   String Name=in.nextLine();
   System.out.println("You have entered: "+Name);

Sorry.hix i'm new.but any way the code also can not run.
for the second time,it doesnt allow me to input the Name again.

In my PC the code run OK. Try run it again and post what messages you get

/*
 * 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;
       String Name;
      do{
         // n=in.nextInt();
          System.out.println("Enter you Name: ");

          Name=in.nextLine();
          System.out.println("You have: "+Name);
        StringBuffer name= new StringBuffer(Name);
        //name=in.nextLine();
        
        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("Capitalize name: "+name);
        System.out.print("Middle name is: v"+MiddleName+"\n");
        System.out.print("First name: "+FirstName+"\n");
        System.out.print("Last name: "+LastName+"\n");
        System.out.println("Number of Words :"+ (Words+1));
        System.out.println("Enter 0 or 1 to continue: ");

        n=in.nextInt();
        
      } while  (n<2);
        //System.out.println("Finished!");
        // TODO code application logic here
    }

}

Here the NetBeans said:i dont know why the second time not work.

Enter you Name: 
do do do
You have: do do do
Capitalize name: Do Do Do
Middle name is: vDo
First name: Do
Last name:  Do
Number of Words :3
Enter 0 or 1 to continue: 
0
Enter you Name: 
You have: 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.StringBuffer.charAt(StringBuffer.java:162)
        at beffer.Main.main(Main.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 8 seconds)

It still doesnt work. hix.please..In your computer. does it work? could you type the name for the second time?

Enter you Name:
You have:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.StringBuffer.charAt(StringBuffer.java:162)
at beffer.Main.main(Main.java:33)
Java Result: 1

Of course the program exited because you didn't enter anything the second time:

Enter you Name:
aa bb cc
You have: aa bb cc
Capitalize name: Aa Bb Cc
Middle name is: vBb
First name: Aa
Last name: Cc
Number of Words :3
Enter 0 or 1 to continue:
0
Enter you Name:
dd ee ff
You have: dd ee ff
Capitalize name: Dd Ee Ff
Middle name is: vEe
First name: Dd
Last name: Ff
Number of Words :5
Enter 0 or 1 to continue:
1
Enter you Name:
aa bb
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.StringBuffer.substring(StringBuffer.java:801)
at stam.Main.main(Main.java:47)
You have: aa bb

The third time the program exited with exception because I entered 2 words instead if 3. In your case you entered nothing. You just pressed enter

Enter you Name: 
do do do
You have: do do do
Capitalize name: Do Do Do
Middle name is: vDo
First name: Do
Last name:  Do
Number of Words :3
Enter 0 or 1 to continue: 
0
Enter you Name: 
You have: 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.StringBuffer.charAt(StringBuffer.java:162)
        at beffer.Main.main(Main.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

NO no,in my computer when input 0 (line 10)...it automatically goes to line 17.I dont have any chance to input the name???
A person told me that this problem occurs because the OS of mine.hix

Actually the whole thing was my mistake.
I am sorry.
I was using a wrong jdk and version of your code for testing and I am getting the same errors you get.
I tried some things to fix it with no luck.

Finally I came with this idea:
Replace the nextInt with this:

n=Integer.parseInt(in.nextLine());

Oh it really works.Great.Thanks a million.
But i still dont know why the problem works with my JDK.Could you explain more about my problem?
Thanks again.

Well I don't believe there has something to do with your machine. The problem was the code (and I didn't see it. sorry.)

java.util.Scanner

I believe the problem was the use of both nextLine and nextInt. After you called the nextInt, the loop repeated itself and tried to call the nextLine.
But for some reason that I can't explain it skipped the line you entered or it read on its own an empty line:
nextLine
By adding in both places the nextLine the problem was fixed.

oh Thanks.But any way,i have Some problems with my code.Here, how to modify to input array of string and compare the name(compare first name,if the same first name then compare last name).Tell the smallest one.

oh Thanks.But any way,i have Some problems with my code.Here, how to modify to input array of string and compare the name(compare first name,if the same first name then compare last name).Tell the smallest one.

Actually you cannot input an array of Strings.
Ask the user to enter the length of the array and then do this:

System.out.println("Enter length: ");
int length = Integer.parseInt(in.nextLine());

String [] array = new String[length];

for (int i=0;i<length;i++) {
  System.out.println("Enter name:");
   array[i] = in.nextLine();
}

Now you have the array and do whatever comparisons you want. Use the compareTo method to compare which String is greater than the other for your sorting. Have a separate method that does that.

If you want to "split" the input try this method:

String input = "Name MN Last";
String [] tokens = input.split(" ");
for (int i=0;i<tokens.length;i++) {
   System.out.println(tokens[i]);
}

/////////////////////////////////////////////////////////////

For better solution try:

class Names {
  public String first = "";
  public String last = "";
  public String middle = "";

  public Names() {
  }

  public String toString() {
     return first+" "+middle+" "+last;
   }
}
String input = "Name MN Last";
String [] tokens = input.split(" ");
for (int i=0;i<tokens.length;i++) {
   System.out.println(tokens[i]);
}

Names nm = new Names();
nm.first = tokens[0];
nm.middle = tokens[1];
nm.last = tokens[2];

System.out.println(nm); // the toString method of the object is called automatically
Names [] arrayWithNames = new Names[2];

arrayWithNames[0].first = ....;
arrayWithNames[0].middle = ....;
arrayWithNames[0].last = ....;

for (int i=0;i<arrayWithNames.length;i++) {
   System.out.println(arrayWithNames[i]); 

  // arrayWithNames is an [U]array[/U]
  // arrayWithNames[i] is an [U]Names[/U] object
}

Oh thanks.But what if we dont know the length before.As long as we input name,the program with compare and sort them.

Oh thanks.But what if we dont know the length before.As long as we input name,the program with compare and sort them.

If you don't know the length use the Vector class.

I would like to ask a small code.Suppose i have:
String a=" i love you more!";
int[] b= new int[4];
The question is how to assign the index of something is not a letter in string a to the array b?
Using !Character.isLetter and for loop are ok? i dont know ?? please give me a solution.
Thanks.

You can't put Strings into an integer array. Create a String array instead.

Oh, i mean here is put the INdexOF the spaces to the arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.