0

My code works except I have to make it pass some Junit test. It passes all but one. It passes when the character enters nothing, enters upper case, lower case, or a mix of the two, and it works when the enter Hello World!

public static void main(String[] args)  {
    Scanner input = new Scanner(System.in);
    int[] textArray = new int[26];

    System.out.print("Enter text: ");
    readText(input, textArray);
}

public static void readText(Scanner input, int[]text){
    char letter = 0;

    if (input.hasNext() == false) {
        System.out.println();
    }
    else {
        while (input.hasNext()) {
            String a = input.nextLine();

            for (int i = 0; i < a.length(); i++) {
                letter = a.charAt(i);

                if (letter >= 'A' && letter <= 'Z') {
                    text[letter-65] = (text[letter-65]) + 1;
                }
                else if (letter >= 'a' && letter <= 'z') {
                    text[(letter - 32) - 65] = (text[(letter - 32) - 65]) + 1;
                }
                else if (letter == ' ') {
                    System.out.print("");
                }
                else {
                    System.out.print("");
                }
            }
        }
    }

    String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    for (int y = 0; y < text.length; y++) {
        if (text[y] > 0) {
            System.out.println(alphabet[y] + ": " + text[y]);
        }
    }
}

The Junit tests this input: 1 2 3%n! ? >%n:) !!%n

The expected output is

Enter text: 
    //empty line here

But instead the output from my code is

Enter text: //with no line after

I'm not sure how to get the extra line after without ruining my other junit tests. I tried one way and it worked but then my Hello World didn't work properly.

And example of when its working:
When I hit run the console will say

Enter text:

I have a Scanner input so the user will enter some words and it will look like this in console

Enter text: sOme wordS

Then it will count the number of times each letter was used and print that to console like this

Enter text: sOme wordS
D: 1
E: 1
M: 1
O: 2
R: 1
S: 2
W: 1

If I don't enter anything when asked and just hit the enter key the output is

Enter text:
//empty line here

But when I enter

Enter text: 1 2 3
? ! >
:) !!

The output doesn't add an extra line at the end.

7
  • Use System.out.println("Enter text: ") instead of System.out.print("Enter text: "). Commented Nov 11, 2016 at 17:03
  • System.out.println("Enter text: " + "\n"); You want a new line after "Enter text". If you also want to tab it, you can use System.out.println("Enter text: " + "\n\t"); Commented Nov 11, 2016 at 17:08
  • @return0 Can't do that because user input has to be on the same line as "Enter text: " Commented Nov 11, 2016 at 17:11
  • @JaynineReturns I thought you wanted the expected output with a new line... Not sure what you mean by user input. Commented Nov 11, 2016 at 17:13
  • Where does the user input come in at this line? Before the Enter text? @JaynineReturns Enter text: //empty line here Commented Nov 11, 2016 at 17:21

1 Answer 1

1

Try using System.out.println("Enter text: "); instead of System.out.print("Enter text: ");

Update: After getting clarified your requirement I propose to use the following code.

public static void readText(Scanner input, int[]text){
        char letter = 0;

        while (input.hasNext()) {
            String a = input.nextLine();

            for (int i = 0; i < a.length(); i++) {
                letter = a.charAt(i);

                if (letter >= 'A' && letter <= 'Z') {
                    text[letter-65] = (text[letter-65]) + 1;
                }
                else if (letter >= 'a' && letter <= 'z') {
                    text[(letter - 32) - 65] = (text[(letter - 32) - 65]) + 1;
                }
                else if (letter == ' ') {
                    System.out.print("");
                }
                else {
                    System.out.print("");
                }
            }
        }

        String[] alphabet = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

        boolean emptyOutput = true;
        for (int y = 0; y < text.length; y++) {
            if (text[y] > 0) {
                System.out.println(alphabet[y] + ": " + text[y]);
                emptyOutput = false;
            }
        }
        if (emptyOutput) {
            System.out.println();
        }
    }
Sign up to request clarification or add additional context in comments.

6 Comments

I tired that already. I cannot do that because then when the user enters text it will be on the line under "Enter text: " when its supposed to be on the same line.
why don't you add a System.out.println(); after the line that you read input String a = input.nextLine();
I edited the post. The last sections shows what the output should be like
The only thing you added is the code under where I declared my alphabet array correct? I'm just trying to understand how that wouldn't add an extra line for when I do enter words. It worked though thank you very much! I'm just trying understand how it did is all haha.
Oh I see now! That took me a second but I understand now. Yeah that was my problem I wasn't understanding how I can check if the amount of letters used is 0. But now I get it. That was a simple fix wow I don't know why I was blanking. Thank you so much!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.