473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with reading arrays into a string

ok what my program is suppost to do is open a file - Grades.txt and read each line the breaking the lines down w/ tokens so i may later average some numbers

an example some of the lines from the text is:

234-42-1111 90 80 23 67
214-77-2222 12 45 80
112-45-7689 99 82 71 45

it has a id number on the left and grades on the right.

Im having trouble with reading the text into a string and exempting the lowest grades and then average them at the end

My codes so far is :


import java.util.StringTokenizer;
import java.io.*;


public class Exemption
{
public static void main (String [] args) throws IOException
{
// Strings
String studentNumber, studentgrade;

// access grades.txt

FileReader Grades = new FileReader("Grades.txt");
BufferedReader inputFile = new BufferedReader(Grades);

// reading lines
studentNumber = inputFile.readLine();
System.out.println("Reading file Grades.txt");
System.out.println("");
System.out.println("The grades for the class are listed below:");
System.out.println("");

// display and keep reading
while (studentNumber != null)
{
// display student number
System.out.println(studentNumber);
// read next student number
studentNumber = inputFile.readLine();
}


System.out.println("");
System.out.println("Next we will find out who is excempted from the final");

// tokens
String line, fileIn = "Grades.txt", fileOut = "Exempt.txt";
StringTokenizer tokens;
String name;

//input
FileReader fr = new FileReader(fileIn);
BufferedReader inFile= new BufferedReader(fr);

//output
FileWriter fw = new FileWriter (fileOut);
BufferedWriter bw = new BufferedWriter (fw);
PrintWriter outFile = new PrintWriter (bw);

line = inFile.readLine();
while (line != null)
{
tokens = new StringTokenizer(line);
name = null;

if (tokens.hasMoreTokens())
name = tokens.nextToken();


}
inputFile.close();


}




}


COULD SOMEBODY HELP ME PLEASE!!!
Aug 7 '08 #1
4 1999
blazedaces
284 100+
First of all, put your code in code tags please (when you post look to the right in the box that says "REPLAY GUIDELINES"). It's incredibly hard to read this way.

Are you reading from the file twice in the same method? Why?

I don't understand why you're just setting name to be every tokenizer one at a time.

Think about what you're trying to produce. Think about how you want to output it.

Edit: I think this is what you should be doing: tokenizing the string, making the first one equal to the student ID and taking the rest, one a time parsing them into an int and storing them. Then, after averaging them, keep the average. Sound about right?

Hope this helped

