First program I make after hello world. Firstname, lastname and age. When using String on age, everything works as expected. I was told to use Int on age instead of String, but when I changed to Int and try to compile, I get this error: "cannot find symbol". I guess I have to do something completely different if I want age as an Int. What am I doing wrong?
import java.util.Scanner;
public class Oppgave2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Skriv inn fornavn: ");
String fornavn = scanner.nextLine();
System.out.println("Skriv inn etternavn: ");
String etternavn = scanner.nextLine();
System.out.println("Skriv inn alder: ");
int alder = scanner.nextLine();
System.out.println("Ditt navn er: " + fornavn + " " + etternavn);
System.out.println("Din alder er: " + alder);
}
}
nextLine()
returns aString
, as said in the javadoc of Scanner. UsenextInt()
if you want to get and store a int.