【问题标题】:No need to check this! skip不需要检查这个!跳过
【发布时间】:2018-01-28 09:08:35
【问题描述】:

所以我正在尝试做但显然难以执行的事情是所以我正在尝试做但显然难以执行的事情是所以我正在尝试做但显然难以执行的事情是所以我正在尝试做但显然难以执行是所以我正在尝试做但显然难以执行是所以我正在尝试做但显然难以执行是所以我正在尝试做但显然难以执行是所以我正在尝试要做但显然难以执行的是 文本中的单行 f

import java.util.Scanner;
import java.io.*;
public class hello
{
   public static void main(String[] args) throws IOException
   {
      Scanner Keyboard = new Scanner(System.in);
      System.out.print();
      String response = Keyboard.nextLine();
      File inFile = new File(response);
      Scanner route = new Scanner(inFile);
      while ()
       {
         System.out.print(");
         String word = Keyboard.next();
         String Street = route.next();
         String stopNum = route.next();

【问题讨论】:

  • 您目前在使用此代码时遇到什么错误或问题?
  • 我不知道如何通读文件以找到匹配的 ID 号 @Izruo
  • 不要关闭Scanner 在循环中 ...

标签: po


【解决方案1】:

你在读完一个“行”后关闭你的文件(实际上,我不确定你读了多少行 - 你不打电话给nextLine)。您也没有解析该行。此外,我更喜欢 try-with-resources 而不是显式关闭(并且您的许多变量看起来像类名)。最后,您需要检查line 是否符合您的条件。这可能是这样的,

Scanner keyboard = new Scanner(System.in);
System.out.print("Enter filename >> ");
String response = keyboard.nextLine();
File inFile = new File(response);
System.out.print("Enter tram tracker ID >> ");
String word = keyboard.nextLine(); // <-- read a line. Bad idea to leave trailing
                                   //     new lines.
try (Scanner route = new Scanner(inFile)) {
    while (route.hasNextLine()) {
        String[] line = route.nextLine().split("\\^");
        String street = line[0];
        String stopNum = line[1];
        String trkID = line[2];
        String road = line[3];
        String suburb = line[4];
        if (!trkID.equals(word)) {
            continue;
        }
        System.out.printf("street: %s, stop: %s, id: %s, road: %s, suburb: %s%n", 
                street, stopNum, trkID, road, suburb);
    }
}

【讨论】:

    【解决方案2】:

    您的代码打印文件中的所有内容。

    要打印具有给定 ID 的行:

    你可以先将文件的所有行缓冲到一个 ArrayList 中,像这样在 main 方法中:

    ArrayList<String>  lines = new ArrayList<>();
    while (route.hasNextLine())
    {
        lines.add(route.nextLine());
    }
    

    然后创建一个方法来查找具有特定 ID 的行:

    public static int find(ArrayList information, int ID)
    {
        String idString = "" + ID;
        ListIterator<String> li = information.listIterator();
        String currentLine = "";
        int index = 0;
        while(li.hasNext())
        {
            currentLine = li.next();
    
            int count = 0;
            int index1 = 0;
            int index2 = 0;
    
            /*Trying to locate the string between the 2nd and 3rd ^ */
            for(int i = 0; i < currentLine.length(); i++)
            {
                 if(currentLine.substring(i, i+1).equals("^"))
                {
                    count++;
                    if(count == 2)
                        index1 = i;
                    else if(count == 3)
                    {
                        index2 = i;
                        break;
                    }
                }
            }
    
            if(currentLine.substring(index1+1, index2).equals (idString))
                return(index);
    
            index++;
            }
        //If no such ID found, return -1;
        return -1; 
    }
    

    在main方法中:

    System.out.println("enter an ID")
    int ID = Integer.parseInt(Keyboard.next());
    int lineNumber = find(lines, ID);
    if(lineNumber == -1)
        System.out.println("no information found");
    else
        System.out.println(lines.get(lineNumber));
    

    【讨论】:

      猜你喜欢
      • 2015-02-17
      • 2012-08-28
      • 2015-01-10
      • 2015-12-18
      • 1970-01-01
      • 2021-06-28
      • 2015-04-07
      • 2011-04-25
      • 1970-01-01
      相关资源
      最近更新 更多