2

I'm doing a simple program regarding methods. But I have one problem. Everything is already working except when looping. When I choose to loop again. The program skips on inputting the name. And proceeds directly to the year and section. Here's the code:

public static void main(String[] args) {
do{
    System.out.println("Input info:");
        name=stringGetter("Name: ");
        yearandsec=stringGetter("Year and section: ");
        sex_code=charGetter("Sex code: " + "\n"  + "[M]" + "\n" + "[F]:");
        scode=intGetter("Scholarship code: ");
        ccode=intGetter("Course code: ");
        units=intGetter("Units: ");

        fee_per_unit=doubleGetter("Fee per unit: ");
        misc=doubleGetter("Miscellaneous: ");
        display();
         switches(scode, units, fee_per_unit, misc);
System.out.println("Another?");
dec=rew.nextInt();
}while(dec==1);




    }

Here's the method getting the value for name together with the year and section:

public static String stringGetter(String ny){
       String sget;
        System.out.println(ny);
       sget=rew.nextLine();
       return sget;

    }

I'm really annoyed with this problem, and I don't have any idea on how to fix this. Please help. thanks

2
  • rew.nextLine()? Is rew a java.util.Scanner? Commented Aug 7, 2010 at 8:43
  • yup I added it like this: public static Scanner rew= new Scanner(System.in); It doesnt let me input the name when I try to loop Commented Aug 7, 2010 at 8:45

3 Answers 3

2

Here is a simpler and more complete program that reproduces the error:

public static Scanner rew = new Scanner(System.in);

public static void main(String[] args) {
    int dec;
    do {
        System.out.println("Input info:");
        String name=stringGetter("Name: ");
        String yearandsec=stringGetter("Year and section: ");
        dec=rew.nextInt();
    } while(dec==1);
}

public static String stringGetter(String ny){
    System.out.println(ny);
    return rew.nextLine();
}

The problem is that after calling nextInt() the call to nextLine() reads up to the new line after the int (giving a blank line), not up to the next new line.

If you change dec to a String and change dec=rew.nextInt(); to dec=rew.nextLine(); then it will work fine. Here is a complete example that you can copy and paste into a blank file to see that it works correctly:

import java.util.*;

public class Program
{
    public static Scanner rew = new Scanner(System.in);

    public static void main(String[] args) {
        String dec;
        do {
            System.out.println("Input info:");
            String name = stringGetter("Name: ");
            String yearandsec = stringGetter("Year and section: ");
            dec = stringGetter("Enter 1 to continue: ");
        } while(dec.equals("1"));
    }

    public static String stringGetter(String ny){
        System.out.println(ny);
        return rew.nextLine();
    }
}

You may also want to consider adding proper parsing and validation to your program. Currently your program will behave in an undesirable way if the user enters invalid data.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but I really am unlucky today. I changed the code similar to what you did. But its now not looping at all. Please see the edited code above.
@user225269: The last code example is a complete example. You can just copy and paste it into a blank file and run it to see that it works.
1

The line:

dec = rew.nextInt();

Is reading an int value from the input stream and is not processing the newline character, then when you come back to point where you get the name at which point a new line is still in the Reader's buffer and gets consumed by the stringGetter returning an empty value for name.

Change the line to do something like:

do {
    //....
    s = stringGetter("Another (y/n)? ");
} while ("y".equals(s));

Comments

1

Well you haven't told us what "rew" is, nor what rew.nextInt() does. Is it possible that rew.nextInt() is waiting for the user to hit return, but only actually consuming one character of the input - so that the next call to rew.nextLine() (for the name) just immediately takes the rest of that line? I suspect that's what's happening because you're using System.in - usually reading from System.in only gives any input when you hit return.

(It's possible that this is also only a problem on Windows - I wonder whether it consumes the "\r" from System.in as the delimiter, leaving "\n" still in the buffer. Not sure.)

To test this, try typing in "1 Jon" when you're being asked whether or not to continue - I think it will then use "Jon" as the next name.

Essentially, I think using Scanner.nextInt() is going to have issues when the next call is to Scanner.nextString(). You might be better off using a BufferedReader and calling readLine() repeatedly, then parsing the data yourself.

1 Comment

@user225269: As I said at the bottom, consider explicitly reading a line at a time and parsing that, rather than using Scanner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.