-2

Picture of error

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

}

Deemahom
  • 1
  • 1
  • `Int` is not a type, that's why you get the "cannot find symbol". Either `int` or `Integer` can be used, but you have to use `nextInt()` instead of `nextLine()` since that returns a `String`. – f1sh Jan 14 '19 at 14:45

3 Answers3

0

nextLine() returns a String, as said in the javadoc of Scanner. Use nextInt() if you want to get and store a int.

Olf
  • 374
  • 1
  • 20
0

In the Scanner class, you use nextInt() when expecting the token to be an integer.

Int alder = scanner.nextInt()

0

Read string using nextLine and then convert to int

String reply = sc.nextLine();
int alder = Integer.valueOf(reply).intValue;
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52