Marathon类出现‘Method indexOf undefined for the type Marathon’错误的排查及文件内容提取实现求助
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 yourStatsScanner object.- Your syntax
Stats(indexOf(0, ','))is invalid Java—this looks like you're trying to call a constructor for theMarathonclass (hence the error about Marathon not having anindexOfmethod), 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:
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 ourindexOf()operations on:String line = Stats.nextLine();Parse each part of the line correctly
UseindexOf()on thelinestring to find the positions of commas and spaces, then usesubstring()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
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 theSystem.out.printlninside 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:
Scanneris for reading input (from files, console, etc.), while string manipulation methods likeindexOf()andsubstring()belong to theStringclass. - The checks for
-1are important becauseindexOf()returns-1if 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
File1is located in the root directory of your Java project (or use a full file path likenew File("C:/your-folder/File1")if it's somewhere else).
内容的提问来源于stack exchange,提问作者CRenk

