3

It is my first time using stackoverflow! I am a beginner and I will try to write my problem as short as possible;

User puts a number e.g 1234

char NUMBER = In.read();
while (NUMBER != '\n') {
    Out.println(NUMBER);    
    NUMBER = In.read();
}

The Program will put out the numbers as "1" "2" "3" "4", so far so good.

Now I'd like to multiply these numbers with a number I choose.

When trying this, the program says that it's impossible, because you cannot multiply "char" with "int".

How can I solve this problem? How do I multiply these numbers?

Thank you in advance!

I tried using the ASCII, but that also does not work when trying to multiply.

QBrute
  • 4,405
  • 6
  • 34
  • 40
  • 1
    There's a method [`Character.getNumericValue(char)`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/Character.html#getNumericValue(char)) that should do the trick. – QBrute Apr 11 '23 at 12:52
  • 1
    Bear in mind that `getNumericValue(char)` may [surprise you](https://stackoverflow.com/q/31888001/12567365) - depending on the inputs. – andrewJames Apr 11 '23 at 13:21
  • 1
    Also consider Integer.parseInt in case you want to convert the result of combining multiple characters into a number. – N Sarj Apr 11 '23 at 13:37
  • You could cast the char variable: int var = (int) NUMBER; and then do the multiplication. – fer.trecool Apr 11 '23 at 14:19
  • 1
    But remember that the char variable will take the ASCII value, for example, if the char value is 1 and you multiply it by 5, the result will be 245, because 1 in ASCII is 49(decimal) . – fer.trecool Apr 11 '23 at 14:24
  • I believe the generic term for what you're looking to do is "parse". E.g. try searching for "parse number java". – pamphlet Apr 11 '23 at 17:56

1 Answers1

0

Code points

While char can be made to work for you, that is a legacy data type. As a 16-bit value, char is physically incapable of representing most characters.

So I suggest you make a habit of using code point integer numbers when working with individual characters.

int[] codePoints = "1234".codePoints().toArray() ;

Loop each code point. Test if that code point represents a character that is a digit. Change each code point back to text. Parse that text as an integer. Multiply using Math.multiplyExact to alert us to integer overflow. Finally, report result.

int multiplicand = 2 ;
for( int codePoint : codePoints ) 
{
    if( Character.isDigit( codePoint ) )
    {
        String digitAsText = Character.toString( codePoint ) ;
        int x = Integer.parseInt( digitAsText ) ;
        int y = Math.multiplyExact( x , multiplicand ) ;
        System.out.println( x + " * " + multiplicand + " = " + y) ;
    }
}

See this code run at Ideone.com.

1 * 2 = 2
2 * 2 = 4
3 * 2 = 6
4 * 2 = 8

Shorter code

I am a beginner and I will try to write my problem as short as possible;

Short code is rarely a wise goal.

Longer code, written with short simple statements, is easier to read, easier to debug, and easier for the compiler to optimize.

But if you insist, here is a single statement that leverages advanced features of Java including streams, lambdas, and method references.

"1234"
        .codePoints()
        .filter( Character :: isDigit )
        .mapToObj( Character :: toString )
        .map( Integer :: valueOf )
        .map( ( Integer integer ) -> Math.multiplyExact( integer , 2 ) )
        .forEach( System.out :: println );

This single-statement code involves auto-boxing. So this code may run a tiny bit slower than the longer code above.

When run:

2
4
6
8
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154