Quick JAVA Question

Discussion in 'Mac Programming' started by sixstorm, Apr 17, 2007.

  1. sixstorm macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #1
    Hello all again. I know I only come around with questions but I'm seriously stuck. Our teacher gave us an easy program (to make) that reads a .dat file in this order:

    student number
    first name
    last name
    quiz total points
    program total points
    midterm total points
    final total points

    I have to read in each line into my program (using "/n" newline delimiter) to store information in arrays labeled as student number, student name and average (I have a function for that one). My question is how do you read in things from a file and how does it work?

    Here's my huge guess, feel free to laugh.

    Code:
    
    static Scanner inFile = new Scanner(new FileReader(C:\\student.dat"));
    
    for(int i=0; i < average.length(); i++)
    {
         studentNumber[i] = Int.parseInt();
         studentName[i] = String.parseString + " " + String.parseString;
    }
    
    Just a huge guess, but I'd like to ask for your help. Thanks in advance!
     
  2. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    Here's a method I use, which is one way to read a file. It sticks each line in a Vector. You would change this to parse the line of data per the specs on how the file is laid out.
    Code:
    	java.util.Vector<String> readFile(String fileName) { 
    		
    		java.util.Vector<String> data = new java.util.Vector<String>() ; 
    		try { 
    			java.io.FileReader myFile = new java.io.FileReader(dir + "/" + fileName) ; 
    			java.io.BufferedReader br = new java.io.BufferedReader(myFile) ; 
    
    			String s ; 
    			while ( (s = br.readLine()) != null ) {
    				data.add(s) ; 
    			}
    			myFile.close() ; 		
    		}
    		catch(Exception e) {
    			System.out.println("\n\n*** ERROR READING FILE ***") ;
    			System.out.println(e + "\n\n" ) ; 
    		}
    		return data ; 
    	} 
    	
    
    Do you not have a Java book? If not, try this: http://java.sun.com/docs/books/tutorial/essential/io/index.html

    Todd
     
  3. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #3
    Hmmm..... Thanks for the code and stuff, but I do have to stop and say that this program has to be done a specific way (using what code I've got listed above). My teacher gave us the Scanner-Filereader code and said that it was all that we needed. I just wanna know how to read each line in and how it works.

    Todd - Seriously thanks for the advice but I gotta have it done a certain way. I really appreciate it though. I do have a JAVA book but it doesn't go over input/output stuff in depth, nor does it go over the teacher-given code . . . arghh.
     
  4. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #4
    Ok, so I kinda figured this out. I think you are supposed to use InFile just like console like this:

    x = console.nextInt();
    y = console.nextDouble();

    However, now the problem is that I have to use the newline delimiter in here. So would it be:

    x = console.nextInt() + "/n";

    ?

    Or would you do a println("/n"); ?

    Hmmmm........ :D
     
  5. subl1me macrumors newbie

    Joined:
    Jan 26, 2005
    #5
    The Scanner class that your professor provided you with should probably come with a method that reads the next line into an object or something. You should probably turn whatever it read on that line into a String and then stick it in your array (of Strings).

    If you all you had to do is put that data in an array then you're pretty much done. If you have to mess around with the data, you'll probably have to parseInt the "numbered strings" into numbers.

    Good luck..have fun.
     
  6. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    Well then, perhaps you need this tutorial: http://java.sun.com/docs/books/tutorial/essential/io/scanning.html

    It shows the use of Scanner. It shows how to specify a character to use for delimitting input data, like, perhaps, \n. :)

    It does show the use of BufferedReader, and if you can't use that, then you can probably take that part out to use a character stream instead. (Not that it really matters, as Scanner will be doing it all for you)

    Todd
     
  7. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #7
    Thanks a ton guys, I've almost got this progam finished. Here's the code:

    Code:
    // Jonathan Andrew Scott
    // 4/16/07
    // Assignment 8
    // This program reads an input file with student's data and will
    // calculate averages, determine the highest average, display 
    // student number, name and grade average in columns and other things.
    
    
    import javax.swing.*;
    import java.io.*;
    import java.util.*;
    
    public class jscottpass81
    {
    	
    	static Scanner console = new Scanner(system.in);
    	static Scanner inFile = new Scanner(new FileReader("C:\\students.dat"));
    	
    	public static void main(String[] args)
    	{
    		int numStu, quizPosPoints, progPosPoints, midtermPoints, finalPoints, totalPosPoints;
    		string firstName, lastName;
    		
    		// Temp Variables
    		int quizPoints, progPoints, midPoints, finalPoints, average;
    		
    		System.out.println("Enter the total number of students: /n");
    		numStu = Int.parseInt();
    		
    		System.out.println("Enter the total possible Quiz Points: /n");
    		quizPosPoints = Int.parseInt();
    		
    		System.out.println("Enter the total possible Program Points: /n");
    		progPosPoints = Int.parseInt();
    		
    		System.out.println("Enter the total possible Midterm Points: /n");
    		midtermPoints = Int.parseInt();
    		
    		System.out.println("Enter the total possible Final Exam Point: /n");
    		finalPoints = Int.parseInt();
    		
    		totalPosPoints = quizPosPoints + progPosPoints + midtermPoints + finalPoints;
    		
    		// Arrays
    		int[] studentNumber = new int[numStu];
    		string[] studentName = new string[numStu];
    		int[] average = new int[numStu];
    		
    		// Read in information from file on disk
    		
    		for(int i=0; i < studentNumber.length(); i++)
    		{
    			studentNumber[i] = InFile.nextInt();
    			studentName[i] = InFile.nextString() + " /n" + InFile.nextString();
    			quizPoints = InFile.nextInt();
    			progPoints = InFile.nextInt();
    			midPoints = InFile.nextInt();
    			finalPoints = InFile.nextInt();
    			
    			// Calculation for Average
    			average[i] = calcAvg(quizPoints, progPoints, midtermPoints, finalPoints, totalPosPoints);
    			average += average[i];
    		}
    		
    		classAvg = average / numStu;
    						
    		// At end, print out arrays (in column format)
    		
    		printStats();
    		System.out.println("/n");		
    		
    		// Print out number of students and the average for the class
    		
    		System.out.println("Number of Students: " + numStu + "/n");
    		System.out.println("Average for the entire class: " + classAvg + "/n");
    		
    		// Print out the person with the highest average and the average itself
    		
    		int index = highestAvg();
    		System.out.println("The student with the highest average is " + studentName[index] + "/n");
    		System.out.println("His/her average is " + average[index] + "/n");
    		}
    }
    
    
    // Fuctions
    public static int calcAvg(int quizPoints, int progPoints, int midtermPoints, int finalPoints, int totalPoss)
    {
    	int average;
    	
    	average = (quizPoints + progPoints + midtermPoints + finalPoints) / (totalPoss * 100);
    	return average;
    }	
    
    public static int highestAvg()
    {
    	int index;
    	maxAvg = 0;
    	
    	// Finds the highest average
    	for(int i=0; i < average.length(); i++)
    	{
    		if(maxAvg < average[i])
    		{
    			index = [i];
    		}
    		
    		i++;
    	}
    	
    	return index;
    }		
    		
    
    public static void printStats()
    {
    	System.out.println("Student Number" + " " + "Student Name" + " " + "Grade Average /n");
    	for(int i=0; i < average.length(); i++)
    	{
    		System.out.println(studentNumber[i] + "     " + studentName[i] + "     " + average[i] + "/n");
    	}
    }	
    JGrasp doesn't wanna work too well sometimes and it's giving me some crappy compile errors. Going to try it in XCode and see what it says.
     
  8. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    Good for you!

    What happens when the number of students is zero?

    Todd
     
  9. sixstorm thread starter macrumors regular

    Joined:
    Jan 16, 2006
    Location:
    Nashville, TN
    #9
    XCode and JGrasp are being retarded and giving me error that are actually correct. Guess I need to reinstall them or something. I haven't gone through validation, it's the last thing I code for. :D
     
  10. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #10
    You should probably use javac (/Applications/Utilities/Terminal) on the command line to compile the code ;).
     

Share This Page