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.Strin gTokenizer;
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("Gra des.txt");
BufferedReader inputFile = new BufferedReader( Grades);
// reading lines
studentNumber = inputFile.readL ine();
System.out.prin tln("Reading file Grades.txt");
System.out.prin tln("");
System.out.prin tln("The grades for the class are listed below:");
System.out.prin tln("");
// display and keep reading
while (studentNumber != null)
{
// display student number
System.out.prin tln(studentNumb er);
// read next student number
studentNumber = inputFile.readL ine();
}
System.out.prin tln("");
System.out.prin tln("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(file In);
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.hasMore Tokens())
name = tokens.nextToke n();
}
inputFile.close ();
}
}
COULD SOMEBODY HELP ME PLEASE!!!
4 2022
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
- 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();
-
-
-
}
-
-
-
-
-
}
-
- 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();
-
-
-
}
-
-
-
-
-
}
-
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!
JosAH 11,448
Recognized Expert MVP
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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 the module I am having an issue with. Can anyone help!
|
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
customers
data
deactivate
|
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 type strings (containing 20 elements).
c) One of type char (containing 30 elements).
|
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 (tried: visual
studio 2005 + linux/macintosh gcc)...
That's a pretty good thing. I think the error still appears using both
gcc and visual studio 2005. Everything is standard C (ANSI C ?? I don't
know the difference) - but since so many functions...
| |
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 delivery of Travis Oliphant's
NumPy manual, I have a quick question (hopefully) regarding how to read
in Fortran written data.
The data files are not binary, but ASCII text files with no formatting
and mixed data types (strings, integers,...
|
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 seats as 0 and
the flight no. is also repeating. If any can tell why is this please
help me.
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
|
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)
Your first task is to write a program which reads this file into two
parallel arrays in memory. One array contains the titles, and the
other array contains the authors. The arrays are 'parallel' in the
sense that the n-th element of the authors...
|
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 size = 1 session = W_MAIN
QuoteBlockTiming exceeded 1000 ms: 1640 --- Fri Nov 06 06:09:18 CST 2009 170.137.94.2 Class key = 649126749 block size = 1 session = W_MAIN
QuoteBlockTiming exceeded 1000 ms: 1146 --- Fri Nov 06 06:28:25 CST 2009 170.137.88.2...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |