• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java arrays

 
Tharindu Priyath
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test03{
public static void main(String args[]){
int i = Integer.parseInt(args[0]);
switch (i)
{
case 1:
System.out.println("case 1");
case 2:
System.out.println("case 2");
default:
System.out.println("default");
break;
}
}
}
This program shows Runtime error. Why?
What is athe meaning of this statement?
int i = Integer.parseInt(args[0]);
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
args[0] is a command line argument that you give it when you run it.

If you look at the variable args, it signifies a string array. You're converting the string into an int.

Also, you should add break statements after each case, otherwise it's going to run through the whole list.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Upon the running of a program, it's possible to pass in String values to a program. String args[] is the array that holds the (optional) input values. Since you didn't pass anything to the program when you tried to run it, you got an error because you tried to use the value that you didn't pass in.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50180
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the Batmobile!! Java Tutorials!! Look here and here. I think the 2nd link will be more useful.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic