首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从txt文件收集信息

从txt文件收集信息
EN

Stack Overflow用户
提问于 2021-11-21 03:47:01
回答 2查看 64关注 0票数 0

文件花名册: roster.txt

代码语言:javascript
复制
Reagan rebradshaw835
Ryley rbarber894
Peyton pstott885
Tyrese tmayo945
Caius ccharlton329

该程序必须处理异常处理。我目前正在努力克服的问题是能够获得适当的输出。我不知道是否有一种特定的方法可以找到包含名称的行,并返回行中除名称之外的所有内容。这样我们就可以返回分配的用户名。

例如,第一行显示:

代码语言:javascript
复制
Reagan rebradshaw835

方法仅返回rebradshaw835

下面是代码:(更新代码) vhthanh真的帮助我理解了如何读取文件!我决定使用替换字符串命令来编辑代码。如果没有vhthanhI,可能不会学习如何与文件交互。

代码语言:javascript
复制
    package chapter11;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;

public class LabProgram {
   
   public static String findID(String studentName, Scanner infoScnr) throws Exception {
    
       while (infoScnr.hasNextLine()) {
           
            String line = infoScnr.nextLine();
            //String[] values = line.split(" ", 2);
            
                        
            if (line.contains(studentName)) {
                
                line = line.replace(studentName + " ", "");
            
            return line;    
                
                
            }
               
               
           }
       
       throw new Exception ("Student ID not found for " + studentName);
        
        
       }
   
   public static String findName(String studentID, Scanner infoScnr) throws Exception {
       
       while (infoScnr.hasNextLine()) {
           
        String line = infoScnr.nextLine();
        //String[] values = line.split(" ", 2);
        
                    
        if (line.contains(studentID)) {
            
            line = line.replace(studentID + " ", "");
        
        return line;    
            
            
        }
           
           
       }
       
       throw new Exception ("Student name not found for " + studentID);
    
   }
   
   public static void main(String[] args) throws IOException {
      Scanner scnr = new Scanner(System.in);
      String studentName;
      String studentID;
      String studentInfoFileName;
      FileInputStream studentInfoStream = null;
      Scanner studentInfoScanner = null;
      
      // Read the text file name from user
      studentInfoFileName = scnr.next();

      // Open the text file
      studentInfoStream = new FileInputStream(studentInfoFileName);
      studentInfoScanner = new Scanner(studentInfoStream);
      
      // Read search option from user. 0: findID(), 1: findName()
      int userChoice = scnr.nextInt();

      // FIXME: findID() and findName() may throw an Exception.
      //        Insert a try/catch statement to catch the exception and output the exception message.
      try {
      if (userChoice == 0) {
         studentName = scnr.next();
         studentID = findID(studentName, studentInfoScanner);
         System.out.println(studentID);
      }
      else {
         studentID = scnr.next();
         studentName = findName(studentID, studentInfoScanner);
         System.out.println(studentName);
      }
      
      studentInfoStream.close(); 
      
      }
      
      
      catch (Exception exp) {
          
        System.out.println(exp.getMessage());  
      }
      
      
      
   }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-21 04:07:27

您需要从Scanner中读取每一行,并将字符串split()到一个包含2个值的数组中,然后进行比较。尝试以下代码:

代码语言:javascript
复制
public static String findID(String studentName, Scanner infoScnr) throws Exception {
    while (infoScnr.hasNextLine()) {
        String line = infoScnr.nextLine();
        String[] values = line.split(" ", 2);
        if (studentName != null && studentName.equals(values[0])) {
            return values[1];
        }
    }
    throw new Exception ("Student name not found for " + studentName);
}

public static String findName(String studentID, Scanner infoScnr) throws Exception {
    while (infoScnr.hasNextLine()) {
        String line = infoScnr.nextLine();
        String[] values = line.split(" ", 2);
        if (studentID != null && values.length > 1 && studentID.equals(values[1])) {
            return values[0];
        }
    }
    throw new Exception ("Student id not found for " + studentID);
}
票数 0
EN

Stack Overflow用户

发布于 2021-11-21 05:56:09

代码语言:javascript
复制
.split (" ")

对于间距

代码语言:javascript
复制
.split ("/n")

用于新行(nextLine)

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

https://stackoverflow.com/questions/70051669

复制
相关文章

相似问题

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