-2

My code is relatively simple. I have to create a program that takes the input of a string, and tracks the frequency of each string. As well as the user should be able to type in a line, click enter and be able to enter the next line, etc. until the user clicks enter twice in a row. Here is my code so far:

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 ;
    }
}
}

Netbeans isn't giving me any syntax errors meaning there isn't any exception stack trace. I'm guessing the issue is between the replaceAll line and the null statements in my while loop (that allows the user to click the enter key twice before the program runs.

I'm very new to Java, so sorry for any 'rookie mistakes'

Thanks!

Edit: My apologies, I thought the exception stack trace was the red text you get when you run your program with syntax errors (missing a semicolon, etc.)

I mean that my program runs, but it gives me NaN instead of the percentage amount, but when I remove the whole while loop, the program works perfectly without the next line feature.

  • 2
    "Netbeans isn't giving me any syntax errors meaning there isn't any exception stack trace" That's completely wrong. In fact, it's impossible to ever get an exception stack trace if you do have syntax errors, since syntax errors would prevent you from starting your program, and exceptions can't be thrown from a program that never started running. – Joseph Sible-Reinstate Monica Nov 18 '19 at 03:52
  • What is your `while` loop trying to achieve? It looks like you've already got the sentence, before it starts. – Dawood ibn Kareem Nov 18 '19 at 03:54
  • `String sentence = input.replaceAll("\\W", "");` this might cause **NPE** problem – flyingfox Nov 18 '19 at 03:55
  • Yeah, it's guaranteed to, since we can only get to that line once `input` is `null`. – Dawood ibn Kareem Nov 18 '19 at 03:57
  • Did you mean to write `while (input == null)` instead? (And of course change some other parts of the code...) It seems your goal is to get valid input, and you're using `null` to mean "not valid", so not valid would mean keep waiting => null means keep looping? – Dan Getz Nov 18 '19 at 03:57
  • My while loop allows the user to click the enter key and allow them to type on the next line instead of the program running. So the program will only run when the enter key is clicked twice in a row. – Computer Programmer Nov 18 '19 at 04:00
  • I meant to put != null, I got the while loop line of code from another forum on how to use the enter key twice (https://stackoverflow.com/questions/18281543/java-using-scanner-enter-key-pressed) – Computer Programmer Nov 18 '19 at 04:04
  • @DanGetz `while (input == null)` makes no sense here. If it is null it will continue to be null and nothing will ever happen. – user207421 Nov 18 '19 at 08:48

1 Answers1

-1

The issue appears to be your loop for reading input. Your while loop won't exit until you set your string to null, which only happens when piping input to stdin. I believe if you want to only continue when the string is both not null and not empty, an example solution would be:

while(true){
    if(input != null){
        if(!input.isEmpty()){
            break;
        }
    }
    input = sc.nextLine();
}

Edit: Apologies did not fully understand what you were trying to achieve and instead assumed.

replacing the snippet above with:

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();

should get the behaviour you were after

Seamus
  • 131
  • 1
  • 7
  • I tried your line of code, it shows me the percentages but it doesn't let me go to the next line. Ex. I type in "Rocks are cool" I click the enter key once, the cursor goes to the next line, I type in "Sand is not cool" and double click Enter, and then the program tells me the frequency of each letter. – Computer Programmer Nov 18 '19 at 04:10
  • If the next input is null you must break. Neither of these pieces of code does that. – user207421 Nov 18 '19 at 07:53
  • definitely not specified but a fairly straightforward fix – Seamus Nov 18 '19 at 08:06
  • It doesn't have to be specified. It is *required* so as to avoid an NPE in the subsequent code. it should in fact be the controlling condition of the `while`: `while ((nextInput = sc.nextLine()) != null)`.Nothing is forever. – user207421 Nov 18 '19 at 08:46
  • you avoid the NPE by consistently looping until it is not null, as they specified the exit condition to be when a new line is received without entry. If truly splitting hairs it should be Throwing an exception in the case of a null value as this is undefined behaviour. – Seamus Nov 18 '19 at 08:56
  • You can't 'consistently loop until it is not null'. Once it is null it will stay null. And none of the code you posted does that anyway. – user207421 Nov 18 '19 at 09:15
  • All of this is redundant as the only time the value of `nextInput` could be null is when an exception is thrown from `sc.nextLine()`, as that exception is not handled, there is no case where nextInput can be null where a value is expected. Furthermore implementing a check on this is trivial and once again none of this was defined. – Seamus Nov 18 '19 at 09:42