aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch and parseInt??? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "switch and parseInt???" Watch "switch and parseInt???" New topic
Author

switch and parseInt???

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
What happens when you try to compile and execute the following application with the command-line argument "1"? Choose all correct options.
1. class Switcheroo {
2. public static void main(String[] args) {
3. int i = 0;
4. try {
5. i = Integer.parseInt(args[0]);
6. }
7. catch (Exception e) { }
8.
9. switch (i) {
10. case 0:
11. System.out.println("zero");
12. case 1:
13. System.out.println("one");
14. default:
15. System.out.println("default");
16. }
17. }
18. }

Options :
1) compiler error
2) The program prints "zero"
3) The program prints "one"
4) The program prints "default"
Ans : 3) and 4)
can any one explain me how do we get such an output.what is parseInt??
what does a "parseInt" do?..
Sonir
Chris Graham
Greenhorn

Joined: Jan 08, 2002
Posts: 22
parseInt() is a static method that belongs to Interger objects. If you look at the Java API it will list all the methods for this class.
I'm having trouble finding the links to the API that has the Interger object (perhaps someone else will be able to find it and post it here).
i = Integer.parseInt(args[0]);
The array args[] is an array of String objects. If I'm not mistaken, the first element in that array is the number of command line parameters. In order to initialize int i = args[0], you have to convert args[0] to an int. You do this by using Interger.parseInt(args[0]). Basically an Interger is an object and an int is an atomic type, and the parseInt() method (which is a static method available to the Interger class) reads in a String and returns an int.
You can't perform:
int i = args[0];
because args[0] is a String, and you can't set an interger equal to a string (maby you can, but it's not logically correct, and the value would be invalid).
i.e.
int x = String y //is not valid
int x = int y //is valid
The Interger.parseInt() is just the method you use to do the conversion for you.
Hope this answers your question.
--Chris
--Am I a Ranch Hand yet?
[ January 09, 2002: Message edited by: Chris Graham ]
Pradeepa Battina
Greenhorn

Joined: Jan 08, 2002
Posts: 3
parseInt() method takes a string argument and returns an integer.
If the command line argument is 1,
the switch takes 1 as argument and the control
goes to case 1:, so one is displayed and because
it doesn't have break the control goes to default
where default is displayed.
Jason Kretzer
Ranch Hand

Joined: May 31, 2001
Posts: 280
I believe this has been answered but I will try to make it a little clearer.
Command-line arguments are stored in the args array of the main method.(String args[]) They are stored as Strings.
To get an integer value from a String representation of a number, you use the static method parseInt(String intNum) from the wrapper class Integer.

For the 'switch' part of the question, first you pass an argument to a switch, in this case i. Then the argument is used to determine which case should be executed. Usually, there are 'break' statements after each case to tell when the 'switch' block should be exited. In your example there is no breaks after any of the cases, so the switch sets execution to case 1, executes it, and then falls through to the next line of code which is default.


Hope this helps.


Jason R. Kretzer<br />Software Engineer<br />System Administrator<br /><a href="http://alia.iwarp.com" target="_blank" rel="nofollow">http://alia.iwarp.com</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: switch and parseInt???
 
Similar Threads
Keep getting this error and not sure why
Question on switch construct
Switch statements
Dan Chisholm's question from chapter 5
[HELP]Trying to make a decision cmd adventure! How to branch decisions? NEED HELP AGAIN :S! NOT done