0

This program is meant to use only if statements to take in three numbers as user input, and then print them to the screen in ascending order. However, when I attempt to run this code, I receive the following ouput:

Enter first number: Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at OrderNumbers.main(OrderNumbers.java:10)

This is the code that I am currently running:

import java.util.*;

public class OrderNumbers{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);

      System.out.print("Ordering 3 whole numbers \n");

      System.out.print("Enter first number: ");
      int numberOne = scan.nextInt();
      System.out.print("Enter second number: ");
      int numberTwo = scan.nextInt();
      System.out.print("Enter third number: ");
      int numberThree = scan.nextInt();

      if( numberOne > numberTwo && numberTwo > numberThree){
         System.out.println( "Ordered numbers are: " + numberThree + ", " + numberTwo + ", " + numberOne);
      }
      else if( numberOne > numberTwo && numberThree > numberTwo) {
         System.out.println( "Ordered numbers are: " + numberTwo + ", " + numberThree + ", " + numberOne);
      }
      else if( numberTwo > numberOne && numberOne > numberThree) {
         System.out.println( "Ordered numbers are: " + numberThree + ", " + numberOne + ", " + numberTwo);
      }
      else if( numberTwo > numberOne && numberOne < numberThree) {
         System.out.println( "Ordered numbers are: " + numberOne + ", " + numberThree + ", " + numberTwo);
      }
      else if( numberThree > numberOne && numberOne < numberTwo) {
         System.out.println( "Ordered numbers are: " + numberOne + ", " + numberTwo + ", " + numberThree);
      }
      else if( numberThree > numberOne && numberOne > numberTwo){
         System.out.println( "Ordered numbers are: " + numberTwo + ", " + numberOne + ", " + numberThree);
      }
   }
}

I do not know where the error lies, but any help would be appreciated.

ntalbs
  • 28,700
  • 8
  • 66
  • 83
dff
  • 311
  • 2
  • 17
  • 4
    How are you running the code, and did you enter any numbers? – Jon Skeet Sep 08 '15 at 06:19
  • 1
    The error is in line 10 of your code, as it says here: at OrderNumbers.main(OrderNumbers.java:10) – Stultuske Sep 08 '15 at 06:19
  • 2
    Code is running fine. but there is issue with ordering of the output. – YoungHobbit Sep 08 '15 at 06:20
  • 1
    `Ordering 3 whole numbers Enter first number: 5 Enter second number: 4 Enter third number: 6 Ordered numbers are: 4, 6, 5` This is the result i got . What numbers did you give as input ? – Kibadachi Sep 08 '15 at 06:22
  • 4
    http://stackoverflow.com/questions/13729294/nosuchelementexception-with-java-util-scanner. Plus solve your problem with array and sort – qwr Sep 08 '15 at 06:33
  • Obviously your if-else conditions are wrong. You want logic for ordering three numbers? – Dakshinamurthy Karra Sep 08 '15 at 06:35
  • @Kibadachi `print them to the screen in ascending order` meaning of this to sort the input number in increasing order. I tired `1, 4, 7` and got output as `1, 7, 4` the expected output should be `1, 4, 7`. Is my understanding correct. – YoungHobbit Sep 08 '15 at 06:36
  • Code is fine, only problem is with ordering, plz specify what are you giving as input. – RishiKesh Pathak Sep 08 '15 at 06:42
  • I don't have any exception with your code. However, your logic in "numberTwo > numberOne && numberOne < numberThree" is not really correct. For example: first = 3, seconde = 5, third = 4. It will print: 3,5,4. :) Is that the one you want?! or you would like to print: 3,4,5?! – Kenny Tai Huynh Sep 08 '15 at 06:46

1 Answers1

0

The exception might be thrown because you're not entering a number. You should check if your input is a number with input.hasNextInt() and print error if it's not the case with input.next()(which returns a String ) to see what you're taking from input.

Asoub
  • 2,273
  • 1
  • 20
  • 33