简体   繁体   中英

Suggestions with Strings from File

I am trying to read from a external file. I have successfully read from the file but now I have a little problem. The file contains around 88 verbs. The verbs are written in the file like this: be was been beat beat beaten become became become and so on...

What I need help with now is that I want a quiz like programe where only two random strings from the verb will come up and the user have to fill inn the one which is missing. Instead of the one missing, I want this("------"). My english is not so good so I hope you understand what I mean.

    System.out.println("Welcome to the programe which will test you in english verbs!");
    System.out.println("You can choose to be tested in up to 88.");
    System.out.println("In the end of the programe you will get a percentage of total right answers.");

    Scanner in = new Scanner(System.in);
    System.out.println("Do you want to try??yes/no");

    String a = in.nextLine();
    if (a.equals("yes")) {
        System.out.println("Please enter the name of the file you want to choose: ");

    } else {
        System.out.println("Programe is ended!");
    }



    String b = in.nextLine();
    while(!b.equals("verb.txt")){
        System.out.println("You entered wrong name, please try again!");
         b = in.nextLine();

    }
    System.out.println("How many verbs do you want to be tested in?: ");
    int totalVerb = in.nextInt();
    in.nextLine();


    String filename = "verb.txt";
    File textFile = new File(filename);
    Scanner input = new Scanner(textFile);

    for (int i = 1; i <= totalVerb; i++){



        String line = input.nextLine();

        System.out.println(line);
        System.out.println("Please fill inn the missing verb: ");
        in.next();
    }

    System.out.println("Please enter your name: ");
    in.next();

You can do something like this

import java.util.Scanner;
import java.io.*;

public class GuessVerb {

    public static void main(String[] args) throws IOException{

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a file name: ");

        String fileName = in.nextLine();
        File file = new File(fileName);

        Scanner input = new Scanner(file);

        String guess = null;
        int correctCount = 0;

        while(input.hasNextLine()) {
            String line = input.nextLine();          // get the line

            String[] tokens = line.split("\\s+");    // split it into 3 word

            int randNum = (int)(Math.random() * 3);  // get a random number 0, 1, 2

            String newLine = null;                          // new line

            int wordIndex = 0;
            switch(randNum){                         // case for random number
                case 0: newLine = "------ " + tokens[1] + " " + tokens[2]; 
                            wordIndex = 0; break;
                case 1: newLine = tokens[0] + " ------ " + tokens[2]; 
                            wordIndex = 1; break;
                case 2: newLine = tokens[0] + " " + tokens[1] + " -------";
                            wordIndex = 2; break;
           }

           System.out.println(newLine);

           System.out.println("Please fill inn the missing verb: ");
           guess = in.nextLine();

           if (guess.equals(tokens[wordIndex])){
               correctCount++;
           }
        }
        System.out.println("You got " + correctCount + " right");
    }
}

Above is complete running program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM