Monday, April 3, 2017

Compilation errors in simple Java program with Scanner

Leave a Comment
import java.util.*; public class strings {     public static void main (String [] args) {         Scanner keyboard = new Scanner(System.in);         System.out.print("Type your first integer: ");         int first = keyboard.next();         System.out.print("Type your seconds integer : ");         int second = keyboard.next();         System.out.print("The sum of your two integers are:");     } } 

I dont know why im getting 2 errors on strings cannot converted to int.

13 Answers

Answers 1

keyboard.nextInt();  

instead of

keyboard.next(); 

If you want to use keyboard.next();, then change int first to String first.

Modify your code to this:

import java.util.*; public class Demo {     public static void main (String [] args) {         Scanner keyboard = new Scanner(System.in);         System.out.print("Type your first integer: ");         int first = keyboard.nextInt();          System.out.print("Type your seconds integer : ");         int second = keyboard.nextInt();          System.out.print("The sum of your two integers are:" +(first+second));     } } 

Change your class name to another instead of string.

Answers 2

There are multiple ways to do this, depending on how much validation you want to do.

In the solution below first number is obtained using .nextInt(). Note that if it is not an integer we need to clear the stream input, so in catching of InputMismatchException we do call .next(). Otherwise the same exception would keep occurring since the input has not been cleared

The second number is obtained using .next() which returns a string and we cast that string to the integer. Note that here we catch a different exception NumberFormatException to ensure that the input is still good. (we do not call .next())

 public static void main (String [] args) {     try (Scanner keyboard = new Scanner(System.in)) {         System.out.print("Type your first integer: ");         int first;         do {             try {                 first = keyboard.nextInt();                 break;             } catch (InputMismatchException e) {                 keyboard.next();                 System.out.print("Invalid first integer, try again: ");             }         } while (true);         System.out.print("Type your seconds integer : ");         int second;         do {             try {                 second = Integer.valueOf(keyboard.next());                 break;             } catch (NumberFormatException e) {                 System.out.print("Invalid seconds integer, try again: ");             }         } while (true);         System.out.print("The sum of your two integers are: " + (first + second));     } } 

One last thing I did was try on the resource, this way the input stream will be closed regardless if the program exits normally or throws an exception.

Answers 3

Scanner class next() method returns String value. You can do anyone of the following steps to solve the compilation error.

  1. You need to convert the String value to int.

    int first = Integer.parseInt(keyboard.next()); 
  2. You can use nextInt() method to get int value directly from the user.

Answers 4

This will read the whole line then convert it to an Integer

int first = Integer.paresInt(keyboard.nextLine());

or

If you are sure input is in digit

int first = keyboard.nextInt();

Answers 5

I have three solutions for this problem :

First

In your code you are using keyboard.next() which will return a string that you have to convert it into Integer. For that you can use keyboard.nextInt(), it will directly convert the string into integer.

import java.util.Scanner;  public class strings {     public static void main(String[] args) {         Scanner keyboard = new Scanner(System.in);         System.out.print("Type your first integer: ");         int first = keyboard.nextInt();         System.out.print("Type your seconds integer : ");         int second = keyboard.nextInt();         System.out.print("The sum of your two integers are:"+(first+second));     } } 

Second

or you can convert the string into Integer by using Integer.parseInt(**String to be converted to Integer**), this will convert the string into Integer.

import java.util.Scanner;  public class strings {     public static void main(String[] args) {         Scanner keyboard = new Scanner(System.in);         System.out.print("Type your first integer: ");         int first = Integer.parseInt(keyboard.next());         System.out.print("Type your seconds integer : ");         int second = Integer.parseInt(keyboard.next());         System.out.print("The sum of your two integers are:"+(first+second));     } } 

Third

or you can convert the string into Integer by using Integer.valueOf(**String to be converted to Integer**), this will convert the string into Integer.

