student record program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: student record program

  1. #1
    Join Date
    Jan 2006
    Posts
    10

    Question student record program

    Hello, I'm supposed to write a program that reads in a text file called "Lab17a.dat". The file is organized with a separate character string on each line. This is a sequential file of characters, but the file is organized to store student records with three fields in each record. There is a name field, age field and a gpa field. Keep in mind that Java does not realize these are records. You, as programmer, need to convert the age string to an integer and the gpa string to a double.
    This is what I have so far:
    Code:
    import java.io.*;
    
    public class lab17a
    {
    	public static void main(String args[]) throws IOException
    	{
    		int count=1; 
    		int age; double gpa;
    		String name;
    		File inFile = new File(args[0]);
    		if (inFile.exists())
    		{
    			BufferedReader inStream = 
    			new BufferedReader(new FileReader(inFile));				
    			String inString;									
    			while((inString = inStream.readLine()) != null)	
    			{
    			if(count==3)	
    				{
    					gpa=Double.parseDouble(inString);	//converts gpa
    					System.out.println("gpa"+ gpa);
    				}
    				
    			if(count==1)
    			{
    				name=inString;
    				System.out.println( name); //gets the name
    				
    			}
    			
    			
    		if(count==2)
    			{
    				age=Integer.parseInt(inString);	//converts age
    				System.out.println("age" + age);
    			}
    				 
    				} 
    			  			
    			inStream.close();    
    		}
    		else
    		{
    			System.out.println(inFile + " does not exist"); //if file does not exist
    			
    		}
    		System.out.println();
    	}
    }
    I was wondering if somebody could tell me if I'm on the right track or not. Thank you!
    Absit invidia

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    I would recommend using the Scanner class for this. Here's a tutorial on how to use it (look at the "Reading From a File" part).

    There are a lot of little bugs in your program. First of all, you're not increasing the counter anywhere... and using a counter is inefficient anyway. Instead, if you're going to use the BufferedReader class, then you should use the StringTokenizer class.

    The Scanner class would be the easiest though.

    Good luck!

  3. #3
    Join Date
    Jan 2006
    Posts
    10
    I was looking around, and saw examples using those classes, but I don't think my teacher will let us use that yet, since we haven't gotten that far.
    Absit invidia

  4. #4
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Quote Originally Posted by docfish
    I was looking around, and saw examples using those classes, but I don't think my teacher will let us use that yet, since we haven't gotten that far.
    You're going to have to use something like that. You could also read in each line, then split it. eg:
    Code:
    String line;
    while ((line = in.readLine()) != null) {
        String[] split = line.split(" ");
        
        name = split[0];
        age = Integer.parseInt(split[1]);
        gpa = Integer.parseInt(split[2]);
    }

  5. #5
    Join Date
    Jan 2006
    Posts
    10
    I think I found a different way to do it, I'm not really sure if it is right:

    Code:
    import java.io.*;
    import java.text.DecimalFormat;
    
    public class lab17a2
    {
    public static void main(String args[]) throws IOException
    	{
    		int count=0;
    		int sum=0;
    		int age; double gpa;
    		String name;
    		File inFile = new File(args[0]);
    		BufferedReader inStream = new BufferedReader(new FileReader(inFile));	
    		if (inFile.exists())
    		{
    			String inString;									
    			while((inString = inStream.readLine()) != null)	
    			{
    			
    				 name=inString; 
       				 System.out.println(name);
       				
    	            inString = inStream.readLine(); 
        			age=Integer.parseInt(inString);   //converts age 
        			System.out.println(age); 
        		
        			inString = inStream.readLine(); 
                 	gpa=Double.parseDouble(inString);   //converts gpa 
                  	System.out.println(gpa); 
                  
                  
    			} 
    			
    
    			  			
    			inStream.close();    
    		}
    		else
    		{
    			System.out.println(inFile + " does not exist"); //if file does not exist
    			
    		}
    		System.out.println();
    	}
    }
    Also, I have a question, if I want to count the number of time that it diplays name, I know that I can use a for loop.
    Code:
    for(int a=1; a<??; a++)
    What would be my upperbound though?
    Thanks in advance!
    Absit invidia

Similar Threads

  1. Invalid DNS record
    By fegf in forum .NET
    Replies: 0
    Last Post: 12-30-2005, 05:51 AM
  2. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 11:06 AM
  3. Replies: 0
    Last Post: 11-19-2001, 01:11 AM
  4. Replies: 0
    Last Post: 11-13-2001, 09:07 PM
  5. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 06:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center