Question:
please help me debug this JAVA(source code)program! It can be compiled,but it does not run!!!?
mjmg
2007-07-07 21:56:34 UTC
***after i compiled this as javac go.java, it has no errors, but when I want to run this already as " java go " it cannot be run. there says Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at go.main(go.java:3)
***here's the source code: (please do help me debug and run this JAVA program please)

class go{
public static void main(String [] args){
String s = args[0];
int[] f = tally(s);

}
static int[] tally(String s){
int[] f = new int[26];
s = s.toUpperCase();

for(int i=0; i char ch = s.charAt(i);

if (Character.isLetter(ch)){
int j = ch - 'A';
++f[j];
}
}

return f;

}

static void print(int[] f){
for(int j=0; j char ch = (char)(j+'A');
if (f[j]>0)
System.out.println(ch +": "+f[j]);
}
}
}
Five answers:
anonymous
2007-07-07 22:21:37 UTC
look to this sample program:





public class Test {

public static void main(String args[]) {

System.out.println("Hello There "+ args[0] +" "+args[1]);

}

}







To run this class after compiling it, you will specify two arguments e.g.



java Test Joe Blogg



It will print



Hello There Joe Blogg where args[0] = Joe and args[1] = Blogg





in ur program u r not passing any argument while executing it.

as on the third line :- String s=args[0];

if u will not pass any argument for array args[] then it will throw ArrayIndexOutOfBound exception. Hope u get it.
mc_barron
2007-07-07 22:01:22 UTC
The problem is that your program is expecting an argument but you're not passing in an argument. The line that is looking for an argument:

...

public static void main(String [] args){

String s = args[0]; <--- Here you are looking for an argument

...



Perhaps try something like "java go Whatever".
anonymous
2016-05-21 06:00:36 UTC
Hahaha No debugging suks!
iammisc
2007-07-07 22:02:36 UTC
you need to type in java go , where is any number, the program needs a number to tally.
abd
2007-07-07 22:51:16 UTC
in the command line, try to run ur program as:

java go "test"


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...