- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须根据出现在它们之前的字符串是否是某个关键字“load”从输入文件中读取整数。没有关键数字告诉我们要输入多少个数字。这些数字必须保存到一个数组中。为了避免为扫描的每个附加数字创建和更新新数组,我想使用第二个扫描器首先查找整数的数量,然后让第一个扫描器扫描多次,然后再恢复到测试字符串。我的代码:
public static void main(String[] args) throws FileNotFoundException{
File fileName = new File("heapops.txt");
Scanner scanner = new Scanner(fileName);
Scanner loadScan = new Scanner(fileName);
String nextInput;
int i = 0, j = 0;
while(scanner.hasNextLine())
{
nextInput = scanner.next();
System.out.println(nextInput);
if(nextInput.equals("load"))
{
loadScan = scanner;
nextInput = loadScan.next();
while(isInteger(nextInput)){
i++;
nextInput = loadScan.next();
}
int heap[] = new int[i];
for(j = 0; j < i; j++){
nextInput = scanner.next();
System.out.println(nextInput);
heap[j] = Integer.parseInt(nextInput);
System.out.print(" " + heap[j]);
}
}
}
scanner.close();
}
我的问题似乎是通过 loadscan 进行扫描(仅用于整数的辅助扫描器)也会使主扫描器向前移动。有什么办法可以阻止这种情况发生吗?有什么方法可以让编译器将扫描器和加载扫描视为单独的对象,尽管它们执行相同的任务?
最佳答案
您当然可能有两个 Scanner 对象同时从同一个 File 对象读取。推进一个不会推进另一个。
示例
假设myFile
的内容是123 abc
。下面的代码片段
File file = new File("myFile");
Scanner strFin = new Scanner(file);
Scanner numFin = new Scanner(file);
System.out.println(numFin.nextInt());
System.out.println(strFin.next());
...打印以下输出...
123
123
但是,我不知道你为什么要这样做。使用单个扫描仪来实现您的目的会简单得多。我在下面的代码片段中调用了我的 fin
。
String next;
ArrayList<Integer> readIntegers = new ArrayList<>();
while (fin.hasNext()) {
next = fin.next();
while (next.equals("load") {
next = fin.next();
while (isInteger(next)) {
readIntegers.Add(Integer.parseInt(next));
next = fin.next();
}
}
}
关于Java:两个扫描仪读取同一输入文件。可行吗?有用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22723630/
我在使用 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
我是一名优秀的程序员,十分优秀!