0

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);
    }
    }
mohsen.nour
  • 852
  • 3
  • 16
  • 24
  • Minor point: consider a `do {} while ();` loop instead - you're guaranteed one iteration that way. Also, adding `System.out.println(Character.getName(cnt));` to your loop should show you exactly why it doesn't work. – JonK Mar 04 '15 at 09:24

4 Answers4

1

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);
        }
    }
}
vincent
  • 1,191
  • 10
  • 22
  • In systematIO method, after pressing the enter, character works fine And show the value, but in cntApp method it's not we have two same methods whic act different in System.in – mohsen.nour Mar 04 '15 at 09:43
  • 1
    not really, all the characters entered are in a buffer – vincent Mar 04 '15 at 10:23
  • with System.in i think the only way is to read until is to read it until it's null. That's the reason why i use Scanner object. – vincent Mar 04 '15 at 10:28
1

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();
    }
...
owenrb
  • 515
  • 2
  • 7
1

With reference to this answer.

You can add below line add the end of systemIO() and cntApp() method i.e. 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.

Community
  • 1
  • 1
Naman Gala
  • 4,480
  • 1
  • 18
  • 48
0

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);
    }
}
nomis
  • 2,045
  • 11
  • 15
  • They could, but *why* does that solve their issue? It would be helpful to explain what the problem actually is before offering a solution to it. – JonK Mar 04 '15 at 09:27