-1

I am trying to make program that convert number to words but when I use charAt(0) to get the value of index 0 its returning me strange values

 import java.util.Scanner;

public class numToWord {
    public static   void main(String[] args) {

        String[] digit = {"Zero","One ","Two", "Three ","Four ","Five ","Six ", "Seven ", "Eight ", "Nine" };
        Scanner sc  = new Scanner(System.in);
        String strNum = sc.next(); //input 123
        int intnum = Integer.valueOf(strNum.charAt(0));
        System.out.println(intnum);
        System.out.println(digit[intnum]);
        //output 49

    }
}

example input 123 output 49

Input Output:

Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
  • 2
    What exactly is strange? If you input the string `"5"`, it consists of a single character, `'5'`, which has a codepoint of 53 in most encodings. – Lasse V. Karlsen Jul 11 '21 at 20:15
  • why its returning codepoint it's supposed to char right ? – Pratham Bhatnagar Jul 11 '21 at 20:21
  • 1
    What do *you* think a `char` should contain? It contains the codepoint of a character (well, technically it could be just a surrogate pair, but that's the exception, for most commonly used characters `char` = codepoint holds true). – Joachim Sauer Jul 11 '21 at 20:27
  • And after printing out the 'incorrect' value, the program will crash with an out-of-bounds array index, since there are not 54 elements in the digit array. (Oh, I didn't look at the linked picture of the output until now - yup, it crashed as expected). – iggy Jul 11 '21 at 21:45
  • ``char`` is an integer type in java. – NomadMaker Jul 11 '21 at 22:08

3 Answers3

2

You are currently reading the ascii value of the first character of your input. Also, you have spaces in your digit mapping. What you wanted to do was parse each digit and get the mapping. Something like,

String[] digit = { "Zero", "One", "Two", "Three", "Four", 
        "Five", "Six", "Seven", "Eight", "Nine" };
Scanner sc  = new Scanner(System.in);
String strNum = sc.next();
for (int i = 0; i < strNum.length(); i++) {
    if (i != 0) {
        System.out.print(" ");
    }
    int v = Character.digit(strNum.charAt(i), 10); // strNum.charAt(i) - '0'
    System.out.print(digit[v]);
}
System.out.println();
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

valueOf() is used for string conversion not for char type.

If you want to convert a char value to an integer using valueOf() it's always return the ASCII value of it. Use Character.getNumericValue() to solve this problem.

import java.util.Scanner;
public class numToWord{
    public static   void main(String[] args) {
        String[] digit = {"Zero","One ","Two", "Three ","Four ","Five ","Six ", "Seven ", "Eight ", "Nine" };
        Scanner sc  = new Scanner(System.in);
        String strNum = sc.next();
        //int intnum = Integer.valueOf(strNum.charAt(0));
        int intnum = Character.getNumericValue(strNum.charAt(0));
        System.out.println(intnum);
        System.out.println(digit[intnum]);
    }
}
Seraj
  • 46
  • 4
0

Look up what ASCII is.

The character '1' maps to the decimal value 49 on the ASCII chart.

ASCII chart

Visual Studio
  • 184
  • 1
  • 10