Asked  7 Months ago    Answers:  5   Viewed   34 times

I am trying to find a way to take a char input from the keyboard.

I tried using:

Scanner reader = new Scanner(System.in);
char c = reader.nextChar();

This method doesn't exist.

I tried taking c as a String. Yet, it would not always work in every case, since the other method I am calling from my method requires a char as an input. Therefore I have to find a way to explicitly take a char as an input.

Any help?

 Answers

34

You could take the first character from Scanner.next:

char c = reader.next().charAt(0);

To consume exactly one character you could use:

char c = reader.findInLine(".").charAt(0);

To consume strictly one character you could use:

char c = reader.next(".").charAt(0);
Tuesday, June 1, 2021
 
revive
answered 7 Months ago
60

(I think you've already figured this out but ...)

The readLine() method returns the rest of the current line. That is, all unconsumed characters up to the next "end of line" sequence, or the "end of stream", which ever comes first. It will block, waiting until the current line (according to the above) is available.

So if your socket readLine() call blocks, it is waiting for the remote to either send an end-of-line marker (e.g. 'n'), or close its socket output stream (which will result in an "end-of-stream" at this end).

Q: Why does it "work" when you read from the console?

A: The console adds an "end-of-line" sequence to the stream whenever you hit ENTER. (Precisely what sequence is added is OS dependent, but the Scanner class will cope with all common varieties, and some unusual ones too.)


The lesson here is that you should only use Scanner.readLine() if the input stream is line oriented; i.e. if whatever wrote / generated the stream is including "end-of-line" markers.

Monday, August 16, 2021
 
DiglettPotato
answered 4 Months ago
98

You probably want something like this instead:

while (in.hasNext()) {
    System.out.println("letter = " + in.next());
    System.out.println("integer1 = " + in.nextInt());
    System.out.println("integer2 = " + in.nextInt());
}
Saturday, August 28, 2021
 
Oshrib
answered 4 Months ago
60
def characters(c):
  print ' '.join(map(chr, range(ord(c), ord('a'), -1) + range(ord('a'), ord(c)+1)))

>>> characters('d')
d c b a b c d

or

def characters(c):
  for n in xrange(ord(c), ord('a'), -1):
    print chr(n),
  for n in xrange(ord('a'), ord(c)+1):
   print chr(n),
  print
Tuesday, August 31, 2021
 
alez
answered 3 Months ago
41

Scanner.nextInt method only reads the next token from the input passed from user. So, it ignores the linefeed that is at the end of each input. And that linefeed goes as input to the next call to Scanner.nextInt, and hence your input is ignored.

You can use a blank Scanner.nextLine after each call to Scanner.nextInt to consume the linefeed.

if(scanner.hasNextInt()){
    int x = scanner.nextInt();
    scanner.nextLine();  // Add a blank nextLine
    list.add(x);
    System.out.println("user input: " + x);
}

Or you can also use scanner.nextLine() only for reading integers, and convert the input to integer using Integer.parseInt.

if(scanner.hasNextInt()) {
    int x = 0;

    try {
         x = Integer.parseInt(scanner.nextLine());
    } catch (NumberFormatException e) {
         e.printStackTrace();
    }

    list.add(x);
    System.out.println("user input: " + x);
}

Actually, since you are using scanner.hasNextInt in your if, you don't really need that try-catch around your Integer.parseInt, so you can remove that.


UPDATE : -

I would replace your do-while with a while and just remove the if-else check from inside.

while (scanner.hasNextLine()) {
    String input = scanner.nextLine();

    if (input.equals("End")) {
        break;
    } else {
        try {
            int num = Integer.parseInt(input);
            list.add(num);

        } catch (NumberFormatException e) {
            System.out.println("Please Enter an Integer");
        }
    }
}
Tuesday, November 23, 2021
 
octern
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share