Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么不是';我的代码不能按预期工作吗?我认为空指针异常存在问题。(爪哇)_Java - Fatal编程技术网

Java 为什么不是';我的代码不能按预期工作吗?我认为空指针异常存在问题。(爪哇)

Java 为什么不是';我的代码不能按预期工作吗?我认为空指针异常存在问题。(爪哇),java,Java,我的代码相对简单。我必须创建一个程序,接收字符串的输入,并跟踪每个字符串的频率。除了用户应该能够键入一行之外,单击enter并能够输入下一行,等等,直到用户在一行中单击enter两次。以下是我目前的代码: package javaapplication8; import java.util.Scanner; public class JavaApplication8 { static Scanner sc = new Scanner (System.in); public static

我的代码相对简单。我必须创建一个程序,接收字符串的输入,并跟踪每个字符串的频率。除了用户应该能够键入一行之外,单击enter并能够输入下一行,等等,直到用户在一行中单击enter两次。以下是我目前的代码:

package javaapplication8;

import java.util.Scanner;

public class JavaApplication8 {

static Scanner sc = new Scanner (System.in);

public static void main(String[] args) {
    System.out.print("Enter a sentence: ");
    String input = sc.nextLine() ;
    while (input!=null) {
        if (input.isEmpty()){
            System.out.println("Read Enter Key.");
            break;
        } else if (sc.hasNextLine()){
            input = sc.nextLine();
        } else {
            input = null;
        }
    }
    String sentence = input.replaceAll("\\W", "");
    sentence = sentence.toUpperCase();
    int [] frequencies = new int [26];
    int value = 65;
    double valuecount = 0;

    for (int i = 0; i < 26 ; i++) {
        for (int i2 = 0; i2 < sentence.length(); i2++){
            if (sentence.charAt(i2) == value){
                valuecount += 1 ;
            }
        }
        double percent = (valuecount / sentence.length() ) * 100;
        char val = (char) value;
        System.out.format (val + " occured %.2f percent of the time. \n", + percent);
        value += 1;
        valuecount =0 ;
    }
}
}
包javaapplication8;
导入java.util.Scanner;
公共类JavaApplication8{
静态扫描仪sc=新扫描仪(System.in);
公共静态void main(字符串[]args){
System.out.print(“输入句子:”);
字符串输入=sc.nextLine();
while(输入!=null){
if(input.isEmpty()){
System.out.println(“读取输入键”);
打破
}else if(sc.hasNextLine()){
输入=sc.nextLine();
}否则{
输入=空;
}
}
字符串语句=input.replaceAll(“\\W”和“);
句子=句子.toUpperCase();
int[]频率=新int[26];
int值=65;
双值计数=0;
对于(int i=0;i<26;i++){
for(int i2=0;i2<句子长度();i2++){
if(句子字符(i2)=值){
valuecount+=1;
}
}
双百分比=(valuecount/句子长度())*100;
char val=(char)值;
System.out.format(val+“发生%.2f时间的百分比。\n”,+percent);
数值+=1;
valuecount=0;
}
}
}

Netbeans没有给我任何语法错误,这意味着没有任何异常堆栈跟踪。我猜问题出在replaceAll行和while循环中的null语句之间(这允许用户在程序运行之前单击enter键两次)

我对Java非常陌生,因此对于任何“新手错误”感到抱歉

谢谢

编辑:很抱歉,我认为异常堆栈跟踪是在语法错误(缺少分号等)下运行程序时得到的红色文本


我的意思是,我的程序运行,但它给我的是NaN而不是百分比,但是当我删除整个while循环时,程序在没有下一行功能的情况下运行得非常好。

问题似乎是读取输入的循环。while循环将不会退出,直到您将字符串设置为null,这仅在将输入管道化为std时才会发生在中,我相信如果您只想在字符串不为null且不为空时继续,示例解决方案如下:

while(true){
    if(input != null){
        if(!input.isEmpty()){
            break;
        }
    }
    input = sc.nextLine();
}
编辑: 道歉并没有完全理解你想要达到的目的,而是假设了

将上面的代码段替换为:

StringBuilder inputBuilder = new StringBuilder();
String nextInput = sc.nextLine();
while(true){
    if(nextInput != null){
        if(nextInput.isEmpty()){
            break;
        }
        inputBuilder.append(nextInput);
    }
    nextInput = sc.nextLine();
}
String input = inputBuilder.toString();

“Netbeans没有给我任何语法错误,这意味着没有任何异常堆栈跟踪”这是完全错误的。事实上,如果你确实有语法错误,就不可能获得异常堆栈跟踪,因为语法错误会阻止你启动程序,并且不能从从未开始运行的程序中抛出异常。当循环尝试实现时,你的
是什么?看起来你已经获得了这可能会导致NPE问题是的,这是有保证的,因为我们只能在
input
null
时才能到达那一行。你是不是想在(input==null)
时写
似乎你的目标是获得有效的输入,你用
null
来表示“无效”,所以无效意味着继续等待=>null意味着继续循环?我试过你的代码行,它显示了百分比,但不让我进入下一行。例如,我键入“Rocks is cool”我点击回车键一次,光标转到下一行,我输入“沙子不酷”然后双击Enter,然后程序告诉我每个字母的频率。如果下一个输入为null,则必须中断。这两段代码都不会这样做。肯定没有指定,但相当简单的修复方法不必指定。它是必需的,以避免在后续代码中出现NPE。它实际上应该是
while的滚动条件:
while((nextInput=sc.nextLine())!=null)
。没有什么是永恒的。您可以通过持续循环来避免NPE,直到它不为null,因为他们指定了在没有输入的情况下接收新行时的退出条件。如果确实存在分歧,则应在null值的情况下引发异常,因为这是未定义的行为。