首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从用户输入输出的.txt数组

问从用户输入输出的.txt数组
EN

Stack Overflow用户
提问于 2018-08-26 08:41:27
回答 1查看 53关注 0票数 0

自那以后,我根据这个论坛的建议更新了这个代码。我仍然不知道如何让我选择的.txt文件打印出输入名称的所有实例。我的文件名为namesbystate,其中包含了我的所有.txt文件。访问这个和输入的名称的所有实例是我遇到问题的地方。我想知道我是否应该将myFile替换为namesbystate作为路径扩展?

代码语言:javascript
复制
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());
}
}

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-26 09:31:21

像这样的东西会带你到你想去的地方。我没有你的状态文本文件的格式,所以我不能为你写完整的程序

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

代码语言:javascript
复制
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());
    }
}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52022344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档