Results 1 to 7 of 7
  1. #1
    new_1 is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default cant take input from user

    hi,
    my application was successfully compiled, but when i tried to run it, kept running..
    this happens at the step when im taking input from user.
    i use a scanner object to take input from user.
    i imported all java.util classes.
    What could be the problem???
    plz help ASAP.
    thnx.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Let's see what you did.

  3. #3
    new_1 is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default take input from user

    well, the code was long, but even the example below didnt work.
    Java Code:
    public class Main {
        public static void main(String[] args) 
        {
           Scanner input = new Scanner( System.in );
           int i=input.nextInt();
        }
    
    }
    and of course class java.util.Scanner is imported above.
    ill appreciate immediate help.
    thanks.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Yes, using nextInt with the System.in will cause the jvm to move along after the first use (of something like nextInt) without blocking for user input. Try nextLine instead.
    Java Code:
    import java.util.*;
    
    public class Test {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("enter an int");
            int n = Integer.parseInt(scanner.nextLine());
            System.out.println("you entered " + n);
            System.out.println("enter a String");
            String line = scanner.nextLine();
            System.out.println("you entered " + line);
        }
    }
    When you have trouble you can always throw a small test app together to explore things.

  5. #5
    new_1 is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default

    hi,
    ive copied the code you provided above and tried to run it,
    same problem arises!
    thanks.

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by new_1 View Post
    hi,
    ive copied the code you provided above and tried to run it,
    same problem arises!
    thanks.
    Are you sure you entered it correctly? I copied and ran his code and it worked just fine for me.

  7. #7
    new_1 is offline Member
    Join Date
    Dec 2007
    Posts
    5
    Rep Power
    0

    Default

    Thanks, it worked now. I just wasnt familiar with new IDE i had.

Similar Threads

  1. How to get the User Name
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-04-2008, 03:43 PM
  2. Prompting user input of a string.
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-09-2008, 07:23 PM
  3. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 08:03 AM
  4. New User
    By TKI5 in forum New To Java
    Replies: 0
    Last Post: 12-13-2007, 02:13 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 09:46 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
  •