用户输入的.txt数组

时间:2018-08-26 00:41:27

标签: java java.util.scanner

此后,我根据该论坛的建议更新了此代码。我仍然对如何选择我的.txt文件以打印出所输入名称的所有实例感到困惑。我所有包含.txt文件的文件都被命名为namesbystate。要访问此文件,输入的所有名称实例都是我遇到的问题。我想知道是否用namesbystate替代myFile作为路径扩展?

package babynamestatesocial;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class BabyNameStateSocial {

private Scanner x;

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

// Scanner variable set up to intake user input for state selection and person's name
Scanner scan = new Scanner(System.in);
System.out.println("Available state files are: \n" +
        "AK " + "AL " + "AR " + "AZ " + "CA " + "CO " + "\n" +
        "CT " + "DC " + "DE " + "FL " + "GA " + "HI " + "\n" +
        "IA " + "ID " + "IL " + "IN " + "KS " + "KY " + "\n" +
        "LA " + "MA " + "MD " + "ME " + "MI " + "MN " + "\n" +
        "MO " + "MS " + "MT " + "NC " + "ND " + "NE " + "\n" +
        "NH " + "NJ " + "NM " + "NV " + "NY " + "OH " + "\n" +
        "OK " + "OR " + "PA " + "RI " + "SC " + "SD " + "\n" +
        "TN " + "TX " + "UT " + "VA " + "VT " + "WA " + "\n" +
        "WI " + "WV " + "WY " + "\n");
System.out.print("Enter a state to read names from: " + "\n");
String filename = scan.nextLine() + ".txt";
System.out.println("Which name would you like to look up?");
String personName = scan.nextLine();

File myFile = new File(filename);

openFile(myFile,personName);

}

private static void openFile(File myFile, String personName){
try {
    Scanner sc = new Scanner(myFile);
    while (sc.hasNext()) {
        // nextLine variable now has the line from the file in it that matches the name the person input
        String nextLine = sc.nextLine();
        if (nextLine.contains(personName)) {

        }
    }
} catch(FileNotFoundException e) {
    System.out.print(e.getMessage());
}
}

}

1 个答案:

答案 0 :(得分:0)

像这样的事情会让您到达想要的地方。我没有您的状态文本文件的格式,所以我无法为您编写完整的程序

(编辑-我只是稍微修改了代码。我应该写的不是sc.next(),而是sc.nextLine()。以下程序通过该编辑成功运行):

package babynamestatesocial;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class BabyNameStateSocial {



private Scanner x;

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


    Scanner scan = new Scanner(System.in);
    System.out.println("Available state files are: \n" +
            "AK " + "AL " + "AR " + "AZ " + "CA " + "CO " + "\n" +
            "CT " + "DC " + "DE " + "FL " + "GA " + "HI " + "\n" +
            "IA " + "ID " + "IL " + "IN " + "KS " + "KY " + "\n" +
            "LA " + "MA " + "MD " + "ME " + "MI " + "MN " + "\n" +
            "MO " + "MS " + "MT " + "NC " + "ND " + "NE " + "\n" +
            "NH " + "NJ " + "NM " + "NV " + "NY " + "OH " + "\n" +
            "OK " + "OR " + "PA " + "RI " + "SC " + "SD " + "\n" +
            "TN " + "TX " + "UT " + "VA " + "VT " + "WA " + "\n" +
            "WI " + "WV " + "WY " + "\n");
    System.out.print("Enter a state to read names from: " + "\n");
    String filename = scan.nextLine() + ".txt";
    System.out.println("Which name would you like to look up?");
    String personName = scan.nextLine();

    File myFile = new File(filename);

    openFile(myFile,personName);

}

private static void openFile(File myFile, String personName){
    try {
        Scanner sc = new Scanner(myFile);
        while (sc.hasNext()) {
            String nextLine = sc.nextLine();
            if (nextLine.contains(personName)) {
                //nextLine variable now has the line from the file in it that matches the name the person input so you need to parse that line and do something with it
            }
        }
    } catch(Exception e) {
        System.out.print(e.getMessage());
    }
}

}