• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Exception  RSS feed

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i have a program called "Passwordchecking",at runtime it is throwing ArrayIndexOutOfBoundsException.
i know the reason of why it is throwing that kind of exception.here's the code

class Passwordchecking
{
/**
*
Aug 12, 2007@param args
*/
public static void main(String args[]){

boolean ACCEPTED=true;


if((args[0].length()>=4) && (args[0].length()<=12)) {
String s=args[0];

if((s.charAt(0)>=97 && s.charAt(0)<=122)||(s.charAt(0)>=65 && s.charAt(0)<=90))
{
for(int i=0;i<args[0].length();i++)
{
char f=s.charAt(i);

int g=s.indexOf(f,i+1);

if(g==-1){
continue;
}
//if(g==(i+2)){
//if(s.charAt(g)==)

System.out.println(s.indexOf(s.charAt(g),i+1));

String fs1=s.substring(s.indexOf(f),i+2);

String fs2=s.substring(g,g+2);
if(fs1.equals(fs2)){
ACCEPTED=false;
System.out.println("Rejected");
break;
}else{ if(g!=s.length())
continue;
else
System.out.println("Accepted");
}
// }else
// continue;
}
}else{
System.out.println("Rejected");
}
if(ACCEPTED)
System.out.println("Accepted");
}
else
System.out.println("Rejected");


}
}

i want to show the output "Rejected" in case if user not passing arguments to the main method.
How can i do that?

Regards
Kirba.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the stack trace of the exception message. It tells you exactly in which line of code the exception happens. Then lookup that line in your code and find out why the exception happens there.

Note that if you have an array with N elements, then you can access the elements with the indices 0 to N-1.

This program expects an argument on the command line (which is passed to the main() method in the parameter 'args'). So when you run it, you must specify the parameter on the command line, for example:

java Passwordchecking helloworld

Note that class Passwordchecking should also be public. Add the word 'public' before the class declaration:

public class Passwordchecking { ...

If you want to see if the user has given any command line parameters, then you should check the length of the 'args' array:

Trying to access args[0] if 'args' has no elements will result in an ArrayIndexOutOfBoundsException.
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Young

Regards
Kirba.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!