分开我是相当新的java的所以任何帮助,将不胜感激。从文件中读取特定线和数据从一条线
我试图写一个程序,要求从读取该文件的用户,则它要求一个ID,该ID在文件中搜索并输出从该行中的信息。 文本文件,它会从被格式化这样写着:
苹果^ 201^3
香蕉^ 202^4
橙^ 205^5
的2nd
列是ID。
此外,我需要的信息分离出一些如何,所以我可以输出这样的:
项目:苹果
ID:201:
价格:$ 3
import java.util.*;
import java.io.*;
public class fruit
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a filename >> ");
String filename = keyboard.nextLine();
File f = new File(filename);
Scanner fin = new Scanner(f);
System.out.println("Enter item ID: ");
int fruitID = keyboard.nextInt();
while(fin.hasNextLine())
{
String line = fin.nextLine();
if(fin.hasNextInt(fruitID))
{
System.out.println(line);
}
else
{
System.out.println("ERROR");
}
}
fin.close();
}
}
客串,这是你在找什么:https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – Arne