Results 1 to 10 of 10
  1. #1
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default 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.

  3. #3
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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).

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Read/scan textfile in java. But there's more to it! Help

    how I can read/scan the text file (verb.txt).
    Your code uses the Scanner class. Have you searched on the Forum for code samples that use Scanner to read files?
    Start with reading the all the records of the file. You'll need a loop to do that.

  6. #6
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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.");
      	}
    
      			
    
    	}
    }
    Output is user typed values is underlined once again.:

    /*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

  7. #7
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Read/scan textfile in java. But there's more to it! Help

    how I can display random lines from the text file.
    You could use the Random class to give you random numbers and use some comparison on those numbers to decide to display a line. For example if the number is odd or if its value is greater than some given number.

  9. #9
    instinctx is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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;

    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Read/scan textfile in java. But there's more to it! Help

    I haven't learned to much about the random
    Write a small test program that uses the methods of the Random class and prints out the values that the methods return to see what the methods do. Try lots of the methods with lots of different arguments to those methods that take arguments.

    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

  1. Hi scan textfile upward
    By RichersooN in forum New To Java
    Replies: 3
    Last Post: 09-15-2011, 03:32 AM
  2. How to read text after delimiter in textfile ?
    By sandeep43 in forum New To Java
    Replies: 6
    Last Post: 07-27-2011, 11:11 AM
  3. how to read only parts of textfile by java
    By smn in forum SWT / JFace
    Replies: 2
    Last Post: 10-08-2010, 04:31 AM
  4. Do you have Virus scan computer code for Java
    By star24h.com in forum New To Java
    Replies: 5
    Last Post: 11-08-2009, 04:07 AM
  5. textfile with Java problems
    By saytri in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 06:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •