Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java problem

  • 30-11-2004 06: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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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