hello. i have done the followinh program to convert a String value to a double value.when i am compiling this program, i get an error as"unclosed character literal in the 18th line.can anyone say what's the problem?

import java.io.*;
class strin
{
    public static String a(String s);
    {
        String j="";
        char a;
        int i;
        for(i=0;i<s.length();i++)
        {
            c=s.charAt(i);
            if(c==' ')
            {
                j=j+c;
            }
            switch(j)
            {
                case 'one':System.out.print("1");break;
                case 'two':System.out.print("2");break;
                case 'three':System.out.print("3");break;
                case 'four':System.out.print("4");break;
                case 'five':System.out.print("5");break;
                case 'six':System.out.print("6");break;
                case 'seven':System.out.print("7");break;
                case 'eight':System.out.print("8");break;
                case 'nine':System.out.print("9");break;
                case 'ten':System.out.print("10");break;
                case 'eleven':System.out.print("11");break;
                case 'twelve':System.out.print("12");break;
                case 'thirteen':System.out.print("13");break;
                case 'fourteen':System.out.print("14");break;
                case 'fifteen':System.out.print("15");break;
                case 'sixteen':System.out.print("16");break;
                case 'seventeen':System.out.print("17");break;
                case 'eigtheen':System.out.print("18");break;
                case 'ninteen':System.out.print("19");break;
                case 'twenty':System.out.print("2");break;
                case 'thirty':System.out.print("3");break;
                case 'fourty':System.out.print("4");break;
                case 'fifty':System.out.print("5");break;
                case 'sixty':System.out.print("6");break;
                case 'seventy':System.out.print("7");break;
                case 'eigthy':System.out.print("8");break;
                case 'ninty':System.out.print("9");break;
                case 'hundred':System.out.print("1");break;
                case 'and':System.out.print("");break;
                case 'dot':System.out.print(".");break;
                case 'point':System.out.print(".");break;
            }
        }
        return i;
    }
    public static void main()throws IOException
    {
        try
        {             
        String r;
        BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter a sentence to print it in numbers");
        r=x.readLnie();
        String h;
        str cl=new cl();
        h=cl.a(r);
    }
    catch(Exception e)
    {
        System.out.print(e);
    }
}
}

Recommended Answers

All 10 Replies

A char is just that, ONE character. 'and' is not a valid char.
You want to do a switch on Strings? Don't we all. You can't. It may be introduced in Java 1.7.

Have a loook at Hashmap - it allows you to build a lookup table with Strings as keys and Integers or other Strings as values and it will do the lookup for you...

i am convetring the character to a string value by usu=ing the variable 'j'.

Maybe so, but what I said before is still true.

Member Avatar for coil

And also, you can't use a switch-case on a String.

You want to do a switch on Strings? Don't we all. You can't. It may be introduced in Java 1.7.

I don't. If you're trying to switch on Strings, to my mind that suggests that you've got something confused somewhere in your program.
Here's my reasoning: The only Strings that are sufficiently clean to use in a switch are either ones you've generated in the program - in which case, you might as well generate a primitive and save the overhead - or ones that you've cleaned up from the user input - in which case, you might as well clean them all the way up, to a primitive. So what would be the case where a switch on a String would make life better? I think it's one of those misfeatures that could only lead to bad code.

The case presented here is about the only one where this could be useful, but how often do we do this, outside of CS101?

I (almost) agree. I was just making the point that "people" often seem to want to do that, and that you can't yet, but that it may be in 1.7, so the O/P isn't alone here.
Personally, I take the view that if, as you say, the strings are suffiently clean, then it should be an enum. Switches on enums are exceptionally easy to read and understand, and very safe indeed.

can i do any modification in my program? if so, where? can you please help?

Like I said right at the beginning - forget the switch and use a Hashmap to convert "one" to "1", "dot" to "." etc etc

what is a hashmap and how do i use it? please help me @JamesCherril

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.