-blazed
Aug 7 '08 #2
Expand|Select|Wrap|Line Numbers
  1. import java.util.StringTokenizer;
  2. import java.io.*;
  3.  
  4.  
  5. public class Exemption
  6. {
  7. public static void main (String [] args) throws IOException
  8. {
  9. // Strings
  10. String studentNumber, studentgrade;
  11.  
  12. // access grades.txt
  13.  
  14. FileReader Grades = new FileReader("Grades.txt");
  15. BufferedReader inputFile = new BufferedReader(Grades);
  16.  
  17. // reading lines
  18. studentNumber = inputFile.readLine();
  19. System.out.println("Reading file Grades.txt");
  20. System.out.println("");
  21. System.out.println("The grades for the class are listed below:"); 
  22. System.out.println("");
  23.  
  24. // display and keep reading
  25. while (studentNumber != null)
  26. {
  27. // display student number
  28. System.out.println(studentNumber);
  29. // read next student number
  30. studentNumber = inputFile.readLine();
  31. }
  32.  
  33.  
  34. System.out.println("");
  35. System.out.println("Next we will find out who is excempted from the final");
  36.  
  37. // tokens
  38. String line, fileIn = "Grades.txt", fileOut = "Exempt.txt";
  39. StringTokenizer tokens;
  40. String name;
  41.  
  42. //input
  43. FileReader fr = new FileReader(fileIn);
  44. BufferedReader inFile= new BufferedReader(fr);
  45.  
  46. //output
  47. FileWriter fw = new FileWriter (fileOut);
  48. BufferedWriter bw = new BufferedWriter (fw);
  49. PrintWriter outFile = new PrintWriter (bw);
  50.  
  51. line = inFile.readLine();
  52. while (line != null)
  53. {
  54. tokens = new StringTokenizer(line);
  55. name = null;
  56.  
  57. if (tokens.hasMoreTokens())
  58. name = tokens.nextToken();
  59.  
  60.  
  61. }
  62. inputFile.close();
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70. }
  71.  
Aug 7 '08 #3
Expand|Select|Wrap|Line Numbers
  1. import java.util.StringTokenizer;
  2. import java.io.*;
  3.  
  4.  
  5. public class Exemption
  6. {
  7. public static void main (String [] args) throws IOException
  8. {
  9. // Strings
  10. String studentNumber, studentgrade;
  11.  
  12. // access grades.txt
  13.  
  14. FileReader Grades = new FileReader("Grades.txt");
  15. BufferedReader inputFile = new BufferedReader(Grades);
  16.  
  17. // reading lines
  18. studentNumber = inputFile.readLine();
  19. System.out.println("Reading file Grades.txt");
  20. System.out.println("");
  21. System.out.println("The grades for the class are listed below:"); 
  22. System.out.println("");
  23.  
  24. // display and keep reading
  25. while (studentNumber != null)
  26. {
  27. // display student number
  28. System.out.println(studentNumber);
  29. // read next student number
  30. studentNumber = inputFile.readLine();
  31. }
  32.  
  33.  
  34. System.out.println("");
  35. System.out.println("Next we will find out who is excempted from the final");
  36.  
  37. // tokens
  38. String line, fileIn = "Grades.txt", fileOut = "Exempt.txt";
  39. StringTokenizer tokens;
  40. String name;
  41.  
  42. //input
  43. FileReader fr = new FileReader(fileIn);
  44. BufferedReader inFile= new BufferedReader(fr);
  45.  
  46. //output
  47. FileWriter fw = new FileWriter (fileOut);
  48. BufferedWriter bw = new BufferedWriter (fw);
  49. PrintWriter outFile = new PrintWriter (bw);
  50.  
  51. line = inFile.readLine();
  52. while (line != null)
  53. {
  54. tokens = new StringTokenizer(line);
  55. name = null;
  56.  
  57. if (tokens.hasMoreTokens())
  58. name = tokens.nextToken();
  59.  
  60.  
  61. }
  62. inputFile.close();
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70. }
  71.  
My main problem is how to put the file into strings i guess to separate the id from the grades? Im very new with this so its kind of confusing!
Aug 7 '08 #4
JosAH
11,448 Expert 8TB
Read entire lines using a BufferedReader; split every line into individual
tokens using the String.split() method. Tokenizers are clumsy.

Alternatively use a Scanner to read ordinary int numbers.

kind regards,

Jos
Aug 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Grrrbau | last post by:
I'm a beginner. I'm looking for a good C++ book. Someone told me about Lafore's "Object-Oriented Programming in C++". What do you think? Grrrbau
2
by: WAYNEL | last post by:
Hi I am trying to re-write some of the example code that Agilent gives for VB to VB.Net. In .Net I keep getting the error 'cannot change the number of dimensions of an array'. I have paste...
1
by: john_g83 | last post by:
Hello all, new user to c programming and am having difficulty with a little program I am trying to write. Basically what I have is a file which contains a list of words i.e: tst.txt: customer...
8
by: hothead098 | last post by:
ASSIGNMENT (4) USING AND MANIPUPATING ARRAYS (Chapter 10 material) For this assignment you are to: 1) Create and manage arrays a) One of type integers (containing 10 elements). b) One of...
33
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now...
10
by: Tyler | last post by:
Hello All: After trying to find an open source alternative to Matlab (or IDL), I am currently getting acquainted with Python and, in particular SciPy, NumPy, and Matplotlib. While I await the...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
4
by: ndedhia1 | last post by:
I am reading in a file to see what delays I am getting on what IP address: QuoteBlockTiming exceeded 1000 ms: 1684 --- Fri Nov 06 06:09:10 CST 2009 170.137.94.95 Class key = 649126730 block...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.