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));
}
}