You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Marathon类出现‘Method indexOf undefined for the type Marathon’错误的排查及文件内容提取实现求助

Fixing the "Method indexOf undefined for the type Marathon" Error in Your Java Code

Hey there! Let's work through this issue together—file parsing is a great first project, and it's totally normal to hit snags like this when you're starting out.

First, Why You're Seeing That Error

The core problem here is how you're trying to use indexOf() and your Scanner object:

  • indexOf() is a method belonging to the String class, not the Scanner class. You can't call it directly on your Stats Scanner object.
  • Your syntax Stats(indexOf(0, ',')) is invalid Java—this looks like you're trying to call a constructor for the Marathon class (hence the error about Marathon not having an indexOf method), which isn't what you want at all.

Step-by-Step Fixes

Let's rewrite your code to properly read and parse each line from the file:

  1. Read each line first
    Inside your while loop, start by reading the entire line into a String variable. This is the string we'll run all our indexOf() operations on:

    String line = Stats.nextLine();
    
  2. Parse each part of the line correctly
    Use indexOf() on the line string to find the positions of commas and spaces, then use substring() to extract the parts you need. Here's how to do each section:

    • Extract Point: Get everything from the start of the line to the first comma
    • Extract runnerNum: Get everything between the first comma's following space and the second comma
    • Extract Time: Get everything from the second comma's following space to the end of the line
  3. Move your print statement inside the loop
    Right now, you're only printing once after the loop finishes—this will only show you the last line's data. Move the System.out.println inside the loop to print each runner's info as you parse it.

Full Fixed Code

Here's the revised code with all these changes (plus some safeguards to handle unexpected line formats):

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

public class Marathon { 
    public static void main (String[] args) throws FileNotFoundException{ 
        // Initialize variables to avoid compile warnings
        String time = "", point = "", runnerNum = ""; 
        File runnersLog = new File("File1"); 
        Scanner stats = new Scanner(runnersLog); 

        // Read each line in the file
        while (stats.hasNextLine()) { 
            String line = stats.nextLine();
            
            // Find positions of key characters
            int firstComma = line.indexOf(',');
            if (firstComma == -1) continue; // Skip lines without a comma
            
            int spaceAfterFirstComma = line.indexOf(' ', firstComma + 1);
            if (spaceAfterFirstComma == -1) continue;
            
            int secondComma = line.indexOf(',', spaceAfterFirstComma + 1);
            if (secondComma == -1) continue;
            
            int spaceAfterSecondComma = line.indexOf(' ', secondComma + 1);
            if (spaceAfterSecondComma == -1) continue;
            
            // Extract each part
            point = line.substring(0, firstComma); 
            runnerNum = line.substring(spaceAfterFirstComma + 1, secondComma); 
            time = line.substring(spaceAfterSecondComma + 1); 
            
            // Print the parsed data immediately
            System.out.println("Point: " + point + ", Runner Number: " + runnerNum + ", Time: " + time);
        } 
        stats.close(); 
    } 
}

Quick Notes for Your Learning

  • Always remember: Scanner is for reading input (from files, console, etc.), while string manipulation methods like indexOf() and substring() belong to the String class.
  • The checks for -1 are important because indexOf() returns -1 if it can't find the character you're looking for—this prevents your program from crashing if a line in the file is formatted incorrectly.
  • Make sure your File1 is located in the root directory of your Java project (or use a full file path like new File("C:/your-folder/File1") if it's somewhere else).

内容的提问来源于stack exchange,提问作者CRenk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 11:27:52