User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,768 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,478 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5873 | Replies: 6
Reply
Join Date: Oct 2004
Posts: 16
Reputation: SyLk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
SyLk SyLk is offline Offline
Newbie Poster

help on using StringTokenizer to read and compute multiple lines from text

  #1  
Mar 6th, 2005
currently my program only reads in the one first name and last name with 5 grades and computes the average from the first line of my input file.
eg.
Albert Lake 54 23 54 32 12 average test score 35

im trying to edit this code so that the file reads in multiple first and last names each having 5 grades and does the average just like its doing. im sure its possible to read in multple names on new lines in a text file without introducing more variables, im just not sure how to accomplish this.

example of input file...
Albert Lake 54 23 54 32 12
Paul Brown 32 43 65 87 34
sam jones 32 45 65 76 87
....

please give me some ideas on how i can do this.
Ty


double test1, test2, test3, test4, test5;
double average;
String firstName;
String lastName;
StringTokenizer tokenizer;

BufferedReader inFile = new
BufferedReader (new FileReader ("a:\\test.txt"));

PrintWriter outFile = new
PrintWriter(new FileWriter ("a:testavg.txt"));

DecimalFormat twoDecimal = new DecimalFormat ("0.00");

tokenizer =
new StringTokenizer (inFile.readLine());
firstName = tokenizer.nextToken();
lastName = tokenizer.nextToken();
outFile.println ("Student Name: " + firstName + " " + lastName);

test1 = Double.parseDouble(tokenizer.nextToken());
test2 = Double.parseDouble(tokenizer.nextToken());
test3 = Double.parseDouble(tokenizer.nextToken());
test4 = Double.parseDouble(tokenizer.nextToken());
test5 = Double.parseDouble(tokenizer.nextToken());

outFile.println("Test scores: "
+ twoDecimal.format (test1) + " "
+ twoDecimal.format (test2) + " "
+ twoDecimal.format (test3) + " "
+ twoDecimal.format (test4) + " "
+ twoDecimal.format (test5));

average = (test1 + test2 + test3 + test4 + test5) /5.0;

outFile.println ("Average test score: "
+ twoDecimal.format(average));

outFile.close();
}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,050
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 416
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: help on using StringTokenizer to read and compute multiple lines from text

  #2  
Mar 6th, 2005
Try using arrays instead of single variables, and just throw the reading code into a loop.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Oct 2004
Posts: 16
Reputation: SyLk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
SyLk SyLk is offline Offline
Newbie Poster

Re: help on using StringTokenizer to read and compute multiple lines from text

  #3  
Mar 6th, 2005
thank you very much for responding narrue but im still very much puzzled as to going about implementing an array into this. I just keep gettin stuck on how to create a loop on the readline section of the code so it reads the file line by line and outputs all these lines to a next file.
maybe its late and im not thinking straight ...but i won't be able to sleep until i get this
Reply With Quote  
Join Date: Sep 2004
Posts: 6,050
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 416
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: help on using StringTokenizer to read and compute multiple lines from text

  #4  
Mar 7th, 2005
>I just keep gettin stuck on how to create a loop on the readline section of the code
Try something like this:
String line;

while ((line = inFile.readLine()) != null) {
  // Tokenize and process
}
As for the array part, it would be considerably easier to treat each record as a complete entity rather than separate variables:
class Record {
  private double[] tests;
  private double average;
  private String firstName;
  private String lastName;

  public Record(String first, String last, double[] tests);
  public display();
};
Then you can create a data structure (such as an array) of Records. However, if you don't need to save a record before processing another, there's really no need to go to that kind of effort. You can just use the stream reading loop above with the code you have.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Oct 2004
Posts: 16
Reputation: SyLk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
SyLk SyLk is offline Offline
Newbie Poster

Re: help on using StringTokenizer to read and compute multiple lines from text

  #5  
Mar 7th, 2005
here is the adjustments i have made to my program. I was told by my professor that it was not necessary to use an array to read in several new lines and and send the data back to an output file. this code compiles but its shows a run time error.

// java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at StudentGrade.main(StudentGrade.java:35)
Exception in thread "main"
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class StudentGrade
{
  public static void main (String[] args) throws IOException, FileNotFoundException

  {

    //Declare Variables

    double test1, test2, test3, test4, test5;
    double average;
    String firstName;
    String lastName;
    StringTokenizer tokenizer;
    int count = 0;
    String inputLine = null;
    BufferedReader inFile = new
      BufferedReader (new FileReader ("a:\\test.txt"));

    PrintWriter outFile = new
      PrintWriter(new FileWriter ("a:testavg.txt"));

    DecimalFormat twoDecimal = new DecimalFormat ("0.00");



    inputLine = inFile.readLine();                         
    while(inputLine != null)
    {    
      tokenizer = new StringTokenizer(inputLine);

      firstName = tokenizer.nextToken();  
      lastName = tokenizer.nextToken();
      outFile.println ("Student Name: " + firstName + " " + lastName);

      test1 = Double.parseDouble(tokenizer.nextToken());
      test2 = Double.parseDouble(tokenizer.nextToken());
      test3 = Double.parseDouble(tokenizer.nextToken());
      test4 = Double.parseDouble(tokenizer.nextToken());
      test5 = Double.parseDouble(tokenizer.nextToken());

      outFile.println("Test scores: "
        + twoDecimal.format (test1) + " "
        + twoDecimal.format (test2) + " "
        + twoDecimal.format (test3) + " "
        + twoDecimal.format (test4) + " "
        + twoDecimal.format (test5));

      average = (test1 + test2 + test3 + test4 + test5) /5.0;

      outFile.println ("Average test score: "
        + twoDecimal.format(average));

      inputLine = inFile.readLine(); 
    }	
    outFile.close();
  }
}
Code tags added. -Narue
Reply With Quote  
Join Date: Sep 2004
Posts: 6,050
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 416
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: help on using StringTokenizer to read and compute multiple lines from text

  #6  
Mar 7th, 2005
My first guess would be a formatting issue in your input file.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Oct 2004
Posts: 16
Reputation: SyLk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
SyLk SyLk is offline Offline
Newbie Poster

Re: help on using StringTokenizer to read and compute multiple lines from text

  #7  
Mar 7th, 2005
Ty Narue....your first guess was pretty much correct. I had skipped a line in between the data on the input file
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 4:21 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC