Convert a double digit to String

1

I'm trying to convert a number (Double) entered by keyboard into String,

example = > 8.3 = > eight with three

I created a scanner class to enter the double and I understood that with valueOf I could do it, but I must be doing something wrong logically ...

    public static void main(Double[] args) {

    String cadena;

    System.out.println ("Introduzca nota: ");
    //Creación de un objeto Scanner
    Scanner oNota = new Scanner (System.in);
    Double nota = oNota.nextDouble ();

    cadena = String.valueOf(nota);
    System.out.print(cadena);
    }

Also I am a little blocked with the head of the main class, it is not supposed to be void because I want to return a value, but if I remove it I get an error. Is the Double in the header fine? I understand that if a double is introduced ... that must be Double. Is that so?

Thank you.

    
asked by rodic 02.01.2017 в 22:23
source

2 answers

2

It seems to me that you are already doing it correctly, you receive a value type double stored in the variable nota , use double no Double (primitive type):

double nota = oNota.nextDouble();

and convert it to String, using String.valueOf() :

 cadena = String.valueOf(nota);

to print it you may not even need to convert it to String:

 System.out.print(nota);
    
answered by 02.01.2017 в 22:36
0

The valueOf is fine, try entering the double with the "," for example 8.5 instead of the "." since your Locale is Spain. If you want it with the point indicate the Locale of another country where the point is used and it will not give you an error:

Scanner oNota = new Scanner (System.in).useLocale(Locale.US);

EDIT: Now that I notice the main can not go with Double

public static void main(String[] args) {

If you want to check that they are doubles to do something with them:

public static void main(String[] args) {

        Double[] dbs=new Double[args.length];
        String doublePatron = "([0-9]*)\.([0-9]*)";  

        for(int i=0;i<args.length;i++)
        {
            if(Pattern.matches(doublePatron, args[i]))
                 dbs[i]=Double.valueOf(args[i]);
            else
                dbs[i]=-1.0;
            System.out.println(dbs[i]);
        }

        }

In this case if we execute:

java -jar JavaApplication6.jar 7.5 5.5 A

We would return by console:

7.5
5.5
-1.0

EDIT 2:

With this you pass from number to text from 0 to 9.9, if you need more it is simple.

public static void main(String[] args) {

    Double[] dbs=new Double[args.length];
    String doublePatron = "([0-9]*)\.([0-9]*)";  

    for(int i=0;i<args.length;i++)
    {
        if(Pattern.matches(doublePatron, args[i]))
        {
            String numeroEscrito="";
            for(char c:args[i].toCharArray())
            {
            switch (c){
             case '0':
                 numeroEscrito+="cero";
                 break;
             case '1':
                 numeroEscrito+="uno";  
                 break;
              case '2':
                 numeroEscrito+="dos";
                 break;
             case '3':
                 numeroEscrito+="tres";  
                 break;                    
             case '4':
                 numeroEscrito+="cuatro";
                 break;
             case '5':
                 numeroEscrito+="cinco";  
                 break;
              case '6':
                 numeroEscrito+="seis";
                 break;
             case '7':
                 numeroEscrito+="siete";  
                 break;    
              case '8':
                 numeroEscrito+="ocho";
                 break;
             case '9':
                 numeroEscrito+="nueve";  
                 break;  
             case '.':
                 numeroEscrito+="con";

            }
            numeroEscrito+=" ";//faltaria quitarle el ultimo espacio        

            }
            System.out.println(numeroEscrito);
        }
        else
            System.out.println(args[i]+" no es un Double");
    }

    }

for:

java -jar JavaApplication6.jar 8.6 8.5 A

Da output:

ocho con seis
ocho con cinco
A no es un Double
    
answered by 02.01.2017 в 22:36