Results 1 to 9 of 9
  1. #1
    monkeyposeur is offline Member
    Join Date
    Jun 2016
    Posts
    3
    Rep Power
    0

    Default First Java class - simple code doesn't work - basic input & output

    Hello,

    I am in my first Java class. I have no idea why my simple program won't work in JGrasp. If this is the wrong forum for this type of question I apologize. I'm still a total noob.

    Here is the code:
    Java Code:
    import java.util.Scanner;
    public class GetUserInfo
    {
       public static void main(String[] args)
       {
          int age;
          Scanner inputDevice = new Scanner(System.in);
          System.out.print("What is your age==> ");
          age = inputDevice.nextInt();
          System.out.println("Your age is " + age);
          System.out.print("What is your name==> ");
          name = inputDevice.nextLine();
          System.out.print("Your name is " + name);
          }   
    }
    I get this error:

    GetUserInfo.java:12: error: cannot find symbol
    name = inputDevice.nextLine();
    ^
    symbol: variable name
    location: class GetUserInfo
    GetUserInfo.java:13: error: cannot find symbol
    System.out.println("Your name is " + name);
    ^
    symbol: variable name
    location: class GetUserInfo
    2 errors

    ----jGRASP wedge2: exit code for process is 1.
    Last edited by Tolls; 06-23-2016 at 09:32 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: First Java class - simple code doesn't work - basic input & output

    What is 'name'? The compiler didn't see any definition of it ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: First Java class - simple code doesn't work - basic input & output

    When posting code please wrap it in code tags so it retains its formatting, like I've just done for you.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    monkeyposeur is offline Member
    Join Date
    Jun 2016
    Posts
    3
    Rep Power
    0

    Default Re: First Java class - simple code doesn't work - basic input & output

    Quote Originally Posted by JosAH View Post
    What is 'name'? The compiler didn't see any definition of it ...

    kind regards,

    Jos
    'name' is going to be a string as far as I understand. I am only in Chapter 2 of "Java Programming" by Joyce Farrel (7th edition).

    I understand that I need to declare the integer variable 'age', but do I need to define 'name' before using it as a string variable? I basically copied the code from the book (pg. 78). In the book it states that using nextLine() retreives the next line of data and returns it as a string. There is no mention of defining the variable beforehand.

    So I'm still at a loss as to what I need to do.

    Quote Originally Posted by Tolls View Post
    When posting code please wrap it in code tags so it retains its formatting, like I've just done for you.
    Ok. I'll try and figure out how to do that. If you can let me know how to do so I'd appreciate it.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: First Java class - simple code doesn't work - basic input & output

    how to do that.
    To wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    do I need to define 'name' before using it as a string variable?
    Yes. The compiler must know what type a variable is so that it can create the correct code to use it.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: First Java class - simple code doesn't work - basic input & output

    Quote Originally Posted by monkeyposeur View Post
    'name' is going to be a string as far as I understand. I am only in Chapter 2 of "Java Programming" by Joyce Farrel (7th edition).
    The compiler doesn't know anything about your good intentions; it doesn't have a crystal ball so it complains all it can (and rightly so). Add a line 'String name;' following the declaration of that 'int age;' line and see for yourself ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    monkeyposeur is offline Member
    Join Date
    Jun 2016
    Posts
    3
    Rep Power
    0

    Default Re: First Java class - simple code doesn't work - basic input & output

    Sweet! It works now.

    Thanks for the help everyone!

  8. #8
    mmirbekian is offline Member
    Join Date
    Jul 2016
    Posts
    17
    Rep Power
    0

    Default Re: First Java class - simple code doesn't work - basic input & output

    So basically, this is the correct and safe way of doing it: 2 errors you have to declare name as string & u have to have two different scanner declerations for each input:

    import java.util.Scanner;

    public class Phhhn {

    public static void main(String[] args) {

    Scanner inputDevice = new Scanner(System.in);
    System.out.print("What is your age==> ");
    int age = inputDevice.nextInt();
    System.out.println("Your age is " + age);
    Scanner nameEntry = new Scanner(System.in);
    System.out.print("What is your name==> ");
    String name = nameEntry.nextLine();
    System.out.print("Your name is " + name);

    }
    }

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: First Java class - simple code doesn't work - basic input & output

    You only need the one Scanner.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 13
    Last Post: 10-04-2012, 07:45 PM
  2. Simple rectangle display applet doesn't work in browser
    By stainluss in forum Java Applets
    Replies: 7
    Last Post: 01-31-2012, 11:38 PM
  3. Trying to create simple Timer, doesn't work
    By Ragoune in forum New To Java
    Replies: 4
    Last Post: 07-06-2011, 09:24 PM
  4. Replies: 4
    Last Post: 08-23-2010, 03:19 AM
  5. Replies: 3
    Last Post: 09-22-2009, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •