开发者

Where/how can I allow non-numeric values in my program?

I am making this little program using command line arguments, I have like 90% of the work done. But I am trying to allow the user to enter non numerical values aswell..

User input/output example

Input:

$ java d1 4eb:16 10110110:2 407:8 2048:10

Output:

4eb base 16 is 1259 base 10 10110110 base 2 is 182 base 10 407 base 8 is 263 base 10 2048 base 10 is 2开发者_开发知识库048 base 10

My only problem is with the first input because it has letters and it gives me a number exception error. Any help would be great, and I would prefer a help in the right direction, rather than just the answer. Thank you!

public class homework{
    public static void main (String[] args){
        int answer1=0,check1=0,check2=0,x=0, val=0,rad=0;   //holds integer values user gives and check for : handler, answer etc
        do{   //will continue to loop if no : inputted
        for (x=0;x<args.length;x++){

                check1=args[x].indexOf(":");        //checks input1 for the :
                if(check1==-1){System.out.println("No Colon Found in "+args[x]+".");check1=0;}
                else{
                    String numbers [] = args[x].split(":");     //splits the string at :
                    val = Integer.parseInt(numbers[0]);   //parses [0] to int and assigns to val
                    rad = Integer.parseInt(numbers[1]);     //parses [1] to int and assigns to rad
                    if(val==0||rad==0){System.out.println("The argument "+args[x]+" could not be converted.");check2=0;}
                    else{
                    for (int i = 0; val > Math.pow(rad, i); i++){
                        int digit = (val / (int) Math.pow(10, i)) % 10;
                        int digitValue = (int) (digit * Math.pow(rad, i));
                        answer1 += digitValue;}
                        answer1 = Integer.parseInt(numbers[0], rad);   //finds the answer in base10.
                        System.out.println(val+" base "+rad+" is "+answer1+" base 10.");  //gives user the results
            }}}}while(check1==-1);  }}  //if user forgot : loop


You need to specify the base when you first call parseInt. For example, if you are parsing a hexidecimal number, you need to specify:

val = Integer.parseInt(numbers[0], 16)

You receive this exception because you try to parse a hexadecimal number in base 10.

Maybe you should make the base another command line parameter. I will assume it is the first command line parameter. Then you could run:

int base = Integer.parseInt(args[0])
val = Integer.parseInt(number[0], base)


As requested, here are some hints.

The exception is arising here:

val = Integer.parseInt(numbers[0]);

You are always parsing the number before the colon as if it were in base 10, even if it's not.

Also, the purpose of the for loop eludes me. Once you have parsed the number using the correct radix, printing it out in base 10 is very straightforward.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