0

Heres my code: import java.util.Scanner;

public class BasicInput {
public static void main(String[] args) {

  Scanner scnr = new Scanner(System.in);
  int userInt = 0;
  double userDouble = 0.0;
  char userLetter = 'z';      

  userInt = scnr.nextInt();
  userDouble = scnr.nextDouble();
  userLetter = scnr.nextLine();

  System.out.println("Enter integer: " + userInt);
  System.out.println("Enter double: " + userDouble);
  System.out.println("Enter letter: " + userLetter);      

When I run it with the letter input "z", it gives me this error

    BasicInput.java:13: error: incompatible types
userLetter = scnr.nextLine();
                            ^

required: char

found: String

1 error

What do I do so that it will read a letter as input and output it?

ArK
  • 19,864
  • 65
  • 105
  • 136
Cameron Beers
  • 1
  • 1
  • 1
  • 1

2 Answers2

1

This is trying to store the value of a String into a char - it will not fit

Try

String userLetter = scnr.nextLine();

then just use the first char

userletter.charAt (0);
Scary Wombat
  • 43,525
  • 5
  • 33
  • 63
0

nextLine() accepts String You need to consider userLetter = scnr.next().charAt(0); Take a look at Why can't we read one character at a time from System.in? for further detials about why it's happening.

Community
  • 1
  • 1
Saleh Parsa
  • 1,395
  • 12
  • 22