• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

wrapper class example

>>The wrapper classes can take constructors of either the type they are designed to wrap or of a String that can be converted to that type. Thus the Integer class can take a number that could be contained by an int, but an error will occur if you try to pass a number with a floating point component. Remember that the wrapper classes are just that classes, they are not primitives and instances of the wrappers can only be manipulated in the same way as any class. You may get questions on the exam with code that uses standard math operations to manipulate instances of wrappers. You can of course use the + operator where it would implicitly call the toString method, but as soon as you see the - * or % operator, beware.

public class String2Int{
    public static void main(String argv[]){    
    try{
        int i= Integer.parseInt(argv[0]);
        System.out.println("Coverted to int val = " + i);
        }catch(NumberFormatException nfe){                  System.out.println("Could not covert to int");
        }  
    }
}




i was going through above statements from following link

http://www.jchq.net/certkey/0803certkey.htm

It is not clear to me.

When i ran the program i got
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
      at String2Int.main(String2Int.java:4)

What command line arguments program is expecting. Does it convert string to integer or int to Integer.

Any ideas, resources, links, sample code highly appreciated. thanks in advance.
0
gudii9
Asked:
gudii9
  • 3
  • 3
2 Solutions
 
objectsCommented:
run it using:

java String2Int 45
0
 
objectsCommented:
it convert string (passed on the command line) to an int
0
 
for_yanCommented:
it converts String to integer as your command line arguments are always Strings
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
for_yanCommented:
If you are running it in IDE, say Eclipse,  you should add command line parameters
as arguments of the program in your Run configuaration
0
 
gudii9Author Commented:
i ran in eclipse as 'run cofiguration' and in the arguments i gave like'abc' it says

Could not covert to int


please advise on how can i run this example
0
 
for_yanCommented:
Makes sense if you gave argument "abc" - it cannot interpret this as a an int number

You should give argument as a number , say 15 - in this from
0
 
objectsCommented:
> please advise on how can i run this example

see my first 2 comments. Its expecting the entered string to be a number. The application attempts to parse the string  into an int. thus abc will give you the error you see
you need to specify a number instead
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now