0

I am new to java and to IntelliJ I've started executing some Java codes, and I am wondering why the infinite loop show more results than requested : there is the code :

import java.io.IOException;

public class Main {

  public static void main(String[] args) throws IOException {

    // write your code here
    char ch = 0;

    for(;;){
        System.out.println("Enter your character");
        ch=(char)System.in.read();
        System.out.println("you've entered "+ " "+ch);
        if(ch =='S'){
            break;
        }
        else{
            System.out.println("Type S to Stop");
        }
    }
  }
}

when I type A,the result was the following:

Enter your character
A
you've entered  A
Type S to Stop
Enter your character
you've entered  

Type S to Stop
Enter your character

I don't understand why it doesen't stop and ask for the next character,instead,it gives :

you've entered  

Type S to Stop
Splintos
  • 1
  • 3
  • Seems to work okay for me – MadProgrammer May 30 '21 at 21:59
  • 1
    Because when type A and press Enter, two characters are written to the stream: `'A'` and `'\n'`. Since [`System.in.read()` reads the next byte of data from the input stream](https://stackoverflow.com/a/34120618/9997212), the first time `chr` will be `'A'` and the second one it will be `'\n'`. – enzo May 30 '21 at 22:01
  • Well,I have to press enter in order to pass te request? any solution ? – Splintos May 30 '21 at 22:15
  • how can I force IntelliJ to pick only one character? – Splintos May 30 '21 at 22:25
  • Does this answer your question? [Take a char input from the Scanner](https://stackoverflow.com/a/13942707/9997212) – enzo May 30 '21 at 22:44

2 Answers2

0

I would recommend to try using Scanner next time. Scanner is a good class to use when it comes to scan lines, ints, doubles etc.

I wrote down an solution for you so you can follow along:

import java.io.IOException;
import java.util.Scanner;
public class Main {

public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter your character: ");
    System.out.println("Type S to Stop");
    char ch = scan.nextLine().charAt(0);
    System.out.println("you've entered "+ " "+ch);
    
    while(ch != 'S'){
        System.out.println("Enter your character");
        ch = scan.nextLine().charAt(0);
        System.out.println("you've entered "+ " "+ch);
    }
    System.out.println("Good bye");
}
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
huntgang99
  • 61
  • 3
0

thank you @enzo you helped me. here is my solution and it works :

package com.company;

import java.io.IOException;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {

        // write your code here
        char c = 0;


        for(;;){
            Scanner sc = new Scanner(System.in);
             c = sc.next().charAt(0);
            System.out.println("you've entered "+ " "+c);
            if(c =='S'){
                break;
            }
            else{
                System.out.println("Type S to Stop");
            }
        }

    }
}
Splintos
  • 1
  • 3