Results 1 to 10 of 10
- 11-22-2011, 01:58 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Read/scan textfile in java. But there's more to it! Help
Hey all.
So I got a schoolproject in java, and I'm going to read/scan a text file.
But the tricky part is: My javaprogram contains a question of how many lines of the text file I want to show.
And also, display them in my output.
Theres more to the this program that I've listed here. But I want to try to figure most of this out by myself. But I'm just stuck here
btw, the text file (verb.txt) contains 88 different verbs, and the program is going to be a "teaching" program. The user is going to type in how many verbs she want to be tested in. If she puts in ex. 5, she will generate 5 random verbs.
But, the verbs got one word "blurred" out. And the users object is to type in the missing word.
Like so:
Type in desired amount of verbs: 5
... - was - been
Type in what is missing: be
... - beat - beaten
Type in what is missing: beat
become - became - ...
Type in what is missing: becom
... - bound - bound
Type in what is missing: bound
Feil: bind
My java:
public static void verbTxt() {
Scanner input = new Scanner (System.in);
System.out.println("\nType in desired amount of verbs: ");
int tallSvar = input.nextInt();
if(tallSvar == 5){
fetch.me.only.5.verbs.plz.......();
}
File textFile = new File ("verb.txt");
try {Scanner fileInput = new Scanner (textFile);
}
catch (FileNotFoundException exc) {
System.out.println("\nFile was not found.");
}
}
}
Thanks for any answers.
- 11-22-2011, 05:08 PM #2
Re: Read/scan textfile in java. But there's more to it! Help
Can you explain what your problem is?
What does your program do now? Post its output
What do you want it to do? Add some comments to the output describing what you want the output to look like.
- 11-23-2011, 01:38 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Re: Read/scan textfile in java. But there's more to it! Help
Ok, my problem is that I don't have a clue on how I can read/scan the text file (verb.txt).
Because I'm going to scan it, (the file contains of 88 verb grammar that is listed like this (this is the 7 first:
be was been
beat beat beaten
become became become
begin began begun
bind bound bound
blow blew blown
break broke broken )
And the user is going to use the java program as a teaching device. This is done by letting the user type in how many verbs she wants to get shown (using input.nextString for example) (lets say 3 verbs this time). The program will randomly find 3 different verbs, and show it in the output.
But one of the words is "dot'ed" (...) out. Where the field is dot'ed out, the user have to type in what he/she thinks is the correct verb/answer (using input.nextString for example). The user is going to type her next.input answer after one verb. The java program will tell the user if the answer (next.input) is correct or not.
So this is how I imagined the output should be. Underlined text = user input value:
/* Program asks for how many verbs the user wants to get listed */
Type in desired amount of verbs: 5 /* via. next.input the user can type in how many verbs he/she wants to get listed */
/*program will not list 5 randomly fetched verbs from the file. As commanded by the user. But after every verb listed, the user can type in her answer. If the users next.input/answer is correct. The program will continue to the next verb in the list.*/
... - was - been
Type in what is missing: be /* answer is correct, the program continues to the next verb */
... - beat - beaten
Type in what is missing: beat /* answer is correct, the program continues to the next verb */
become - became - ...
Type in what is missing: become /* answer is correct, the program continues to the next verb */
... - bound - bound
Type in what is missing: bound /* answer was wrong, the program tells the user what is the correct answer */
Wrong: bind
That's the output I wish to achieve. Thanks for the help btw mr. Norm.
- 11-23-2011, 01:49 AM #4
Re: Read/scan textfile in java. But there's more to it! Help
Maybe you could read each line of file into a Collection. Randomly select a line. Randomly select a word not to display. Display other 2 words. Get user input. Compare input to "hidden" word. Display result(s).
- 11-23-2011, 01:49 AM #5
Re: Read/scan textfile in java. But there's more to it! Help
how I can read/scan the text file (verb.txt).
Start with reading the all the records of the file. You'll need a loop to do that.
- 11-23-2011, 03:04 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Re: Read/scan textfile in java. But there's more to it! Help
Ok, so I have coded a bit. And my code is:
Java Code:public static void verbTxt()throws IOException{ File file = new File("verb.txt"); try { Scanner input = new Scanner (System.in); System.out.println("\nType in desired amount of verbs: "); final int end = input.nextInt(); System.out.println(); final int start = 0; final LineNumberReader in = new LineNumberReader(new FileReader(file)); String line=null; while ((line = in.readLine()) != null && in.getLineNumber() <= end) { if (in.getLineNumber() >= start){; System.out.println(line); } } }catch(FileNotFoundException ex){ System.out.println("Failure, file was not found."); } } }
/*i'm going to start in the middle of my program. As it's here I got the problems.*/
Type in desired amount of verbs:
6
be was been
beat beat beaten
become became become
begin began begun
bind bound bound
blow blew blown
Process completed.
___________
Now I only need to find out how I can display random lines from the text file. Dot(.....) out the first word (or random word would have been cooler). And let the user type an answer between every verb line.
Any help what so ever would be appreciated, and also I thank you both so much for the earlier responses.Last edited by Norm; 11-23-2011 at 03:54 PM. Reason: change quote tags to code
- 11-23-2011, 03:07 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Re: Read/scan textfile in java. But there's more to it! Help
It sounds like a very good plan. Though my university haven't thought me that. So don't have a clue on how to do it. Check out my code in the latest comment, and see if it's possible to do something like what you said.
Thanks again
- 11-23-2011, 03:58 PM #8
Re: Read/scan textfile in java. But there's more to it! Help
how I can display random lines from the text file.
- 11-24-2011, 02:45 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Re: Read/scan textfile in java. But there's more to it! Help
Well the big problem is, that I haven't learned to much about the random subject.
I was trying this (which failed.....)
String getRandomNameLocation(String line)
{
Random ran = new Random();
location = ran.nextLine(line);
return;
}
- 11-24-2011, 02:56 PM #10
Re: Read/scan textfile in java. But there's more to it! Help
I haven't learned to much about the random
If you get compiler errors and can not figure out what the problem is, copy and paste the full text of the message here with your questions about the error.
Similar Threads
-
Hi scan textfile upward
By RichersooN in forum New To JavaReplies: 3Last Post: 09-15-2011, 03:32 AM -
How to read text after delimiter in textfile ?
By sandeep43 in forum New To JavaReplies: 6Last Post: 07-27-2011, 11:11 AM -
how to read only parts of textfile by java
By smn in forum SWT / JFaceReplies: 2Last Post: 10-08-2010, 04:31 AM -
Do you have Virus scan computer code for Java
By star24h.com in forum New To JavaReplies: 5Last Post: 11-08-2009, 04:07 AM -
textfile with Java problems
By saytri in forum New To JavaReplies: 4Last Post: 12-29-2007, 06:16 PM
Bookmarks