import java.util.Scanner;  public class strings {     public static void main(String[] args) {         Scanner keyboard = new Scanner(System.in);         System.out.print("Type your first integer: ");         int first = Integer.valueOf(keyboard.next());         System.out.print("Type your seconds integer : ");         int second = Integer.valueOf(keyboard.next());         System.out.print("The sum of your two integers are:"+(first+second));     } } 

Answers 6

You can use scanner.nextLine() (reads the input till end of the line) which gives String output and convert it to int using Integer.parseInt as shown below:

          Scanner keyboard = new Scanner(System.in);           try {               System.out.print("Type your first integer: ");               int first = Integer.parseInt(keyboard.nextLine());               System.out.print("Type your seconds integer : ");               int second = Integer.parseInt(keyboard.nextLine());               int sum  =first+second;               System.out.print("The sum of your two integers are:"+sum);           } catch(Exception exe) {               System.out.println(" Error!!!! Please try again !!!! ");           } finally {               keyboard.close();           } 

Also, try to close the resources (inside finally block, shown above) as a practice.

Answers 7

You are using next() method from Scanner class. This method return String and you cannot store string at int type variable. So you can use nextInt() method for int type input or you can just Store the value at String variable and after that you can convert the String variable to int.

Please follow this code:

import java.util.*; public class strings {     public static void main (String [] args) {         Scanner keyboard = new Scanner(System.in);         System.out.print("Type your first integer: ");         int first = keyboard.nextInt();         System.out.print("Type your seconds integer : ");         int second = keyboard.nextInt();         System.out.print("The sum of your two integers are:" +(first+second));     } } 

or Follow this code

import java.util.*; public class strings {     public static void main (String [] args) {         Scanner keyboard = new Scanner(System.in);         System.out.print("Type your first integer: ");         //Get String input and stroe at String variable         String firstString = keyboard.next();         //Convert the String variable to int          int first=Integer.parseInt(firstString);         System.out.print("Type your seconds integer : ");         //Get String input and stroe at String variable         String secondString = keyboard.next();         //Convert the String variable to int          int second=Integer.parseInt(secondString);         System.out.print("The sum of your two integers are:" +(first+second));     } } 

Answers 8

As others have stated you can use

int first = keyboard.nextInt(); 

but you can also use the next() method along with a parseInt, which can have the handy benefit of showing an error message if you'd like.

import java.util.*; public class myClass { public static void main (String [] args) {     Scanner keyboard = new Scanner(System.in);     System.out.println("Type your first integer: ");     String firstStr = keyboard.next();     System.out.println("Type your seconds integer : ");     String secondStr = keyboard.next();     int first = -1; //assuming only positive integers     int second = -1;      do{       try{       first = Integer.parseInt(firstStr);       second = Integer.parseInt(secondStr);       int sum = first + second;       System.out.println("The sum of your two integers are: " + sum);      } catch (NumberFormatException exception) {       System.out.println("Integers only.");      }   } while (first=-1 && second=-1);  } } 

Answers 9

Change your first and second variables from

int first = keyboard.next();   int second = keyboard.next(); 

to

int first = keyboard.nextInt(); int second = keyboard.nextInt(); 

Answers 10

Problem is here:

int first = keyboard.next(); 

Scanner.next returns String, and you are expecting int, so you need to replace it with

int first = keyboard.nextInt(); 

As well

  int second = keyboard.nextInt(); 

Answers 11

you should replace next with nextInt, like this

int first = keyboard.nextInt(); 

From the API, you can see that the return type of next is String,while nextInt() is int.

Answers 12

Try using

keyboard.nextInt(); 

not

keyboard.next(); 

it displays that it cannot convert because .next is for strings, but for other data types is .next and then the data types with a capital. For example:

long l=keyboard.nextLong();  

and boolean b=keyboard.nextBoolean();

Answers 13

keyboard.next() will return the next complete token from the scanner ,i think of string type so it needs to be typecast to integer and better option is to use keyboard.nextInt() method to take the integer input .

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment