Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java problem

Options
  • 30-11-2004 6:13pm
    #1
    Closed Accounts Posts: 4


    Getting an error reading in number from the output in netbeans. The code looks like
    import java.util.Scanner;
    import static java.lang.System.out;
    class take_in {
        
        
        public take_in() {
        }
        
        
        public static void main(String[] args) {
            Scanner myscanner = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int input = myscanner.nextInt();
            
        }
        
    }
    

    and the errors are:

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:817)
    at java.util.Scanner.next(Scanner.java:1431)
    at java.util.Scanner.nextInt(Scanner.java:2040)
    at java.util.Scanner.nextInt(Scanner.java:2000)
    at take_in.main(take_in.java:13)

    Help would be greatly appreciated


Comments

  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    you get the error straight away? Or after you type something in? Have you tried typing something in like "1 2 3 " or "1" or "1 ". What do they do?

    Or does system.in require piping? (although the API doc says it will block for input).


  • Closed Accounts Posts: 4 telomar


    doesn't allow you to type something in. problem seems to occur at the instantiation of the input:

    int input = myscanner.nextInt();

    it runs i.e. when you run it, it prints that "Enter... " to the screen but wont let you enter anything. if you try sticking in a system.out statement after that int input... it wont print to the screen. Well stumped here.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Try inserting an BufferedReader in place of directly hooking into System.in.


  • Closed Accounts Posts: 4 telomar


    This code here uses BufferedReader and has it working:
    [code]import java.io.* ;



    class Tut1{

    public static void main(String args[])

    {

    InputStreamReader istream = new InputStreamReader(System.in) ;

    BufferedReader bufRead = new BufferedReader(istream) ;



    System.out.println("Welcome To My First Java Program");



    try {

    System.out.println("Please Enter In Your First Name: ");

    String firstName = bufRead.readLine();

    System.out.println("Your Name is: " + firstName);

    }

    catch (IOException err) {

    System.out.println("Error reading line");

    }

    }

    }

    [\code]

    Just can't understand why that Scanner file ain't working. Could it be a problem in the written code?


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    It seems to me that using just System.in doesn't break for input like its supposed to, but I always found it better to use a BufferedReader just to be on the safe side.


  • Advertisement
  • Closed Accounts Posts: 4 telomar


    Yeah its annoying me here... Gonna just leave it. Cheers for the help.


  • Closed Accounts Posts: 92 ✭✭tempest


    The problem is that scanner.nextInt() is being called before anything has been entered on standard input. The contract of the method clearly states that a NoSuchElementException will be raised if the input is exhausted. i.e. a token cannot be retrieved.

    try this.
        public static void main(String[] args) {
            Scanner myscanner = new Scanner(System.in);
            System.out.print("Enter a number: ");
            while (true) {
              if (myscanner.hasNextInt())  {
                int input = myscanner.nextInt();
                System.out.println("You entered: " + input);
              }
            }
            
        }
    
    


  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    Is there by any chance a way of putting "if statements" within "for loops" for java?


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    for(int i = X; i <= Y; i++)
    {
       if(true)
       {
       }
    }
    


  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    Thank you! :) but it goes a little something like this but i dont think its working properly.
    String employee= "Caterina";
    double rate= 10.25;
    int hoursPerWk= Console.readInt("Enter the Hours Per Week: ");
    double salary= rate * hoursPerWk;

    for(hoursPerWk=0; hoursPerWk>0; hoursPerWk++)
    {
    System.out.println(hoursPerWk);

    if(hoursPerWk>40)
    {
    System.out.println("Time and a Half\t" + salary);
    }
    }


  • Advertisement
  • Closed Accounts Posts: 92 ✭✭tempest


    String employee= "Caterina";
    double rate= 10.25;
    
    /* get the hours per week */
    int hoursPerWk= Console.readInt("Enter the Hours Per Week: ");
    double salary= rate * hoursPerWk;
    
    /* 
        Ignore hours per week. Reset the value to 0.
        start off a loop that will never execute as the condition
        will never be met.
    */
    for(hoursPerWk=0; hoursPerWk>0; hoursPerWk++)
    {
      System.out.println(hoursPerWk);
    
      if(hoursPerWk>40)
      {
        System.out.println("Time and a Half\t" + salary);
      }
    }
    

    And in English:
    Basically you are reading in a value and then resetting it in the for loop (hoursPerWk = 0).
    Solution:
    Use a while loop.
    Lesson:
    Always consider what the most appropriate looping structure is for what you want to achieve.

    hoursPerWk=0; hoursPerWk>0;
    hoursPerWk cannot be both equal to 0 and greater than 0 at the same time. therefore the loop can never execute....

    Also you should probably have started a separate thread..


  • Registered Users Posts: 307 ✭✭Thordon


    public take_in() {
    }
    Calling a class take_in makes me sad :(, humpback style is preffered over underscores since java is case sensetive and classes are supposed to start with a capital letter (String for example).


  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    Thanks for the help! ;) I'm still only a rookie at Java but i'll try my best to help yee solve your problems too. :)


Advertisement