I just started learning java and I want to make a simple program where it requests the persons name, outputs the name then asks for the thier favorite number. It will then compare their number to the number 6 and will output something depending on if the number is larger or smaller than 6.
I am getting a "String to int convert" error in Netbeans which has to do with the scanner. Hopefully I am asking this correctly but how can I make the scanner pick-up integers? Thanks
package javaapplication2; import java.util.Scanner; import java.lang.String; public class JavaApplication2 { public static void main(String[] args) { // Creating an instance of the scanner class. // Gets name and numbers. Scanner getName = new Scanner(System.in); Scanner getNumber = new Scanner(System.in); //Holds name and number String userName; int userNumber; // Asks for the users name. // Holds name in userName. System.out.println("What is your name?"); userName = getName.nextLine(); //Reponds with the users name. System.out.println("Hello" + userName + "!"); //Asks for favorite number. // Holds number in userNumber. System.out.println("What is your favorite number?"); userNumber = getNumber.nextLine(); // Checks if users number is larger than 6. if (userNumber > 6) { // Stuff goes here. } } }
documentation- docs.oracle.com/javase/8/docs/api/java/util/Scanner.html