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 5 of 5

Thread: String array problem

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default String array problem

    This is what my program looks like
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;
     
     
    public class mypa9class {
     
    	/**
    	 * @param args
    	 * @throws IOException 
    	 */
    	public static void main(String[] args) throws IOException {
     
    		// get all files and input in arrays
     
    		// name array
    		int max = 9;
    		File last = new File("Lname.txt");
    		Scanner sclast = new Scanner(last);
    		File first = new File("Fname.txt");
    		Scanner scfirst = new Scanner(first);
    		String[] namearray = new String[max];
    		String firstname;
    		String lastname;
     
    		for (int i = 0; i < 10; i++)
    		{
    		firstname = scfirst.next();
    		lastname = sclast.next();
    		namearray[i] = firstname + " " + lastname;
    		}
     
    		// exam 1
    		double average1 = 0;
    		double max1 = 0;
    		double min1 = 0;
    		File exam1 = new File("Exam1.txt");
    		Scanner scexam1 = new Scanner(exam1);
    		double[] exarray1 = new double[max];
    		int count = 0;
    		while (scexam1.hasNext())
    				{
    			exarray1[count] = scexam1.nextDouble();
    			count++;
    				}
    		average1 = getAvg(exarray1);		
    //		System.out.println("average1 " + average1);
     
    		max1 = getMax(exarray1);
    //		System.out.println("max1 " + max1);
     
    		min1 = getMin(exarray1);
    //		System.out.println("min1 " + min1);
     
    		// exam 2
    		double average2 = 0;
    		double max2 = 0;
    		double min2 = 0;
    		File exam2 = new File("Exam2.txt");
    		Scanner scexam2 = new Scanner(exam2);
    		double[] exarray2 = new double[max];
    		count = 0;
    		while (scexam2.hasNext())
    				{
    			exarray2[count] = scexam2.nextDouble();
    			count++;
    				}
    		average2 = getAvg(exarray2);
    //		System.out.println("average2 " +average2);
    		max2 = getMax(exarray2);
    //		System.out.println("max2 " + max2);
    		min2 = getMin(exarray2);
    //		System.out.println("min2 " + min2);
     
    		// exam 3
    		double average3 = 0;
    		double max3 = 0;
    		double min3 = 0;
    		File exam3 = new File("Exam3.txt");
    		Scanner scexam3 = new Scanner(exam3);
    		double[] exarray3 = new double[max];
    		count = 0;
    		while (scexam3.hasNext())
    				{
    			exarray3[count] = scexam3.nextDouble();
    			count++;
    				}
    		average3 = getAvg(exarray3);
    //		System.out.println("average3 " + average3);
    		max3 = getMax(exarray3);
    //		System.out.println("max3 " +max3);
    		min3 = getMin(exarray3);
    //		System.out.println("min3 " + min3);
     
    		// exam 4
    		double average4 = 0;
    		double max4 = 0;
    		double min4 = 0;
    		File exam4 = new File("Exam4.txt");
    		Scanner scexam4 = new Scanner(exam4);
    		double[] exarray4 = new double[max];
    		count = 0;
    		while (scexam4.hasNext())
    				{
    			exarray4[count] = scexam4.nextDouble();
    			count++;
    				}
    		average4 = getAvg(exarray4);
    //		System.out.println("average4 " + average4);
    		max4 = getMax(exarray4);
    //		System.out.println("max4 " + max4);
    		min4 = getMin(exarray4);
    //		System.out.println("min4 " + min4);
     
     
    //	System.out.println(namearray[9]);
    //	System.out.println(exarray4[9]);
     
    	// calculate average for students and input in a new array called exAvg
    	double[] exAvg = new double[max];
    	for (int i = 0; i < 10; i++)
    	{
    	exAvg[i] = computeAverage(exarray1[i], exarray2[i], exarray3[i], exarray4[i])	;
     
    	}
     
    	// calculate grade for each student and input it in a new array titled grade
    	char[] grade = new char[max];
    	for (int i = 0; i < 10; i++)
    	{
    		grade[i] = computeGrade(exAvg[i]);
    	}
     
     
    	//output area + printwriter
    	PrintWriter prw = new PrintWriter("PAOutFile.txt");
     
    	System.out.println("Full Name of Student \t\t Ex1 \t Ex2 \t Ex3 \t Ex4 \t Avg \t Grade");
    	prw.println("Full Name of Student \t\t Ex1 \t Ex2 \t Ex3 \t Ex4 \t Avg \t Grade");
    	for (int i = 0; i < 10; i++)
    	{
    		System.out.println(namearray[i] + "\t\t\t" + exarray1[i] + "\t" + exarray2[i] + "\t" + exarray3[i] + "\t" + exarray4[i] + "\t" + exAvg[i] + "\t" + grade[i]);
    		prw.println(namearray[i] + "\t\t\t\t\t" + exarray1[i] + "\t" + exarray2[i] + "\t" + exarray3[i] + "\t" + exarray4[i] + "\t" + exAvg[i] + "\t" + grade[i]);
    	}
    	System.out.println();
    	System.out.println("Exam Number \t\t\t Max Score \t Min Score \t Average Score");
    	System.out.println("Exam 1 \t\t\t\t " + max1 + "\t\t" + min1 + "\t\t\t" + average1);
    	System.out.println("Exam 2 \t\t\t\t " + max2 + "\t\t" + min2 + "\t\t\t" + average2);
    	System.out.println("Exam 3 \t\t\t\t " + max3 + "\t\t" + min3 + "\t\t\t" + average3);
    	System.out.println("Exam 4 \t\t\t\t " + max4 + "\t\t" + min4 + "\t\t\t" + average4);
    	prw.println("Exam Number \t\t\t Max Score \t Min Score \t Average Score");
    	prw.println("Exam 1 \t\t\t\t " + max1 + "\t\t" + min1 + "\t\t\t" + average1);
    	prw.println("Exam 2 \t\t\t\t " + max2 + "\t\t" + min2 + "\t\t\t" + average2);
    	prw.println("Exam 3 \t\t\t\t " + max3 + "\t\t" + min3 + "\t\t\t" + average3);
    	prw.println("Exam 4 \t\t\t\t " + max4 + "\t\t" + min4 + "\t\t\t" + average4);
     
    	prw.close();
    	}
    	public static double getAvg(double[] exam)
    	{
    		double average = 0;
    		double total = 0;
    		//average exam
    		for (int i = 0; i < 10; i++)
    		{
    			total = total + exam[i];
     
    		}
    		average = total/10;
     
    		return(average);
    	}
     
    	public static double getMax(double[] exam)
    	{
    		double max = 0;
    		for (int i = 0; i < 10; i++)
    		{
    			if (max < exam[i])
    			{
    				max = exam[i];
    			}
    		}
     
     
    		return(max);
    	}
     
    	public static double getMin(double[] exam)
    	{
    		double min = 999;
    		for (int i = 0; i <10; i ++)
    		{
    			if (min > exam[i])
    			{
    				min = exam[i];
    			}
    		}
     
    		return(min);
     
    	}
     
    	public static double computeAverage(double exam1, double exam2, double exam3, double exam4)
    	{
    		double studavg = 0;
    		studavg = (exam1 + exam2 + exam3 + exam4) / 4;
     
    		return(studavg);
    	}
     
    	public static char computeGrade(double avgrade)
    	{
    		char grade = 'E';
    		if (avgrade >= 90)
    		{
    			grade = 'A';
    		}
    		if (avgrade < 90 && avgrade >= 80)
    		{
    			grade = 'B';
    		}
    		if (avgrade < 80 && avgrade >= 70)
    		{
    			grade = 'C';
    		}
    		if (avgrade < 70 && avgrade >= 60)
    		{
    			grade = 'D';
    		}
    		if (avgrade <60)
    		{
    			grade = 'E';
    		}
     
    		return (grade);
    	}
    	}
    Error Code
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
    	at mypa9class.main(mypa9class.java:32)

    There is much more code, but i am just wondering why this gives me an error, when i switch the length of the array to 10 instead of 9 i have no problems. The scanners bring in names from two different text files both with have 10 names each. When making the array i figured 0,1,2, -> 9 would give me the 10 spots i need i don't understand why i need it a 10 which would in turn leave namearry[10] to have nothing in it?
    Let me know if i didn't explain my question good enough.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: String array problem

    namearry[10] is not "nothing in it" it is "does not exist"

    0,1,2, .... 9 is 10 locations

    In the loop it says <9. So lets count. 0, 1, 2, 3, 4, 5, 6, 7, 8, ...stop. 9 is not <9 so the loop body is skipped after 8 cycles.
    When you say less than 10, you get the 9th cycle because 9 is <10.



    why this gives me an error,
    Always post the full text of the error messages with your code and question.
    Error messages have this bad habit of telling what the problem is. Without that message, it can take much longer to determine what the problem is.
    Last edited by jps; December 4th, 2012 at 05:13 AM. Reason: corrected error

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String array problem

    I edited to add my full code, i know where the error occurs its because i changed max = 9 instead of 10, i just can't figure out logically why a 9 will not work. In my for loop its actually <10 which means it will run 10 times including 0 so i don't know why i need an array of [10] which is 11 locations.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: String array problem

    Quote Originally Posted by hwhelp View Post
    .... an array of [10] which is 11 locations.
    myArray[10] gives:
    myArray[0], myArray[1], myArray[2], myArray[3], myArray[4], myArray[5], myArray[6], myArray[7], myArray[8], myArray[9]
    which comes to 10 elements, not 11

    Also note that I had errors in my previous post which may have been misleading, and the errors have been corrected.

    --- Update ---

    Quote Originally Posted by hwhelp View Post
    .... an array of [10] which is 11 locations.
    myArray[10] gives:
    myArray[0], myArray[1], myArray[2], myArray[3], myArray[4], myArray[5], myArray[6], myArray[7], myArray[8], myArray[9]
    which comes to 10 elements, not 11

    Also note that I had errors in my previous post which may have been misleading, and the errors have been corrected.

  5. The Following User Says Thank You to jps For This Useful Post:

    hwhelp (December 4th, 2012)

  6. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: String array problem

    Thats where i was confused, thank you so much !!

Similar Threads

  1. Testing if an inputted String exists in an String Array
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2012, 03:46 PM
  2. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 05:50 PM
  3. Replies: 3
    Last Post: November 20th, 2011, 02:33 PM
  4. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  5. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM