Java 扫描字符串时如何使用扫描程序方法?
我有一个文本文件,由几行由空格分隔的整数组成。我要扫描第一行,然后对第一行中的每个整数做一些事情Java 扫描字符串时如何使用扫描程序方法?,java,Java,我有一个文本文件,由几行由空格分隔的整数组成。我要扫描第一行,然后对第一行中的每个整数做一些事情 String lineString = textScan.nextLine(); //scans the first line of the text file. Scanner Line = new Scanner(lineString); //scans the String retrieved from the text file. while (Line.hasNe
String lineString = textScan.nextLine(); //scans the first line of the text file.
Scanner Line = new Scanner(lineString); //scans the String retrieved from the text file.
while (Line.hasNextInt())
{ //do stuff
}
所以我有一个扫描仪(textScan),它扫描文本文件的第一行,然后将其保存为字符串。然后我有第二个扫描器,它扫描字符串以检查其中的值是否为整数
但是while语句不允许我使用“Line.hasNextInt”来扫描字符串!那么,如何扫描字符串行以确定它是否有整数呢
编辑:
对不起,我的措辞有误。循环将无限运行,因此我尝试在循环之前创建一个print语句:System.out.println(Line);,然后打印出这个错误:
Scanner[delimiters=\p{javaWhitespace}+][position=0][match
valid=false][need input=false][source
closed=false][skipped=false][group separator=\,][decim al
分隔符=.][正前缀=][负前缀=\Q-\E][正
后缀=][负后缀=][NaN字符串=\Q?\E][无限字符串=\Q∞\[英]
奇怪的是它编译得很好
输入文件:
5 4 4 3 5
4 2 2 5 3
5 2 5 2 3
5 4 3 2 3
5 4 2 2 5
4 4 2 4 2
3 3 5 3 5
5 2 4 5 2
4 4 5 4 2
2 4 3 5 2
3 3 3 5 3
2 4 5 3 4
3 5 5 4 3
3 4 2 2 4
5 5 5 4 4
3 4 4 4 5
3 2 4 2 4
5 4 4 2 4
5 3 5 2 3
抱歉,误解了您前面的问题。如果您在同一行中有多个整数,您可以使用分隔符拆分它们,但仍然使用单个扫描仪:
String yourFilename = "filename.txt";
File file = new File(yourFilename).useDelimiter(" ");
Scanner sn = new Scanner(file);
while(sn.hasNextInt()){
// Do your work here
}
您无需使用扫描仪解决该问题
public static void main(String[] args) {
String myString = "This is not int";
String myInt = "123456";
int copyOfInt = 0;
try {
copyOfInt = Integer.parseInt(myInt);
System.out.println(copyOfInt);
copyOfInt = Integer.parseInt(myString);
System.out.println(myString);
} catch (Exception e) {
System.out.println("Not int");
}
// Put this in a loop and you can keep doing stuff
}
或者您可以使用FileInputStream,我认为这样更好。
以下是一个例子:
try (FileInputStream readFile = new FileInputStream(refFile);
InputStreamReader readIn = new InputStreamReader(readFile, "UTF8")) {
BufferedReader ReadBuffer = new BufferedReader(readIn);
String line = "";
while ((line = ReadBuffer.readLine()) != null) {
// Search my string of interest
if (line.equals("Potential/V, Current/A")) {
while ((line = ReadBuffer.readLine()) != null) {
// Get data after string of interest
// Use createDataType_1_Object() to get my data
myDataArray.add(createDataType_1_Object(line));
}
}
}
} catch (Exception e) {
System.out.println(e);
}
我明白问题所在。
您需要在循环中使用Line.nextInt()
来移动光标
否则,直线始终指向起点,循环永远不会结束
要正确使用它,请调用nextInt(),以便它拾取下一个标记:
while (Line.hasNextInt())
{
System.out.println(Line.nextInt());
}
while (Line.hasNextInt())
{
System.out.println("Infinity");
}
无限循环:
while (Line.hasNextInt())
{
System.out.println(Line.nextInt());
}
while (Line.hasNextInt())
{
System.out.println("Infinity");
}
在while循环中,需要放置布尔表达式:
while(Boolean_expression)
{
//Statements
}
您不能将字符串
作为参数输入到Scanner类中。
Scanner类接受
输入流源
->谢谢你的评论,我错了。“不允许我使用”你是什么意思?是否有编译错误消息?程序运行时出现异常?请编辑这个问题,包括你所遇到的具体问题。每一行的确切格式是什么?也要考虑正则表达式是否更适合你的需求,这将读取文件的所有行。@Guy想要的是只读取第一行,然后单独解析该行的int值。因此,要么需要两个扫描器,要么需要一个扫描器来读取输入,然后他需要在该字符串上使用split来获取输入。那么,Java API从指定字符串生成扫描器意味着什么@托马斯:这实际上是正确的<代码>扫描仪(字符串源)构造一个新的扫描仪,生成从指定字符串扫描的值。如果需要,您可以尝试此示例代码<代码>扫描仪行=新扫描仪(“5 4 3 5”)while(Line.hasNextInt())
System.out.println(Line.nextInt())代码>啊,你说得对!这似乎就是问题所在!这是有道理的。是否有一种方法可以在不打印的情况下将光标移到下一个对象上(或从字符串列表中删除该对象)?是的,只需调用Line.nextInt()代码>方法。不必指定或保存其值,也不必在任何地方打印。在循环内部,作为第一条语句,您可以调用Line.nextInt(),然后继续处理。只要在循环内调用nextInt()
,它就会工作。如果问题解决了,你能接受答案并结束这个问题吗?哦,真有趣,我不知道你能做到!谢谢,这两个问题已经解决了哦,很抱歉,我只是让你的un接受了一点时间,把我的评论曝光在这里。我认为扫描仪在这里要简单得多。我更喜欢一个扫描仪,另一个使用正则表达式或字符串拆分。这将比任何其他解决方案都简单得多。我同意你的看法,我认为这更多的是个人偏好。