473,626 Members | 3,531 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 TFTFTFTFFFFTFTF FFTF
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 TFTFTFTFFFFTFTF FFTF
643828 TFTFTTTTTF

Help is needed........p liz

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 3465
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.index Of(' ');
ID = inputLine.subst ring(0, blankPosition);
answerString =
inputLine.subst ring(blankPosit ion + 1, inputLine.lengt h());
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 TFTFTFTFFFFTFTF FFTF
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 TFTFTFTFFFFTFTF FFTF
643828 TFTFTTTTTF

Help is needed........p liz

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
3054
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
7041
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 file... Thank you to all of you who can help me with this one...
19
10302
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 text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
4
9823
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 = tmpfile(); /* write into tmpFile */ ...
6
6337
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 browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes before the end of file, and going backwards is the way. In this case, am I guaranteed best results...
1
2011
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: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
7
6054
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 populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
5
14980
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 and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
3521
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
2
2833
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 wireless where I'm working could just be ungodly slow-- which it is.) Is reading a file much more resource/processor intensive than, say, including a .php file? What about the act of creating a simpleXML object? What about the act of checking the...
0
8266
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, 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...
0
8638
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 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...
1
8365
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,...
0
7196
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, 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...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
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();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
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 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.