473,499 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading from a file

8 New Member
I need help for reading a text file into in my java program.

The file contains this information:

ID# Student’s Answers
---------------------------
236499 TFTFTFTFFFFTFTFFFTF
643828 TFTFTTTTTF FTFTFTTF
917057 FTF FTTTFFFTF FTFTFT
656565 FTF FTFTFTF TTFT TTT
183742 FFTF FFT TFFFTFTFTFF

I have written the code and it compiles well. But when i execute the program it gives me exception errors.

The program reads only upto this place and from here it shows exception errors. My program is not able to read in space that is present in the text file.

ID# Student’s Answers
---------------------------
236499 TFTFTFTFFFFTFTFFFTF
643828 TFTFTTTTTF

Help is needed........pliz

The code i have written for reading this file is:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class read
  5. {
  6.      public static void main(String[] args) throws IOException
  7.      {
  8.     Scanner inFile = new Scanner(new FileReader("Z:\\StudentResponses.txt"));
  9.  
  10.         String student_responses;
  11.         String header1, header2;
  12.         int id;
  13.  
  14.         System.out.println("ID#\tStudent's Answers");
  15.         System.out.println("----------------------------");
  16.  
  17.         header1 = inFile.nextLine();
  18.         header2 = inFile.nextLine();
  19.  
  20.         while (inFile.hasNext())
  21.         {         
  22.             id = inFile.nextInt();
  23.             student_responses = inFile.next();
  24.  
  25.             System.out.println(id + "\t" + student_responses);
  26.         }                
  27.  
  28.         inFile.close();
  29.  
  30.         System.out.println();
  31.  
  32.     }     
  33.  
Oct 11 '06 #1
4 3459
satan
28 New Member
u have to create the files first:
student ID, answer key, etc etc
Oct 11 '06 #2
satan
28 New Member
try this code

while (inFile.hasNext())
{
inputLine = inFile.nextLine();
blankPosition = inputLine.indexOf(' ');
ID = inputLine.substring(0, blankPosition);
answerString =
inputLine.substring(blankPosition + 1, inputLine.length());
score = 0;
Oct 11 '06 #3
r035198x
13,262 MVP
I need help for reading a text file into in my java program.

The file contains this information:

ID# Student’s Answers
---------------------------
236499 TFTFTFTFFFFTFTFFFTF
643828 TFTFTTTTTF FTFTFTTF
917057 FTF FTTTFFFTF FTFTFT
656565 FTF FTFTFTF TTFT TTT
183742 FFTF FFT TFFFTFTFTFF

I have written the code and it compiles well. But when i execute the program it gives me exception errors.

The program reads only upto this place and from here it shows exception errors. My program is not able to read in space that is present in the text file.

ID# Student’s Answers
---------------------------
236499 TFTFTFTFFFFTFTFFFTF
643828 TFTFTTTTTF

Help is needed........pliz

The code i have written for reading this file is:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class read
  5. {
  6.      public static void main(String[] args) throws IOException
  7.      {
  8.     Scanner inFile = new Scanner(new FileReader("Z:\\StudentResponses.txt"));
  9.  
  10.         String student_responses;
  11.         String header1, header2;
  12.         int id;
  13.  
  14.         System.out.println("ID#\tStudent's Answers");
  15.         System.out.println("----------------------------");
  16.  
  17.         header1 = inFile.nextLine();
  18.         header2 = inFile.nextLine();
  19.  
  20.         while (inFile.hasNext())
  21.         {         
  22.             id = inFile.nextInt();
  23.             student_responses = inFile.next();
  24.  
  25.             System.out.println(id + "\t" + student_responses);
  26.         }                
  27.  
  28.         inFile.close();
  29.  
  30.         System.out.println();
  31.  
  32.     }     
  33.  
Yeah the integers and strings are getting mixed up.
This should work though:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Read {
  5.        public static void main(String[] args) throws IOException {
  6.     Scanner inFile = new Scanner(new FileReader(""Z:\\StudentResponses.txt""));
  7.     String header1, header2;
  8.     int id;
  9.     System.out.println("ID#\tStudent's Answers");
  10.     System.out.println("----------------------------");
  11.     header1 = inFile.nextLine();
  12.     header2 = inFile.nextLine();
  13.     while (inFile.hasNext()) {
  14.               String current = inFile.nextLine();
  15.            String[] data = current.split(" ");
  16.            String student_responses = "";
  17.            id = Integer.parseInt(data[0]);
  18.            for(int i = 0; i < data.length; i++) {
  19.         student_responses += data[i];
  20.            }
  21.           System.out.println(id + "\t" + student_responses);
  22.     }
  23.     inFile.close();
  24.     System.out.println();
  25.     }
Oct 11 '06 #4
Fazana
8 New Member
Thanks very much for that code. Now my program is reading all the information from that text file. But still, i have got a small problem with that piece of code.

As you can see from the text file that there is a space between the id# and student's answers. In the output, everything is clustered together with no space. Infact, the output should have first the id # and then a space which separates it from the students answers. In the students responses, there should also be spaces in between like this: TFTFTTTTTF FTFTFTTF
There is a space between F F like this: FspaceF. This space does not appear on the screen.
I tried it very hard to work it out but it does not work.

Hope to get an answer for this soon.
Oct 11 '06 #5

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

Similar topics

4
3038
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
7031
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
10269
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
4
9805
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
6
6316
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
1
1991
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
7
6028
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
14968
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
3510
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
2
2822
by: Derik | last post by:
I've got a XML file I read using a file_get_contents and turn into a simpleXML node every time index.php loads. I suspect this is causing a noticeable lag in my page-execution time. (Or the...
0
7132
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
7009
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...
0
7223
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...
1
6899
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...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
302
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.