Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Issue with arrays

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Issue with arrays

    I have an assignment to read an input file made by me using one line for the students name and the next ten lines for quiz scores. I am supposed to use a bubble sort for the scores and then outprint the name and the top 8 quiz scores for each person. I can get it to work with just the first person, but then run in to one of two exception errors.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at QuizScores.main(QuizScores.java:77)

    or

    Exception in thread "main" java.lang.NumberFormatException: For input string: "Timothy Cretsinger"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at QuizScores.main(QuizScores.java:44)

    here is what the input looks like:

    Patrick Tilley
    67
    84
    89
    78
    91
    94
    88
    87
    88
    93
    Timothy Cretsinger
    84
    86
    85
    91
    87
    83
    81
    91
    92
    80

    and it goes on for 5 people. I understand the exception errors, but can't seem to figure out how to fix it. I know that when I go to read the next 11 lines for the array it can't convert the name to integer, and if I decrease the array to avoid the name, I get the out of bounds error.

    here is what my code looks like:
     
    import java.io.*;
     
    public class QuizScores
    {
    	public static void main(String args[])throws Exception
    	{
    		// Declare variables.
     
    		final String TITLE =  "\n\nRuss Penning Quiz Report\n\n";
    		final int SIZE = 11;		// Number of quizzes
    		int score[] = new int[SIZE];   // Array used to store 10 quiz scores.
    		String stuName;
    		int x;
    		final int LIMIT = SIZE;
    		String stuScoreString;
    		int stuScore = 0; 
    		int pairsToCompare;
    		boolean switchOccurred;
    		boolean done;
    		int temp;
     
    		// Input student's name
    		FileReader fr = new FileReader("scores.dat");
    		BufferedReader br = new BufferedReader(fr);
     
    		// This is the work done in the fillArray() method
    		if((stuName = br.readLine()) != null)
    		{
    				x = 0;
    				done = false;
    				while(x < LIMIT)   
    				{		
     
    					// Place value in array.
    					stuScoreString = br.readLine();
    					// Convert String to integer.
    					stuScore = Integer.parseInt(stuScoreString);		
    					score[x] = stuScore;
    					x++;    // Get ready for next input item.
     
    				}  // End of input loop.
    				stuScore = x - 1;
    				pairsToCompare = stuScore - 1;
     
    				// This is the work done in the sortArray() method
    				switchOccurred = true; // set flag to true
     
    				while(switchOccurred == true)
    				{
    					x = 0;
    					switchOccurred = false;
    						while(x < pairsToCompare)
    						{
    							if(score[x] > score[x + 1])
    							{
    								temp = score[x + 1];
    								score[x+1] = score[x];
    								score[x] = temp;
    								switchOccurred = true;
    							}
    							x++;	
    						}
    						pairsToCompare--;
    				}		
    				// This is the work done in the displayArray() method
    				x = 0;
    				System.out.println(stuName);
    				while(x < stuScore)
    				{
    					System.out.println(score[x + 2]);
    					x++;
    				}
    		}
    		else done = true;
     
    		br.close();
    		System.exit(0);
    	} // End of main() method.
    } // End of QuizScores class.

    If anyone has any idea what I am doing wrong, please let me know, and thank you for all your help.
    Last edited by rdpenning; October 23rd, 2011 at 09:29 AM.


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Issue with arrays

    Here's the fix:

     boolean switchOccurred;
    		boolean done;
     
    		final String TITLE =  "\n\nRuss Penning Quiz Report\n\n";
                    final int LIMIT = 10;  //MODIFIED
    		final int SIZE = 11;		// Number of quizzes
    		int score[] = new int[SIZE];   // Array used to store 10 quiz scores.   
     
    		int stuScore = 0; 
    		int pairsToCompare;                
    		int x;
    		int temp;
     
    		String stuName;
    		String stuScoreString;
     
    		// Input student's name
    		FileReader fr = new FileReader("scores.dat");
    		BufferedReader br = new BufferedReader(fr);
     
    		// This is the work done in the fillArray() method
    		if((stuName = br.readLine()) != null)
    		{
    				x = 0;
    				done = false;
    				while(x < LIMIT)   
    				{		
     
    					// Place value in array.
    					stuScoreString = br.readLine();
    					// Convert String to integer.
    					stuScore = Integer.parseInt(stuScoreString);		
    					score[x] = stuScore;
    					x++;    // Get ready for next input item.
     
    				}  // End of input loop.
    				stuScore = x - 2; //MODIFIED
    				pairsToCompare = stuScore - 1;
     
    				// This is the work done in the sortArray() method
    				switchOccurred = true; // set flag to true
     
    				while(switchOccurred == true)
    				{
    					x = 0;
    					switchOccurred = false;
    						while(x < pairsToCompare)
    						{
    							if(score[x] > score[x + 1])
    							{
    								temp = score[x + 1];
    								score[x+1] = score[x];
    								score[x] = temp;
    								switchOccurred = true;
    							}
    							x++;	
    						}
    						pairsToCompare--;
    				}		
    				// This is the work done in the displayArray() method
    				x = 0;
    				System.out.println(stuName);
    				while(x < stuScore)
    				{
    					System.out.println(score[x + 2]);
    					x++;
    				}                
    		}
    		else done = true;
     
    		br.close();
    		System.exit(0);

    I put a "//MODIFIED" in lines i modified in order to fix the code, it's just a problem of reading the names and trying to process them as numbers. Anyways, by making "LIMIT = SIZE" you're giving LIMIT a value of 11, and there's only 10 scores, so you're forcing your code to read the string "Timothy Cretsinger".

    Same goes for "stuScore", you're supposed to show the 8 highest scores, and you're giving it a value of 9 by inputing "stuScore = x - 1", so i changed it to "stuScore = x - 2", that should fix it.

    I'm not sure if your code is supposed to show only one student's 8 highest scores, but im guessing making a few more loops (depending on your student's quantity) to show the other students won't be such a problem.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issue with arrays

    Thank you so much! Works perfect now!

Similar Threads

  1. issue with arrays in Java
    By Sei783 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 9th, 2011, 01:19 AM
  2. Having an issue reading files to create two arrays.
    By ccrosby in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 26th, 2011, 06:47 AM
  3. Issue with setting up different class with arrays
    By D-COD3R in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 06:09 PM
  4. i do not know how to solve this issue
    By javastupi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2010, 08:28 PM
  5. url pattern issue - please help me...
    By bharathik in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: November 9th, 2009, 04:28 AM