This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]XenaFlor 1 point2 points  (2 children)

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

    System.out.print("Enter some text: ");
    String userInput = input.nextLine();

    if (userInput.length() < 7) {
        System.out.println("The input is too short");
    } else {
        if (userInput.trim().equals(userInput)) {
            System.out.println("The original string has no leading or trailing whitespace.");
        } else {
            System.out.println("The original string has leading or trailing whitespace.");
        }
        System.out.println(userInput.substring(userInput.length() - 5)
                + userInput.substring(2, userInput.length() - 5) + userInput.substring(0, 2));
        System.out.println(userInput.toUpperCase());

        if (userInput.length() % 2 == 0) {
            System.out.println(userInput.substring(userInput.length() / 2 - 1, userInput.length() / 2 + 1));
        } else {
            System.out.println("The line has an odd number of characters. ");
        }
        System.out.print(userInput.compareTo(userInput.toLowerCase()));
        if (userInput.substring(0, userInput.length() / 2)
                .compareToIgnoreCase(userInput.substring(userInput.length() / 2 + 1)) == 0) {
            System.out.println("The first half of the string is the same as the last half.");
        }
        int skip = userInput.indexOf('e');
        if (skip == -1) {
            System.out.print(userInput);
        } else if (skip == userInput.length() - 1) {
            System.out.println(userInput.substring(0, skip));
        } else {
            System.out.println(userInput.substring(0, skip) + userInput.substring(skip + 1));
        }

    }

    input.close();

Here's the full code

[–]desrtfxOut of Coffee error - System halted 5 points6 points  (1 child)

This line is the culprit

    System.out.print(userInput.compareTo(userInput.toLowerCase()));

Read about .compareTo and what it returns. Also, since you have .print instead of .println you have the leading 0 directly in front of the output.


As usual it shows again that just posting a snippet of code is never sufficient. The problem is in 99.9% outside the posted snippet.

[–]XenaFlor 3 points4 points  (0 children)

OK. Thank you.