开发者

Problem in looping when using method in Java

开发者 https://www.devze.com 2023-01-10 19:45 出处:网络
I\'m doing a simple program regarding methods. But I have one problem. Everything is already working except when looping.

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.ou开发者_JAVA百科t.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


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.


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));


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消