Ninja2k
Ninja2k

Reputation: 879

Why do these two methods not work correctly in a single print statement?

I am using two built in methods, charAt() and getNumericValue() to get the character of a string and its numerical value, when I try to use both methods in the same println() it gives me an incorrect result but when I put them in separate print methods they work fine. Although my example works I am curious as to why this is?

This gives the correct result

            {
                // dataStream is a String taken from the user. e.g. "Ninja"
                // Take in dataStream, calculate the value of each character.
                for ( int i = 0; i < dataStream.length() ; i++ )

                    {
                        System.out.print ( dataStream.charAt(i) ) ; // Print the Character at i,
                        System.out.print( Character.getNumericValue ( dataStream.charAt(i) ) ) ;    // Print the value of the character at i.
                        System.out.println ( " ") ; // Tidy
                    }


            }

This doesn't

            {

                // dataStream is a String taken from the user. e.g. "Ninja"
                // Take in dataStream, calculate the value of each character.
                for ( int i = 0; i < dataStream.length() ; i++ )

                    {

                       System.out.print ( dataStream.charAt(i) + Character.getNumericValue ( dataStream.charAt(i) )  ) ;
                       System.out.println ( " ") ; // Tidy

                    }


            }

Upvotes: 0

Views: 43

Answers (4)

user3437460
user3437460

Reputation: 17454

That is because the + operators behaves differently depending on the given types. This should give you some idea what is going on:

System.out.println(charType + charType);    //Addition occurs
System.out.println(intType + charType);     //Addition occurs
System.out.println(StringType + charType);  //Concatenation occurs
System.out.println(charType + StringType);  //Concatenation occurs
System.out.println(StringType + intType);   //Concatenation occurs
System.out.println(intType + StringType);   //Concatenation occurs

System.out.println('A' + 'A');       //prints 130 (65+65)
System.out.println(5 + 'A');         //prints 70 (65+5)
System.out.println("" + 'B');        //prints "B"
System.out.println('B' + "");        //prints "B"
System.out.println("" + 7);          //prints "7"
System.out.println(7 + "");          //prints "7"
System.out.println("" + 'A' + 'B');  //prints "AB"

Upvotes: 1

user3437460
user3437460

Reputation: 17454

when I try to use both methods in the same println() it gives me an incorrect result but when I put them in separate print methods they work fine.

It is because in your second approach, you are printing the sum of the ascii value and int value of "ninja" letter by letter due to the + operator within println().

You may overcome that by appending a string literal before the character:

System.out.print ("" + dataStream.charAt(i) + Character.getNumericValue ( dataStream.charAt(i)));

Upvotes: 2

nail fei
nail fei

Reputation: 2329

the return type of Character.getNumericValue() method is int type and charAt() return char type.


then the value of char type add int type return a value ofint type

Upvotes: 0

SJuan76
SJuan76

Reputation: 24895

Because + only works as concatenations when one of the elements is a String, and what you have is a char and an int value.

So, it promotes (converts) the char to int, adds both numbers and that is what it prints.

You may:

  • Convert to String both the char (Character.toString(char)) and the int (Integer.toString(int)), or one of them.

  • Add a String in the middle dataStream.charAt(i) + "" + Character.getNumericValue(dataStream.charAt(i)). It would be decomposed as (dataStream.charAt(i) + "") + Character.getNumericValue(dataStream.charAt(i)). The first part will evaluate as String concatenation (because of "") and the rest will be also String concatenation (because the result of the first part will be a String). As a side benefit, use an empty space or a symbol to separate both values.

Upvotes: 2

Related Questions