ylimani80
ylimani80

Reputation: 29

When and why to use a number as a String input?

I have been reading a java book where I came across a piece of code where a fake input is provided in order to test the main class. The fake input is a String and later converted into int primitive type using the Integer.parseInt() method. Following are the two classes:

The main class

public class SimpleDotCom{
    int[] locationCells;
    int numOfHits = 0;

    public void setLocationCells(int[] locs){
        locationCells = locs;
    }

    public String checkYourself(String stringGuess){
        int guess = Integer.parseInt(stringGuess);
        String result = "miss";

        for(int cell : locationCells){
            if(guess == cell){
                result = "hit";
                numOfHits++;
                break;
            }
        }

        if(numOfHits == locationCells.length){
            result = "kill";
        }
        System.out.println(result);
        return result;
    }
}

The test class

public class SimpleDotComTestDrive{
    public static void main(String[] args){
        SimpleDotCom dot = new SimpleDotCom();
        int[] locations = {2,3,4};
        dot.setLocationCells(locations);
        String userGuess = "4";
        String result = dot.checkYourself(userGuess);
    }
}

I tried the same example with an int variable and it worked:

public class DotCom{
    int[] locationCells;
    int numOfHits = 0;

    public void setLocationCells(int[] locs){
        locationCells = locs;
    }

    public String checkYourself(int stringGuess){
        //int guess = Integer.parseInt(stringGuess);
        int guess = stringGuess;
        String result = "miss";

        for(int cell : locationCells){
            if(guess == cell){
                result = "hit";
                numOfHits++;
                break;
            }
        }

        if(numOfHits == locationCells.length){
            result = "kill";
        }
        System.out.println(result);
        return result;
    }
}

public class DotComTestDrive{
    public static void main(String[] args){
        DotCom dot = new DotCom();
        int[] locations = {2,3,4};
        dot.setLocationCells(locations);
        int userGuess = 4;
        String result = dot.checkYourself(userGuess);
        //System.out.println("result2 = "+result);
    }
}

I understand the code, but the question is why to have an input as a String where as the same result can be achieved by using an int variable.

I am not sure if I am missing something in understanding or may be my question is completely wrong, but as I tried the same example with an int variable and it worked, I keep wondering when and why to have inputs as a String?

Upvotes: 2

Views: 128

Answers (2)

Heri
Heri

Reputation: 4588

Internally in your program it is recommended to use the strongest possible type, in your case use the int.

But your program has an outside border where data come in and go out, like user input, feedback on the console, I/O operations to files or network, etc. In many cases you have to convert the involved data to or from types which are defined by the interface specification. An XML or json structure transports each data as a string, including numbers and dates. As soon as it passes the border of your application it should be immediately converted to the internally used type (and - of course - checked if the conversion is valid).

The background to this is: Serializing and deserializing (network streams, files) is easier done (better interchangable) using strings. But inside a process (by the CPU) it is better to operate on binary representations of the data.

Upvotes: 4

Isha Agarwal
Isha Agarwal

Reputation: 410

If the number is large enough and can't be inside the integer or long range.

Also, beneficial for solving these problems in an efficient way

1) When you want to check if a number is a palindrome or not in an efficient way.

2) When you want to know digits of the number without division operator.

Few examples Which I am able to recollect now.

Upvotes: 0

Related Questions