- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道是否有一种方法可以让扫描器读取java中的文件并将其编译成数组列表的对象,例如,如果正在读取的文件与workers属性的基本格式相同文件存在;姓名、薪资( float )、技能1( float )、技能2( float ),然后在引入新 worker 后再次重复。如;
文件“workers.txt”:
鲍勃·溢出全日制 12.50 份绘画.45雕刻.85
苏珊·内瑟斯兼职 7.50 份绘画.80雕刻.25
然后将其编译为具有给定属性的worker 1、worker 2、worker 3等并放入数组中。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class RaceCarOne
{
public static void main(String args[]) throws FileNotFoundException
{
//creating File instance to reference text file in Java
File text = new File("C:\\Users\\jayli\\Desktop\\Workers.txt");
//Creating Scanner instance to read File in Java
Scanner s = new Scanner(text);
//Reading each line of file using Scanner class
int lineNumber = 1;
while(s.hasNextLine())
{
String line = s.nextLine();
System.out.println("line " + lineNumber + " :" + line);
lineNumber++;
}
s.close();
}
}
class Worker
{
File text = new File("C:\\Users\\jayli\\Desktop\\Workers.txt");
Scanner s = new Scanner(text);
String Name;
int WorkHours;
int Fab;
int Serv;
int Diag;
int Trans;
int Intake;
int BW;
int Paint;
boolean working = false;
Worker(String workername, float pay, float Fab, float Serv, float Diag, float Trans, float Intake, float BW, float Paint)
{
while(s.hasNextLine())
{
workername = s.nextLine();
pay = s.nextInt();
Fab = s.nextInt();
Serv = s.nextInt();
Diag = s.nextInt();
Trans = s.nextInt();
Intake = s.nextInt();
BW = s.nextInt();
Paint = s.nextInt();
}
}
}
最佳答案
我建议使用 s.nextline() 来获取一行,然后根据您的意愿分割它。您可以为此使用正则表达式模式,您可以使用 Regexr 进行测试。例如。或者,您可以使用 split() -方法并按空格分割。然而,由于这会引发其他问题(例如名称中带有空格的技能),我同意 Sweeper 的观点,因为您可能应该使用 JSON 之类的内容作为输入。
此外,我认为您的 Worker 构造函数只是循环遍历整个文件,并且每次都会在最后一行得到信息。
关于java - 有没有办法使用扫描仪通过读取文件来构建一组对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58188111/
我在使用 Scanner 类中的 useDelimiter 时遇到一些问题。 Scanner sc = new Scanner(System.in).useDelimiter("-"); while(
我是 Java 新手。 我目前正在做一个业余项目;制作基于文本的游戏。我意识到使用 Switch 语句对于此类游戏非常有用。 这基本上就是它的工作原理。 我问用户,你想做什么? 吃 步行 等等 那么,
我正在尝试使用扫描仪从“p.addPoint(x,y);”形式的字符串中读取代码行 我想要的正则表达式格式是: *任何内容*.addPoint(*空格或无* 或 ,*空格或无* 到目前为止我所尝试的方
我正在使用 java Scanner 尝试从名为 Inventory.txt 的文本文件中提取产品信息。 此文件包含以下格式的产品数据: “Danelectro|Bass|D56BASS-AQUA|3
我是java初学者,我正在努力让这段代码正常工作。我正在尝试读取 CSV 文件,然后使用该数据计算平均值,然后返回平均值的最高最低值和平均值的摘要。 输入如下所示: Alicia Marks,89,9
当我进入/忽略文件 reg.txt 中的最后一个新行时,我需要一些有关退出的帮助。截至目前,当它到达最后一行时,我收到一个错误,其中不包含任何内容。 public String load() {
我的程序应该提示用户输入与参加了多少次考试相关的考试分数。然而,这工作得很好,当用户输入负的考试分数时,应该让他们再次重新进入所有三项考试。我的程序会保存任何非负面的考试,因此当您重新输入三项考试时,
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 9 年前。 下面给出的
我想读取用户输入,例如:11 12 13 14 15 16 Scanner sc = new Scanner(System.in); while(sc.hasNext()){
String[] invalidChars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; Scanner sc = ne
我有一个txt文件,每行包含两个单词,例如: USA 321 France 1009 ... Germany 902 如何在二维数组中逐字读取该文件?我有: List> temps = new Arr
我的 Java 作业有问题。我收到意外异常,特别是: java.util.NoSuchElementException: No line found 我正在使用 Scanner(System.in)
我有一个很大的困惑,当我们有 Sonar 服务器时 Sonar 扫描仪有什么用?当我使用 soarqube 服务器分析一个项目时,它进行了分析并且运行良好。我仍然很困惑为什么我们也需要扫描仪。 与ec
为什么我在递归方法中遇到无限循环,而没有机会输入任何符号来打破它? class Test { int key=0; void meth(){ System.out.println
我在运行此函数时遇到错误。它使用扫描仪在某个文件中查找单词。 这里是: public static boolean VerifyExistWord(File FileToSearch, String
各位程序员大家好。 我有一些代码,spring工具套件编辑器的响应也不同,也许你们中的一些聪明人知道为什么。 File inputFile = new File(System.getProperty(
是否可以从我的网络应用程序中使用 Flash 访问通用 twain 扫描仪,保存文件并将其上传到我的应用程序中? 我已经通过谷歌进行了一些搜索,但无法找到更多细节。是否有任何预制的解决方案,付费/免费
我试图在 float 组中引入一组 float 字: protected float[] a = new float [100]; public void setCoef(){ System.
我一直在做一项充当拼字游戏词典的编程作业。该程序接受用户的输入,并根据用户从菜单中请求的内容输出包含单词列表的文件。我遇到的问题与 Scanner.nextLine() 有关。 我不太清楚为什么,但由
我试图让程序询问百分比(等级),但我希望它在用户进行第一个输入并看到输出后再次询问。我在循环中遇到问题,因为未分配变量 myMark。 import java.util.Scanner; public
我是一名优秀的程序员,十分优秀!