jacob schiffer jacob schiffer - 2 months ago 97
Java Question

Why am I getting this output discrepancy in the JVM?

I am using a cloud based Java IDE and I would like to have my output match. I have had success with every part/input except for this one:

Image of the issue

I believe it may have something to do with the case "s" but I am not sure.It could also be the shortenSpace() method. This is how the question reads:


Implement the shortenSpace() method. shortenSpace() has a string
parameter and returns a string that replaces all sequences of 2 or
more spaces with a single space. shortenSpace() DOES NOT output the
string. Call shortenSpace() in the main() method, and then output the
edited string.


Here is my code

import java.util.Scanner;

public class AuthoringAssistant {
// Creates an instance of Scanner class object
private static Scanner scanner = new Scanner(System.in);

/**
*
* The method that replaces two or more sapces with single spaces
* */
private static String shortenSpace(String text) {
String temp = text.trim().replaceAll(" +", " ");
return temp;

}

/**
* The method replaces all exclamation marks with character '.'
* */
private static String replaceExclamation(String text) {
String temp = text.replaceAll("!", ".");
return temp;
}

/**
* The method findText that finds the number of occurenence of find in text
* and returns the count
* */
private static int findText(String text, String find) {
int count = 0;
int i = 0;

while ((i = text.indexOf(find)) != -1) {
text = text.substring(i + find.length());
count += 1;
}

return count;
}

/**
* the method takes a string and returns the number of words in the text.
* */
private static int getNumOfWords(String text) {
// call split method that returns the array of words
// split by spaces
text = shortenSpace(text);
String[] words = text.split(" ");

// return count of words
return words.length;
}

/*
* The method getNumOfNonWSCharacters that remmoves the whitepsaces and
* returns the lenght of the text
*/
private static int getNumOfNonWSCharacters(String text) {
text = text.trim().replaceAll("\\s", "");
return text.length();
}

/**
* The method printMenu that prints a menu of choices and prompts user to
* enter choice.
* */
private static void printMenu() {
System.out.println("\nMENU");
System.out.println("c - Number of non-whitespace characters");
System.out.println("w - Number of words");
System.out.println("f - Find text");
System.out.println("r - Replace all !'s");
System.out.println("s - Shorten spaces");
System.out.println("q - Quit\n");
System.out.println("Choose an option: ");
}

public static void main(String[] args) {
System.out.println("Enter a sample text: ");
String text = scanner.nextLine();
System.out.println("You entered: " + text);
while(true) {
//prompt for the text


//Display user input


//Display menu
printMenu();
char ch = scanner.nextLine().charAt(0);

switch (ch) {
case 'q':
// close the program
// System.out.println("Exit the program");
System.exit(0);

case 'c': // Number of non-whitespace characters
int cntNonWhitespaces = getNumOfNonWSCharacters(text);
System.out.print("Number of non-whitespace characters: " + cntNonWhitespaces);
break;

case 'w': // Number of words
int wordsCount = getNumOfWords(text);
System.out.print("Number of words: " + wordsCount);
break;

case 'f': // Find text
System.out.println("Enter a word or phrase to be found: ");
String find = scanner.nextLine();
int findCount = findText(text, find);
System.out.print("\"" + find + "\" instances: " + findCount);
break;

case 'r': // Replace all !'s
String newstring = replaceExclamation(text);
System.out.println("Edited text: " + newstring);
break;

case 's': // Shorten spaces
String newspace = shortenSpace(text);
System.out.print("Edited text:" + newspace+ " ");
break;

default:
System.out.println("Invalid option. Please try again");
}

System.out.println();
}
}
}


This is the input I am using

There are too many spaces here. Why?
s
q


My output and expected output is in the image.

Answer

As per the comment by @immibis

private static String shortenSpace(String text) {
       String temp = text.replaceAll(" +", " ");  // no trim required
       return temp;

   }

also

 System.out.print("Edited text: " + newspace);