Results 1 to 6 of 6
  1. #1
    Zeezj's Avatar
    Zeezj is offline Member
    Join Date
    Mar 2011
    Location
    Cambridge, MA
    Posts
    11
    Rep Power
    0

    Question how do i specify an integer input by the user? (been doing java for only 2 days)

    hello, my friends. i know this is a stupid question, but how do i specify an int input by the user? i need the user to specify the value of a variable, but i don't know how to do that...

    it's for this thing that rates your awesomeness (if you want to download it than go ahead lol)

    Java Code:
    public class AwesomeTest {
    	public static void main(String[] args) {
    
    		int Awesome;
    
    		System.out.println("on a scale of 1 to 10, how awesome are you?");
    		Awesome = //how to make user specify integer variable??;
    	
    		if ((Awesome < 6) && (Awesome > 1)) {
    			System.out.println("stop being so emo and get some self esteem. try for like, 6 or above, cause anything below five is just plain emo.");
    		}
    	
    		if ((Awesome > 6) && (Awesome < 11)) {
    			System.out.println("that's pretty damn awesome. seriously, you should, like, go for the world record of awesomeness...");
    		}
    
    		if (Awesome > 11) {
    			System.out.println("yeah, nice try buddy. nobody is that awesome. i said on a scale of one to TEN!");
    		}
    
    		if (Awesome < 1) {
    			System.out.println("dude, you need a therapist...");
    		}
    
    	}
    }
    halp?

    p.s. i made anther thread asking a different, stupider question about this same piece of code, so if that is somehow against the rules, than i'm sorry.
    from the desk of August Underhill-Proulx, CEO of Awesomecorp ltd.

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default

    Use a Scanner.

    Java Code:
    Scanner sc = new Scanner(System.in);
    int awesome = sc.nextInt();
    Note also the capitalisation of "awesome" - the compiler may not care if you capitalise it, but by convention all identifiers start with a lowercase letter.

    Also, what happens when awesome is 1, 6 or 11? Probably not what you intend to happen.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The other thread included the question about obtaining the input - and included a suggestion by sunde887 - so it might be more organised if all the discussion stays there.

  4. #4
    Zeezj's Avatar
    Zeezj is offline Member
    Join Date
    Mar 2011
    Location
    Cambridge, MA
    Posts
    11
    Rep Power
    0

    Default

    Quote Originally Posted by Iron Lion View Post
    Use a Scanner.

    Java Code:
    Scanner sc = new Scanner(System.in);
    int awesome = sc.nextInt();
    Note also the capitalisation of "awesome" - the compiler may not care if you capitalise it, but by convention all identifiers start with a lowercase letter.

    Also, what happens when awesome is 1, 6 or 11? Probably not what you intend to happen.
    when i try to run i get this:
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Cannot instantiate the type Scanner
    	The method nextInt() is undefined for the type Scanner
    
    	at AwesomeTest.main(AwesomeTest.java:10)
    from the desk of August Underhill-Proulx, CEO of Awesomecorp ltd.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    if(javaVersion < 1.5) {
        System.out.println("You cannot use Scanner."):
    } else {
        System.out.println("Use an import statement.");
    }
    By the way, whatever method you use if a user enters something that is not an integer then an Exception will be thrown which you will have to handle.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Since you are new I will give you this one. You need to import the scanner, it's not automatically included. Add this line to the top of your code.
    Java Code:
    import java.util.*;
    
    Or. 
    
    import java.util.Scanner;
    If you want to learn best I'd really suggest you get a book with good examples and exercises. Consider head first java or java software solutions(I have not read the latter but the exercises are good).

    Another popular suggestion is the sun java tutorials. These might be a bit hard for you to understand but give it a try.

Similar Threads

  1. Read user input into integer array
    By varunb in forum New To Java
    Replies: 12
    Last Post: 07-09-2010, 01:50 PM
  2. Replies: 13
    Last Post: 04-16-2010, 03:51 AM
  3. Replies: 6
    Last Post: 03-01-2010, 08:00 AM
  4. How do I validate user input from forms with Java?
    By rickywh in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 07:49 AM
  5. how to know the input value of integer
    By ran830421 in forum New To Java
    Replies: 15
    Last Post: 11-18-2009, 10:01 PM

Tags for this Thread

Posting Permissions

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