3 Replies - 2459 Views - Last Post: 10 April 2011 - 11:06 PM Rate Topic: -----

#1 Yahoosakie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 11

Help comparing two strings in two different .txt files.

Posted 10 April 2011 - 08:51 AM

I have 3 txt files.

One named cindy.txt
One named joe.txt
and one named key.txt

I'm trying to create a program that, asks how many files you have and then runs a loop getting the names of each txt file. So for instance if you were to say "2", it would prompt you for the entry of two file names.
You'd input "cindy.txt" then "joe.txt".
Then it would look at line 1 in cindy.txt and compare it to line 1 in key.txt
Then do the same for joe.txt.

The problem is when I run the program as it is, I keep getting the return value of 150 no matter what the input value. Since I have the line "grade = (correct / 20.0) * 100.0" it means it is storing 30 to correct. I have correct in the while loop to where if the values of each line are equal then it will add 1 to the "correct" variable.

My files are,
cindy.txt
1	D
2	D
3	D
4	D
5	D
6	D
7	B
8	C
9	D
10	B
11	C
12	A
13	D
14	C
15	A
16	B
17	A
18	C
19	A
20	B

joe.txt
1	A
2	C
3	A
4	A
5	D
6	B
7	B
8	C
9	D
10	B
11	C
12	A
13	D
14	C
15	A
16	B
17	A
18	C
19	A
20	B

key.txt
1	A
2	C
3	A
4	A
5	D
6	B
7	B
8	C
9	D
10	B
11	C
12	A
13	D
14	C
15	A
16	B
17	A
18	C
19	A
20	B


NameGrading.java
import java.io.*;
import java.util.Scanner;

/*****************************************************
Name: NameGrading,java
Purpose: To take txt files, view the files, and submit a grade summary.
@Author: 
@Version: 1.0
******************************************************/

public class hNameGrading
{
	public static void main(String[] args) throws IOException
	{
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Welcome to grading and analysis system. \nPlease have your student files"
		+ " ready before running this program.");
		System.out.print("How many students took the exam: ");
		int size = keyboard.nextInt();
		keyboard.nextLine();
		double[] grade = new double[size];
		String[] filename = new String[size];
		int student;
		double lowest= 0;
		double average = 0;
		double highest = 0;
		for (student = 0; student < size; student++)
			{
				System.out.println("Please enter student #"+ (student + 1) + " file name: ");
				filename[student] = keyboard.nextLine();
				grade[student] = grading(filename, size);
				System.out.println("blah: " + grade[student]);
      			System.out.println("Ok");
      			grade[student] = grading(filename, size);
			}
			System.out.println("blah: " + grade[student]);
			System.out.println("ha: " + grade[student]);
			highest = findHighest(grade, highest);
			lowest = findLowest(grade);
			average = findAverage(grade, size);



	}

/***************************************
Method grading uses Scanner to read answers from student files, compare
the answers with keys, and return a grade based on the answers given in
the file.
@param filename, a text file that stores student answers
@return student grade
****************************************/

public static double grading(String filename[], int size) throws IOException
	{
		double grade;
		double correct = 0.0;
		for (int count = 0; count < size; count++)
		{
			File file = new File(filename[count]);
			Scanner inputFile = new Scanner(file);
			File keyfile = new File("key.txt");
			Scanner keyFile = new Scanner(keyfile);
			while (inputFile.hasNext())
			{
				String answer = inputFile.nextLine();
				String key    = keyFile.nextLine();
				if (answer.equals(key))
				{
					correct = correct + 1;
				}
			}
			inputFile.close();
			keyFile.close();
		}
		grade = ( correct / 20.0) * 100.0;

		return grade;

	}
/*********************************
Method findHighest finds the highest value in an array named grades and
returns that value.
@param grades an array that stores student grades
@return the highest value in the array grades
*********************************/
public static double findHighest(double grades[], double max)
	{

		for (int count = 0; count < 100; count++)
		{
			if (grades[count] > max)
			{
				max = grades[count];
			}
		}
		return max;
	}
/*********************************
Method findLowest find the lowest value in an array named grades and
returns that value.
@param grades an array that stores student grades
@return the lowest value in the array grades
*********************************/
public static double findLowest(double[] grades)
	{
		double low = grades[0];
				for (int count = 0; count < 20; count++)
				{
					if (grades[count] < low)
					{
						low = grades[count];
					}
				}
		return low;
	}
/********************************
Method findAverage finds the average value in the array grades and
returns that value.
@param grade an array that stores student grades
@return the average value in the array grades
********************************/
public static double findAverage(double[] grades, int size)
	{
		double sum = 0;
		for (int count = 0; count < size; count++)
		{
			sum = sum + grades[count];
		}
		double average = (sum/size);

		return average;
	}
}




Thank you soo much in advance, this is my first semester of programming and we just started on how to use arrays, so my mistake may lay in that.

This post has been edited by Yahoosakie: 10 April 2011 - 08:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help comparing two strings in two different .txt files.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4438
  • View blog
  • Posts: 12,308
  • Joined: 18-April 07

Re: Help comparing two strings in two different .txt files.

Posted 10 April 2011 - 10:30 AM

Ok you ask for the number of students in the class. So lets say you enter 30 and this is put into "size" in your main method. You then call grading using this value. So you are passing 30 to grading() where you then go into a for loop. Why are you looping here 30 times? You want to only be looping through the file once for each call.

Your grading() method doesn't even need the size right? It just needs to know the filename to compare to the key. One time you are calling grading("cindy.txt") and another time you are calling grading("joe.txt"). The loop will then loop through cindy.txt or joe.txt and compare it to the key (not looping through size). It then returns a single grade for that student. Back in main() you would be looping 30 times making 30 calls to grading(), one for each student.

Hope you are getting my point there. grading() loops through just the file, comparing each answer, tallying up the correct value, and calculating the grade for that single student. Get rid of the for loop in your grading() function, just loop through the file.

:)

This post has been edited by Martyr2: 10 April 2011 - 10:31 AM

Was This Post Helpful? 2
  • +
  • -

#3 Yahoosakie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 11

Re: Help comparing two strings in two different .txt files.

Posted 10 April 2011 - 11:42 AM

Thank you so much, you did a great job explaining. I was able to figure it out and now I got it working. I'm not sure why i passed that size variable through, I guess it was from an earlier thought before I changed the method some.

Thank you. :)
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10820
  • View blog
  • Posts: 40,339
  • Joined: 27-December 08

Re: Help comparing two strings in two different .txt files.

Posted 10 April 2011 - 11:06 PM

Also, something you might consider in the future is to design a class rather than use parallel arrays. It adheres to Java and industry standards much more than parallel arrays. You might want to check out my tutorial Moving Away From Parallel Arrays for more information. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1