我正在使用扫描仪逐行读取文本文件但是如何获取行号,因为扫描程序遍历每个输入?我的程序是这样的:
s = new Scanner(new BufferedReader(new FileReader("input.txt")));
while (s.hasNext()) {
System.out.print(s.next());
Run Code Online (Sandbox Code Playgroud)
这工作正常,但例如:
1,2,3 3,4,5
我想知道它的行号,这意味着1,2,3在第1行,而3,4,5在第2行.我怎么做到的?
我有一个包含以下数据的日志文件:
最短路径(2):: RV3280-RV0973C-RV2888C
最短路径(1):: RV3280-RV2502C
最短路径(2):: RV3280-RV2501C-RV1263
最短路径(2):: RV2363-Rv3285-RV3280
从每一行,我需要括号内的数字,第一个蛋白质的名称(第一行中的RV3280)和最后一个蛋白质的名称(第一行中的RV2888C).
我用这个Scanner对象编写了一个代码.
try{
Scanner s = new Scanner(new File(args[0]));
while (s.hasNextLine()) {
s.findInLine("Shortest path\\((\\d+)\\)::(\\w+).*-(\\w+)"); // at each line, look for this pattern
MatchResult result = s.match(); // results from
for (int i=1; i<=result.groupCount(); i++) {
System.out.println(result.group(i));
}
s.nextLine(); // line no. 29
}
s.close();
}
catch (FileNotFoundException e) {
System.out.print("cannot find file");
}
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果,但我也收到了一条错误消息.我得到的上述输入文件的输出是:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at nearnessindex.Main.main(Main.java:29)
2
RV3280
RV2888C …Run Code Online (Sandbox Code Playgroud) import java.util.*;
class Averager
{
public static double unlimited()
{
int count = 0;
double sum = 0;
Scanner scan = new Scanner(System.in);
while(scan.hasNext())
{
double d = scan.nextDouble();
sum += d;
count++;
}
double ave = sum/count;
return ave;
}
public static void main(String[] args) {
System.out.println(unlimited()+"\n");
}
}
Run Code Online (Sandbox Code Playgroud)
使用整数时没有错误,但如果我使用带有点的数字则会出现错误.
$ javac Averager.java; java Averager
0.5
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at Averager.unlimited(Averager.java:12)
at Averager.main(Averager.java:21)
Run Code Online (Sandbox Code Playgroud)
据我所知,0.5应该用双倍覆盖.如果不是,请有人纠正我.
我的程序中有一个扫描程序,它读取部分文件并将其格式化为HTML.当我正在阅读我的文件时,我需要知道如何让扫描仪知道它在一行的末尾并开始写入下一行.
这是我的代码的相关部分,如果我遗漏了任何内容,请告诉我:
//scanner object to read the input file
Scanner sc = new Scanner(file);
//filewriter object for writing to the output file
FileWriter fWrite = new FileWriter(outFile);
//Reads in the input file 1 word at a time and decides how to
////add it to the output file
while (sc.hasNext() == true)
{
String tempString = sc.next();
if (colorMap.containsKey(tempString) == true)
{
String word = tempString;
String color = colorMap.get(word);
String codeOut = colorize(word, color);
fWrite.write(codeOut + " ");
}
else …Run Code Online (Sandbox Code Playgroud) 我正在阅读一个文件,该文件在一行中读取所有内容:
Hello World!\nI've been trying to get this to work for a while now.\nFrustrating.\n
Run Code Online (Sandbox Code Playgroud)
我的扫描仪从文件中读取并将其放入字符串中:
Scanner input = new Scanner(new File(fileName));
String str = input.nextLine();
System.out.print(str);
Run Code Online (Sandbox Code Playgroud)
现在,我希望输出为:
Hello World!
I've been trying to get this work for a while now.
Frustrating.
Run Code Online (Sandbox Code Playgroud)
但相反,我得到了与输入完全相同的东西.也就是说,每个\n都包含在输出中,并且所有内容都在一行而不是单独的行.
我认为Scanner能够正确地读取转义字符,但它反而将它复制到字符串上,就像它的\n一样.
我有一个看似简单的问题,即将逗号分隔String为标记,在以下情况下输出应包含空标记:
String逗号中的第一个字符是逗号.String是逗号.例如,对于String:",abd,def,,ghi,"应该产生输出:{"", "abd", "def", "", "ghi", ""}.
我已经尝试使用String.split,Scanner并且StringTokenizer对于这一点,但每个给出不同的不希望的输出(下文实施例).有人可以建议一个优雅的解决方案,最好使用JDK类吗?显然我可以自己编写代码,但我觉得我错过了上述三种方法之一.请注意,分隔符是固定的,String但不一定是逗号,也不是单个字符.
示例代码
import java.util.*;
public class Main12 {
public static void main(String[] args) {
String s = ",abd,def,,ghi,";
String[] tokens = s.split(",");
System.err.println("--- String.split Output ---");
System.err.println(String.format("%s -> %s", s, Arrays.asList(tokens)));
for (int i=0; i<tokens.length; ++i) {
System.err.println(String.format("tokens[%d] = %s", i, tokens[i]));
}
System.err.println("--- Scanner Output ---");
Scanner sc …Run Code Online (Sandbox Code Playgroud) 我正在使用java.util.Scanner从classpath读取文件内容,使用以下代码:
String path1 = getClass().getResource("/myfile.html").getFile();
System.out.println(new File(path1).length()); // 22244 (correct)
String file1 = new Scanner(new File(path1)).useDelimiter("\\Z").next();
System.out.println(file1.length()); // 2048 (first 2k only)
Run Code Online (Sandbox Code Playgroud)
代码从命令运行(maven测试)
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin/java -Dmaven.home=/usr/share/java/maven-3.0.4 -Dclassworlds.conf=/usr/share/java/maven-3.0.4/bin/m2.conf -Didea.launcher.port=7533 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 12 CE.app/bin" -Dfile.encoding=UTF-8 -classpath "/usr/share/java/maven-3.0.4/boot/plexus-classworlds-2.4.jar:/Applications/IntelliJ IDEA 12 CE.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher --fail-fast --strict-checksums test
Run Code Online (Sandbox Code Playgroud)
它在我的win7机器上完美运行.但在我搬到mac后,同样的测试失败了.我试着谷歌但没有找到多少=(
为什么Scanner with delimiter\Z在win7上将我的整个文件读成一个字符串,但不会在mac上执行?我知道有更多的方法来阅读文件,但我喜欢这个单行,并想了解它为什么不起作用.谢谢.
将输入扫描程序(如键盘)声明为类的全局变量是否被认为是错误的编程习惯?如:
private static Scanner input = new Scanner(System.in);
Run Code Online (Sandbox Code Playgroud)
我正在处理来自各种方法的大量输入,并且似乎很容易将键盘发送到每个方法
什么是ScannerKotlin中的Java ?
我用过readLine()但我想知道它的类型是否安全?
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
Run Code Online (Sandbox Code Playgroud)
我知道,scanner.skip()但我不知道上面这行到底是做什么的。我在很多程序中都看到过,但我不知道到底发生了什么。有人能帮我吗?