-1

I am trying to check for integers, "" and " " when a user inputs a string. Every thing is working as expected but when I pass an empty field it validates and prints invalid input. Apart from that it also prints the println statement from the catch. Below is my code and actual output.

package com.assessment;

import java.util.Scanner;

public class MaxCharString {

    public static void main(String[] args) {
        int i, j, count = 0, maxcount = 0;
        System.out.println("Enter a string to find the maximum character.");
        Scanner input = new Scanner(System.in);
        String character = input.nextLine().toLowerCase();
        try {
            if (character == "") {
                System.out.println("Invalid Input");
            }
            int inputCharacter = Integer.parseInt(character);
            System.out.println(inputCharacter + " is not a valid Input. Please try again.");
        } catch (NumberFormatException e) {
            char result = 0;
            char ch[] = character.toCharArray();
            for (i = 0; i < ch.length; i++) {
                count = 0;
                for (j = 0; j < ch.length; j++) {
                    if (ch[i] == ch[j]) {
                        count++;
                    }
                    if (maxcount < count) {
                        maxcount = count;
                        result = ch[i];
                    }
                }
            }
            System.out.println("Maximum count of occuring character in the string is: " + result);
            input.close();
        }
    }
}

Output:

Invalid Input
Maximum count of occuring character in the string is:  

Could anyone please help me out with this problem. Thank you.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
Sam
  • 62
  • 8

1 Answers1

0

Your problem will be the equality sign.

Strings have to be checked with your_string.equals("").

If you are checking individual characters, you have to use single quotes and double equals. E.g. your_char = ' '.

This whole thing is a bit off then as you're checking a string, not a character, and you also used the wrong equals.

    String character = input.nextLine().toLowerCase();
    try {
        if (character == "") {
            System.out.println("Invalid Input");
        }

Fix it like:

    //Better name.
    String line = input.nextLine().toLowerCase();
    try {
        if (line.equals("")) {
            System.out.println("Invalid Input");
        }

This is also kind of odd as you normally wouldn't use the catch for real logic. Catch is for an accidental error. If you are branching for valid tasks, use an if-else.

FYI, easiest way to debug this is to hop into the debugger and step line by line to see where it is and what is unexpected.

Why Did It Reach the Catch?

As the comments pointed out, you are trying to parse an empty value into an integer which will not work. Also, you should log the exception when you get into it so you see "why" you got into the catch. e.printStackTrace() would make it obvious for you if you added it as the first line in the catch.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • It doesn't work with string.equals("") as well. I am not getting why it is getting inside catch block. – Sam May 24 '21 at 23:58
  • 1
    @SambidRijal it's getting inside the catch because of this line in the try scoop ```int inputCharacter = Integer.parseInt(character);``` you're trying to parse a string that can be empty. – Oussama Makhlouk May 25 '21 at 00:04