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: -
import java.util.*;
-
import java.io.*;
-
-
public class read
-
{
-
public static void main(String[] args) throws IOException
-
{
-
Scanner inFile = new Scanner(new FileReader("Z:\\StudentResponses.txt"));
-
-
String student_responses;
-
String header1, header2;
-
int id;
-
-
System.out.println("ID#\tStudent's Answers");
-
System.out.println("----------------------------");
-
-
header1 = inFile.nextLine();
-
header2 = inFile.nextLine();
-
-
while (inFile.hasNext())
-
{
-
id = inFile.nextInt();
-
student_responses = inFile.next();
-
-
System.out.println(id + "\t" + student_responses);
-
}
-
-
inFile.close();
-
-
System.out.println();
-
-
}
-
4 3465
u have to create the files first:
student ID, answer key, etc etc
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;
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: -
import java.util.*;
-
import java.io.*;
-
-
public class read
-
{
-
public static void main(String[] args) throws IOException
-
{
-
Scanner inFile = new Scanner(new FileReader("Z:\\StudentResponses.txt"));
-
-
String student_responses;
-
String header1, header2;
-
int id;
-
-
System.out.println("ID#\tStudent's Answers");
-
System.out.println("----------------------------");
-
-
header1 = inFile.nextLine();
-
header2 = inFile.nextLine();
-
-
while (inFile.hasNext())
-
{
-
id = inFile.nextInt();
-
student_responses = inFile.next();
-
-
System.out.println(id + "\t" + student_responses);
-
}
-
-
inFile.close();
-
-
System.out.println();
-
-
}
-
Yeah the integers and strings are getting mixed up.
This should work though: - import java.util.*;
-
import java.io.*;
-
-
public class Read {
-
public static void main(String[] args) throws IOException {
-
Scanner inFile = new Scanner(new FileReader(""Z:\\StudentResponses.txt""));
-
String header1, header2;
-
int id;
-
System.out.println("ID#\tStudent's Answers");
-
System.out.println("----------------------------");
-
header1 = inFile.nextLine();
-
header2 = inFile.nextLine();
-
while (inFile.hasNext()) {
-
String current = inFile.nextLine();
-
String[] data = current.split(" ");
-
String student_responses = "";
-
id = Integer.parseInt(data[0]);
-
for(int i = 0; i < data.length; i++) {
-
student_responses += data[i];
-
}
-
System.out.println(id + "\t" + student_responses);
-
}
-
inFile.close();
-
System.out.println();
-
}
-
}
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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:
|
|
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 */
...
|
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...
| |
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...
|
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...
|
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...
|
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++.
|
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...
|
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: 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: 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...
|
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: 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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |