Results 1 to 12 of 12
  1. #1
    ZeoVioles is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Exception in thread "main" java.util.NoSuchElementException Help Please

    I don't want help with the entire code, just whats causing the problem in the output.

    Here is the code:


    Java Code:
    import java.util.Scanner;
    import java.io.*;
    public class Assign2Main
    {
        // Code the following method
        // sort the array passed to it with the smallest number first
        // code the sort as a selection sort.
        public static void selectionSort(int[] a1){
    
        }
     //   public static void printArray(int[] intArray)
        //{
       //     for(int j = 9; j >= 0; j--)
         //		 System.out.print(intArray[j] + ", ");
      //  }
        // Code a method named printArray that will be used to display any integer array
        // with each number separated by a comma.
    
    
        public static void main(String[] args) throws IOException{
          Scanner kb = new Scanner(System.in);
        // Prompt for and Read in an integer number between 1 and 10
          System.out.print("Please enter an integer between 1 and 10. ");
          int num = kb.nextInt();
        // If the number entered is not in the 1 – 10 range, continue to prompt
        // until the number is in the range.
          while (num < 1 || num > 10){
            System.out.print("Please enter an integer between 1 and 10. ");
            num = kb.nextInt();
          }
          
    
        // Use a switch statement to display the number as text. If 1 is entered
        // One should be output.
          switch (num){
          case 1:
        	  System.out.println("One");
        	  break;
          case 2:
        	  System.out.println("Two");
        	  break;
          case 3:
        	  System.out.println("Three");
        	  break;
          case 4:
        	  System.out.println("Four");
        	  break;	  
          case 5:
        	  System.out.println("Five");
        	  break;
          case 6:
        	  System.out.println("Six");
        	  break;
          case 7:
        	  System.out.println("Seven");
        	  break;
          case 8:
        	  System.out.println("Eight");
        	  break;
          case 9:
        	  System.out.println("Nine");
        	  break;
          case 10:
        	  System.out.println("Ten");
        	  break;
          }
          
        // Instantiate a Scanner object that will read its input from an input file
        // named inputFile.txt. Download inputFile.txt from Niihka
          File input = new File( "inputFile.txt" );
          Scanner scan = new Scanner(input);
          
        // Declare a 10 element integer array
          int [] intArray = new int [10] ;
    
        // Read in the 10 integer values that are stored in inputFile.txt
        // Display all the numbers in the array, the last number first and
        // each number separated using a comma.
          int i = 0;
          while (scan.hasNextInt()){
        	  intArray[i] = scan.nextInt();
        	  System.out.print(intArray[i] + ", ");
        	  i++;
          }
    
    
        // Display the largest and smallest numbers in the array
          int max = 0;
          int min = 100;
          
          for (i = 0; i <= intArray.length; i++){
        	  if (max < intArray[i])
        		  max = intArray[i];
        	  if (min > intArray[i])
        		  min = intArray[i];
          }
          System.out.println("Maximum is: " + max + " Minimum is: " + min);
        // Display the numbers in the array in sorted order using the selctionSort
        // method coded above. Display in the same format as above. Each number 
        // is separated with a comma
     
        // Create a second array that has 100 integer numbers. The first 10 numbers
        // in this array will be the same 10 as in the first array, the next 10
        // numbers will be 2 times the numbers in the first array, the next 10
        // 3 times the numbers, the next 10, 4 times the numbers until the 
        // last ten will be 9 times the numbers
    
        // Display the second array. Each number is separated with a comma.
    
        // Create a third array that is a 2 dimensional 10 by 10 array.
        // The numbers in the first row will be the same 10 as in the first array. 
        // The numbers in the second row will be 2 times the numbers in the first array.
        // The numbers in the third row will be 3 times the numbers in the first array.  
        // The numbers in the fourth row will be 4 times the numbers in the first array.  
        // The numbers in the last row will be 10 times the numbers in the first array.
    
    
        // Display the third array so each row is displayed on a line by itself.
        // Use the printArray method you previously coded.
    
    
        }
    
    	//private static void (boolean b) {
    		// TODO Auto-generated method stub
    		
    	//}
    }
    Here is the output:

    Please enter an integer between 1 and 10. 5
    Five
    10, 10, 10, 10, 10, 10, 10, 10, 10, 10, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Assign2Main.main(Assign2Main.java:103)
    Last edited by ZeoVioles; 09-04-2012 at 03:51 AM. Reason: Exception solved now new problem.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    On line 86 the code calls nextInt() when there is no input available. You should call one of the Scanner class's methods (names start with has) to check if there is input available before calling the nextInt() method.

    Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ZeoVioles is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    Quote Originally Posted by Norm View Post
    On line 86 the code calls nextInt() when there is no input available. You should call one of the Scanner class's methods (names start with has) to check if there is input available before calling the nextInt() method.

    Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum

    Do you mean 'scan.hasNext()' if not, I don't know what your talking about...
    ,

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    There are several different has... methods. Have you tried using any of them to keep from trying to read past available input.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ZeoVioles is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    Quote Originally Posted by Norm View Post
    There are several different has... methods. Have you tried using any of them to keep from trying to read past available input.
    I'm sorry, again do you mean hasNext...() or just has...()?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    do you mean hasNext...() or just has...()?
    Those don't look like valid method names. You need to read the API doc to get valid method names.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    ZeoVioles is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    Quote Originally Posted by Norm View Post
    Those don't look like valid method names. You need to read the API doc to get valid method names.
    Sorry, I meant scan.hasNextInt()...

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    Have you tried using that method? The loop should end when there is no more input or when the array is full.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    ZeoVioles is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    Quote Originally Posted by Norm View Post
    Have you tried using that method? The loop should end when there is no more input or when the array is full.
    Hold on let me edit my post. because I got another error.

  10. #10
    ZeoVioles is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    Quote Originally Posted by Norm View Post
    Have you tried using that method? The loop should end when there is no more input or when the array is full.
    I changed the original post, its given me a new error.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Assign2Main.main(Assign2Main.java:103)
    At line 103 the code used an index with the value of 10 in an array that has less than 11 elements.
    Remember array indexes go from 0 to the length-1. The indexes for a 10 element array are 0 through 9

    Make sure the index does not go past the end of the array.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    ZeoVioles is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default Re: Exception in thread "main" java.util.NoSuchElementException Help Please

    Quote Originally Posted by Norm View Post
    At line 103 the code used an index with the value of 10 in an array that has less than 11 elements.
    Remember array indexes go from 0 to the length-1. The indexes for a 10 element array are 0 through 9

    Make sure the index does not go past the end of the array.
    Ok I found the problem, Thanks.

Similar Threads

  1. Replies: 11
    Last Post: 02-11-2011, 12:11 AM
  2. Replies: 4
    Last Post: 05-07-2010, 07:58 PM
  3. Exception in thread "main" java.util.NoSuchElementException
    By vileoxidation in forum New To Java
    Replies: 5
    Last Post: 09-17-2008, 07:29 AM
  4. Replies: 4
    Last Post: 06-08-2008, 02:19 PM
  5. Replies: 5
    Last Post: 05-14-2008, 01:43 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
  •