-
-
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!
-
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 
-
 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]);
}
-
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
-
Replies: 0
Last Post: 12-30-2005, 04:51 AM
-
By Gordon Reichhardt in forum VB Classic
Replies: 2
Last Post: 01-08-2002, 10:06 AM
-
By Kyle Gabhart in forum Java
Replies: 0
Last Post: 11-19-2001, 12:11 AM
-
By Steven Anderson in forum Java
Replies: 0
Last Post: 11-13-2001, 08:07 PM
-
By Andrew McLellan in forum Java
Replies: 3
Last Post: 05-09-2001, 05: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks