Java 我的程序执行';我无法读取txt文件中的第二行

Java 我的程序执行';我无法读取txt文件中的第二行,java,Java,我需要提示用户输入txt文件名,然后读取文件中的内容。我的程序似乎工作正常唯一的事情是它只读取我的txt的第一行 这是我的两行 38 4 19-27-15-3 4 19 38 14 7-10 9-18-10 17 42 98 我必须添加另一个扫描仪才能读取第二行吗?有人能帮帮我吗 import java.io.*; import java.util.*; public class NegativeSum{ public static void main (String [] args

我需要提示用户输入txt文件名,然后读取文件中的内容。我的程序似乎工作正常唯一的事情是它只读取我的txt的第一行

这是我的两行

38 4 19-27-15-3 4 19 38

14 7-10 9-18-10 17 42 98

我必须添加另一个扫描仪才能读取第二行吗?有人能帮帮我吗

import java.io.*;
import java.util.*;

public class NegativeSum{
    public static void main (String [] args )
    throws FileNotFoundException{

Scanner console = new Scanner ( System.in);
System.out.println("enter  a name of a file " );
String name = console.nextLine();
Scanner input = new Scanner ( new File (name));



        negativesum(input);


        }//end of amin

public static boolean negativesum(Scanner input)
throws FileNotFoundException{

    int sum=0;
    int count = 0;

    while ( input.hasNextInt()){
        int next =input.nextInt();
        sum+=next;
        count++;

        if ( sum<0){
            System.out.println("sum of " + sum + " after " + count + "steps" );
            return true;
            }

        }///end of while
    System.out.println("no negative sum ");
    return false;




    }//end of metho d

}//end of main
import java.io.*;
导入java.util.*;
公共类负片{
公共静态void main(字符串[]args)
抛出FileNotFoundException{
扫描仪控制台=新扫描仪(System.in);
System.out.println(“输入文件名”);
字符串名称=console.nextLine();
扫描仪输入=新扫描仪(新文件(名称));
负离子(输入);
}//阿明末
公共静态布尔值negativesum(扫描仪输入)
抛出FileNotFoundException{
整数和=0;
整数计数=0;
while(input.hasNextInt()){
int next=input.nextInt();
总和+=下一个;
计数++;

如果(sum当前代码读取文件的第二行


System.out.println(“在“+count+”步数之后的“+sum+”之和”);
将在总和小于0的情况下打印目前,数字总和不小于0,只需添加一个数字,使总和小于0,并打印预期输出

您不需要第二个
扫描仪
。我建议您阅读该文件

逐行解析每一行,例如使用
字符串。拆分

返回true;当第一个值小于零时,使“if”循环返回,因此它只读取第一行。

如下设置程序

while(input.hasNext()) {
    if(input.hasNextInt())
        // Do your thing
    else
        input.next(); // Discard non-integer tokens
}

我建议您在后面添加一个简单的打印语句

  int next = input.nextInt();
比如:

当我测试您的代码时,它打印:

38 0 4 38 19 42 -27 61 -15 34 -3 19 4 16 19 20 38 39 14 77 7 91 -10 98 9 88 -18 97 -10 79 17 69 42 86 98 128 no negative sum 38 0 4 38 19 42 -27 61 -15 34 -3 19 4 16 19 20 38 39 14 77 7 91 -10 98 9 88 -18 97 -10 79 17 69 42 86 98 128 没有负和
因此,我看不出您的代码有任何错误。

我接受了您的代码,它按原样工作。在给定输入的18个步骤后,您将得到226的总和。 只需将System.out.println语句置于if条件之外,然后查看您得到的结果。

公共静态布尔值negativesum(扫描仪输入)
public static boolean negativesum(Scanner input)
        throws FileNotFoundException {

    int sum = 0;
    int count = 0;

    while (input.hasNext()) {
        if (input.hasNextInt()) {
            sum += input.nextInt();
            ++count;
        } else
            input.next();
    } // /end of while

    System.out.println("Numbers parsed: " + count + "\nTotal Sum: " + sum);
    return sum < 0;

} // end of method
抛出FileNotFoundException{ 整数和=0; 整数计数=0; while(input.hasNext()){ if(input.hasNextInt()){ sum+=input.nextInt(); ++计数; }否则 input.next(); }///时间结束 System.out.println(“解析的数字:“+count+”\n总和:“+Sum”); 返回和<0; }//方法结束
目前,您的代码没有将换行符识别为任何特殊内容;这意味着您的输入被视为一条长线。从这个意义上说,它“有效”,但您的问题暗示这不是您想要的

我猜您希望每一行都被单独处理,并且程序在每一行的末尾“重置”

实现这一点的一种方法是使用一个扫描仪逐行读取文件,然后对于每一行,使用第二个扫描仪读取该行的内容

Scanner input = new Scanne(new File (name));
while (input.hasNextLine()) {
    String line = input.nextLine();
    negativesum(new Scanner(line));
}

然后,您可以保持您的
negativesum
方法与当前完全相同。

正如您所建议的,您只调用scanner.nextLine()一次。这将只读取第一行。您需要某种循环构造来读取其他行(许多选项可用)。@user1676075:代码从未调用
scanner.nextLine()
nextLine
调用与
扫描仪
无关。它在我的系统上正常工作。您是否将数字打印为system.out.println(“总和”+next);int next=input.nextInt();@Yoana
Scanner
默认情况下忽略换行符。因此换行符不是标记,因此没有必要放弃它。删除了错误声明。Thanx。为什么您认为逐行读取文件并使用
String.split
(以及
parseInt
)与使用
扫描仪相比有什么改进吗?这是如何回答问题的?没问题。很高兴能够提供帮助。
Scanner input = new Scanne(new File (name));
while (input.hasNextLine()) {
    String line = input.nextLine();
    negativesum(new Scanner(line));
}