473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to read from textfile in Java

35
Hi i'm a student studying java (i'm still a beginner). i have a project were i have to do a quiz with a set of questions. So i think that i have to type the questions in a textfile for example in notepad. But i need to call it from java. My problem is that i need to read each question as a whole sentence. The way it is written it is broken, writing a word on each line. How can i read it as a whole sentence? This is the source code i have written:


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

class FileInputScannerDemo2
{
public static void main( String[] args ) throws FileNotFoundException
{
// declare and initialize variables

String question = "";


Scanner LineInput = null;
// input
File inFile = new File( "questions.txt" ); // locate data source

// processing
if( inFile.exists() )
{
// create Scanner object with file object as source
LineInput = new Scanner( inFile );

while (LineInput.hasNext() )
{

question = LineInput.next();
this.getClass().getResourceAsStream("questions.txt ");

System.out.println( question+" ");
}
}
else
{
System.out.println( inFile + " does not exist" );
System.exit(0);
}


} // end main

} // end class

Pls if you can help me
Thanks a lot.
Dec 12 '07 #1
1 2039
BigDaddyLH
1,216 Expert 1GB
The simplest way to break a stream of characters into lines is to use BufferedReader and its readLine method. I suppose you could stick with Scanner, but it doesn't make it any easier.
Dec 12 '07 #2

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

Similar topics

2
by: Tim | last post by:
Greetings, I need to read the contents of a textfile to memory and then write these contents to another location. I was thinking of reading this data into a datareader, but I don't see...
8
by: andreas | last post by:
A textfile can have different formats like ANSI,UTF8, Unicode ... Using streamreader for a textfile can have different results for chars like é,à,è....depending of the fileformat. To solve this...
9
by: Justme | last post by:
Novice programmer needs help with using fgets to read and ignore the first two lines of a file. I've gone thru the previous posting regarding fgets, but none of them seems to help my situation. I...
8
friendjin
by: friendjin | last post by:
How can I read every line from the existed file. eg. the content of textfile: serialnumber = " 50"; serialfile path = "C:\\doc\\"; errorfile path = "C:\\doc\\d\\"; I want to read "50" from...
0
by: canephalanx | last post by:
Hello, i am making an IDE using c# and java as my target language i have most of my functions working already and now i need to know how to compile what i have in my textBox1 which contains all...
1
by: boss1 | last post by:
i have textfile containing multiple rows.Each row containing same type of strings.now my problem is i can read only one row frequently but i can't read multiple rows. so how can i solve this...
2
by: RaviRajhulk | last post by:
I am trying to read folder and subfolder and print their name into a text file.I am able to read the folder and its content and write it to the textfile but I am not able to read the content of the...
6
by: Per Juul Larsen | last post by:
Hi. My Textfile looks like this Variable 1 Variable 2 Variable 3 Variable 4 On opening the application it will read the textfile, and assign each line of variable to at textfield in VB and...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I am trying to loop a Textfile. This Textfile is located on a shared drive and it is used by another process. When I try to read it I get an exeption: The process cannot access the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.