Jump to content

Java error: cannot find symbol

Go to solution Solved by minibois,

Pretty sure instead of the nextLine(); on the Int, you need to use nextInt();

 

Basically you're trying to read an int into the program, while it is expecting a String with nextLine() and using nextInt will allow it to accept an int.

 

Also, yes int is preferred for age input; as that is actually a number you can do calculations with.

Hello. First program I make after hello world. First name, 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? 

 

Code is below the picture.

 

oop.PNG.1117477229fb67b7528cb669727c9887.PNG

import java.util.Scanner;

      /*Oppgave 2a: Hvis filnavn skal endres, så må også public class endres til det samme,
        som kan sees nedenfor (public class Oppgave2)                                    */

public class Oppgave2 {

    public static void main(String[] args) {
        // Oppretter og instansierer et objekt av typen Scanner
        Scanner scanner = new Scanner(System.in);

        // Printer ut til konsollen
        System.out.println("Skriv inn fornavn: ");
        // Leser input fra konsollen
        String fornavn = scanner.nextLine();

        System.out.println("Skriv inn etternavn: ");
        String etternavn = scanner.nextLine();


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

}

 

Main:  1650 v2   @ 4,6GHz   -   X79 Deluxe                -   GTX 1080 @ 2000MHz   -   24GB DDR3 @ 2400MHz / CL10

Side:   i7-4790K @ 4,5GHz   -   Maximus 7 Hero        -   GTX 1070 @ 2114MHz    -  16GB DDR3 @ 2666MHz / CL12

 

HWBOT

Link to comment
https://linustechtips.com/topic/1021248-java-error-cannot-find-symbol/
Share on other sites

Link to post
Share on other sites

Pretty sure instead of the nextLine(); on the Int, you need to use nextInt();

 

Basically you're trying to read an int into the program, while it is expecting a String with nextLine() and using nextInt will allow it to accept an int.

 

Also, yes int is preferred for age input; as that is actually a number you can do calculations with.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

Is there any reason you are using Atom instead of a more "helpful" IDE? Atom is fine for stuff like PHP, JS, HTML, CSS,.... but once you get to more "complex" object oriented languages it gets messy(lots of stuff to remember). That's when you really benefit from a good IDE.

 

I would suggest you look into IntelliJ (https://www.jetbrains.com/idea/), its free(with some limitations on a couple of frameworks,..) and they even give out free licences for students.. :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×