简体   繁体   中英

while loop does not work properly java

In the following code for continuing the program user should press y character but when I press y the loop terminate

public class JavaFile
{

    int i = 0;
    public void systemIO()throws java.io.IOException {
        System.out.println("Enter the character");
        i = System.in.read();
        System.out.println("the character Enter by the user : "+(char)i);
        System.out.println("the assci vlue "+i);
    }
    public void cntApp()throws java.io.IOException{
        char cnt = 'y';
        while(cnt =='y'){
            systemIO();
            System.out.println("press 'y'  if you want continue");
            cnt = (char)System.in.read(); 
            System.out.println("The Entery value   "+cnt);
        }

    }
    public static void main(String args[]) {
        try{
        new JavaFile().cntApp();
    }catch(java.io.IOException ioExe){
        System.out.println(ioExe);
    }
    }

If you run it in debug :

while(cnt =='y'){
        systemIO();
        System.out.println("press 'y'  if you want continue");
        cnt = (char)System.in.read(); 
        System.out.println("The Entery value   "+cnt);
}

You'll see that

cnt = (char)System.in.read();

return '\\r', correspond to the validation by enter.

So, i will use a Scanner instead and it will be something like that :

public class JavaFile {
    int i = 0;
    Scanner reader = new Scanner(System.in);
    public void systemIO()throws java.io.IOException {
        System.out.println("Enter the character");
        i = reader.next().charAt(0);
        System.out.println("the character Enter by the user : "+(char)i);
        System.out.println("the assci vlue "+i);
    }
    public void cntApp()throws java.io.IOException{
        char cnt = 'y';
        while(cnt =='y'){
            systemIO();
            System.out.println("press 'y'  if you want continue");
            cnt = reader.next().charAt(0);
            System.out.println("The Entery value   "+cnt);
        }

    }
    public static void main(String args[]) {
        try{
            new JavaFile().cntApp();
        }catch(java.io.IOException ioExe){
            System.out.println(ioExe);
        }
    }
}

It looks like the keyboard input is buffered and the following code is getting line-terminator/null value that causes the while-loop to terminate.

cnt = (char)System.in.read();

Try refactoring the code using Scanner class instead of System.in.read()

import java.util.Scanner;    

public class JavaFile {
        private Scanner scanner = new Scanner(System.in);
...

    public void cntApp() throws java.io.IOException {

        String line = "y";
        while ("y".equals(line)) {
            systemIO();
            System.out.println("press 'y'  if you want continue");
            line = scanner.nextLine();
            System.out.println("The Entery value   " + line);
        }

        scanner.close();
    }
...

With reference to this answer .

You can add below line add the end of systemIO() and cntApp() method ie after the System.in.read() method call.

System.in.skip(System.in.available());

So your methods will look like this.

public void systemIO() throws java.io.IOException {
    System.out.println("Enter the character");
    i = System.in.read();
    System.out.println("the character Enter by the user : " + (char) i);
    System.out.println("the assci vlue " + i);
    System.in.skip(System.in.available());
}

public void cntApp() throws java.io.IOException {
    char cnt = 'y';
    while (cnt == 'y') {
        systemIO();
        System.out.println("press 'y'  if you want continue");
        cnt = (char) System.in.read();
        System.out.println("The Entery value   " + cnt);
        System.in.skip(System.in.available());
    }
}

Note: But using Scanner(System.in) is better approach then using System.in.read() directly.

You could rewrite your cntApp method to use a Scanner and take the first character entered

public void cntApp() throws java.io.IOException {
    Scanner in = new Scanner(System.in);
    char cnt = 'y';
    while (cnt == 'y') {
        systemIO();
        System.out.println("press 'y'  if you want continue");
        cnt = in.next().charAt(0);
        System.out.println("The Enter value   " + cnt);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM