dimanche 11 septembre 2016

New to Java -- Return Unit test failing

Im working on a Java program in class and im unsure why or how this is failing considering I get the correct print out when the program is ran.

Here is the code I have:

import java.util.Scanner;
    public class TextAnalyzer
  {
    // Create scanner
    private static Scanner scnr = new Scanner(System.in);
    // Create text string
    private static String text;
    public static void main(String[] args)
    {
        System.out.println("Enter a sentence or phrase: ");
        // Scan for text input
        text=scnr.nextLine();

        System.out.println("You entered: "+ text);

        // Call getNumOfCharacters
        int count = getNumOfCharacters();
        System.out.println();
        System.out.println("Number of characters: " + count);
        // Call outputWithoutWhitespace
        String modifiedstring = outputWithoutWhitespace();
        System.out.println("String with no whitespace: " + modifiedstring);

    }
        // Method outputs string without spaces
        private static String outputWithoutWhitespace()
    {
        text=text.trim().replaceAll(" ","");
        return text;
    }
        // Method to return number of characters
        private static int getNumOfCharacters()
    {
        return text.length();
    }
  }

The output passes on all levels, it's the Unit test for the number of characters in the input that is failing and I really just need some guidance as a new student to Java programming.

Here is a printout of the tests: Pass/Fail image

My assumption is that the getNumOfCharacters() is returning the number of characters AFTER the whitespaces have been removed, but I could be wrong.

Any help/guidance would be greatly appreciated!!!

Aucun commentaire:

Enregistrer un commentaire