Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default New in Java silly question about input

    I'm trying to run this program but I can't something in the input is wrong and I don't know what.

    I JUST STARTED WITH JAVA 1 WEEK AGO. YES I'M A NOOB LOL

    package ifstatement;
    import java.util.Scanner;
    public class exerciseclass {

    /**
    * @param args
    */
    public static void main(String[] args) {
    System.out.print("What is your age? ");
    Scanner user = new Scanner(System.in);
    System.out.println(user.nextLine());


    if (user.nextLine() <=18){
    System.out.println("User is 18 or younger");
    }
    else if (user.nextLine() > 18 && age < 40){
    System.out.println("User is between 19 and 39");
    }

    else {
    System.out.println("User is older than 40");

    }
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: New in Java silly question about input

    Quote Originally Posted by hectorespresati View Post
    I'm trying to run this program but I can't something in the input is wrong and I don't know what.
    Please tell us what the program is supposed to be doing and what it is instead currently doing. The better you describe your problem usually the better we can help. Also please read my signature link below on how to use code tags so that your code is readable.

  3. #3
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: New in Java silly question about input

    I just want to put an input for the user so the program checks if the user is under 18 years old, between 18 or 40 or over 40.

    The program just gives me an error that something is not well typed and it doesn't work lol

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: New in Java silly question about input

    Quote Originally Posted by hectorespresati View Post
    I just want to put an input for the user so the program checks if the user is under 18 years old, between 18 or 40 or over 40.

    The program just gives me an error that something is not well typed and it doesn't work lol
    Again, please tell us all the necessary information. If you're seeing an error, it would help immensely if you show us the entire error message. We're terrible at reading minds you know.

  5. #5
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: New in Java silly question about input

    Sorry:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Duplicate local variable user

    at ifstatement.exerciseclass.main(exerciseclass.java: 19)

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: New in Java silly question about input

    You're trying to run code that doesn't compile -- never do this. Instead, first try to compile your class, and then report to us the compilation errors you see.

    Edit, the error looks to be telling us that you are declaring the *same* variable. user, *twice*, the second time occurring on line 19. You should only declare this variable once.

    edit: but I don't see this error in your code.

    I suggest: try as I suggest, compile your program and report all errors here. Re-print your code here, but please use code tags (again see my signature link below). Please make sure that your code is well formatted with decent indentation so we can read it well. Then indicate by comment which line is causing the errors your getting.
    Last edited by Fubarable; 02-05-2013 at 03:55 AM.

  7. #7
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: New in Java silly question about input

    I'm sorry but I don't have very clear what you mean.
    I tried to make some changes but I'm still doing something wrong and I don't know what :(

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: New in Java silly question about input

    Quote Originally Posted by hectorespresati View Post
    I'm sorry but I don't have very clear what you mean.
    I tried to make some changes but I'm still doing something wrong and I don't know what :(
    Please tell us, what part of my requests above do you not understand?

    Do you understand the difference between compiling and running a program? The .java code must first be compiled by the javac compiler to .class code, and then the class code is runnable. I'm asking you to compile your code and to show us any errors that the compiler is showing you. I'm asking you to post your latest code, the one that you've compiled most recently. I'm asking that you read my link in my signature links below on "how to use code tags" (or just click on the link here).

    Most importantly, if anything we say or request confuses you, please ask for clarification. But also the more specific your request the better we can help.

  9. #9
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default Re: New in Java silly question about input

    Hector, in an attempt to aid Fubarable in explaining to you why he is asking of you what he is asking is for a few reasons and I will attempt to explain them in a very simple way.

    If someone comes to you with a problem and only tells you that "it doesn't work", what would be your first reaction? First you must find out what the "it" is in that sentence. The next step you must take in that situation is understanding what it is supposed to do. Most of the time this can be done without doing more than simply looking at the object that is not working.

    Unfortunately for us all, programming is not much like this at all. Without understanding where the programmer is coming from and what his desired goal is we have almost no idea on where to start. While some of the people here are very good at determining this, it is not always possible.

    So what does this mean for you? This means you need to give us more information than we need, you want to give us more relevant information than you think we need. What is your input? What is your expected output? If you don't understand something, tell us what it is and how it confuses you. If its about an input from a scanner and you do not understand how a scanner input can be taken and used in your program you must tell us this.

    If you don't understand why your syntax error is a syntax error, you must point us to your syntax error. If you do not know where your syntax error is then you must give us your compiler error. The full block of red text most compilers spit out when something has gone terribly wrong.

    Today I ran into an issue that I was stumped on myself, my code compiled but I could not figure out why my code was acting up. So I dissected my code into focusing on the part where I was having an issue. My actual code is over 1,500 lines long currently and is no where near complete, but no one wants to sift through 1,500+ lines of code.

    I suggest you read this website about the Short, Self Contained, Correct Example structure. It explains a lot of about how to properly ask people for help. Then I have linked my post from earlier which uses this model and is very thorough in my description of the issue which allowed me to receive help very quickly from site members. After reading the SSCCE website it may help to compare the site to how my post looks to see it a little more practical. Building a simple gui using arrays and loops.

    It is okay to be new, we all have to start somewhere and everyone has walked in your shoes before. At one point in time each and every one of us had no idea what we were doing and had to learn by reading and then poking and prodding some code. Even posting a question online is a learning curve, and if you browse the forums you will find very many people do it wrong.

    My final advice to you is to use your words, articulate descriptions will often prove to be more useful than a block of code. I know I just wrote you a term paper here, and I hope you took the time to read it and were able to understand it fully.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. question on input in java?
    By MW130 in forum New To Java
    Replies: 9
    Last Post: 01-07-2013, 08:32 AM
  2. Silly boolean buttons
    By dd2308 in forum New To Java
    Replies: 8
    Last Post: 10-05-2012, 01:50 AM
  3. Silly strings
    By MhilleXD in forum Advanced Java
    Replies: 1
    Last Post: 10-02-2012, 11:32 AM
  4. Silly strings
    By MhilleXD in forum JCreator
    Replies: 1
    Last Post: 10-02-2012, 11:31 AM
  5. Replies: 5
    Last Post: 04-11-2012, 10:03 PM

Posting Permissions

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