dan-dan
03-01-2012, 11:30 PM
Hey.
OK, I'm very new to Java (and OOP) and just trying to get a grip of it all. So far so good but just hit this little problem. I myself can't find any errors in the code, and Netbeans compiles it fine without producing any errors.
Basically, my intentions are to output the invalid args[] errors (if any) followed by the two equations.
Each time I run it, the output is listed randomly, sometimes correctly, sometimes not (like below).
I've re-positioned everything countless times but it's still always random!
Please help!
run:
Illegal input at argument 2 with input a. Characters only!
The sum of valid passed arguments is 3
Illegal input at argument 4 with input b. Characters only!
The average of the passed arguments is 2.0
BUILD SUCCESSFUL (total time: 0 seconds)
class Averager {
public static void main(String[] args) {
int sum = 0;
int validArgs = 0;
if (args.length > 0) {
for (int i = 0; i < args.length; i++) {
if (notLegalCharacters(args[i])) {
System.err.print("Illegal input at argument " + (i + 1) + " with input " + args[i] + ". Characters only! \n");
}
else {
sum += Integer.parseInt(args[i]);
validArgs++;
}
}
System.out.println("The sum of valid passed arguments is " + validArgs);
System.out.println("The average of the passed arguments is " + (float) sum / validArgs);
}
else {
System.err.println("You did not provide any arguments!");
}
}
static boolean notLegalCharacters(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i)))
return true;
}
return false;
}
}
OK, I'm very new to Java (and OOP) and just trying to get a grip of it all. So far so good but just hit this little problem. I myself can't find any errors in the code, and Netbeans compiles it fine without producing any errors.
Basically, my intentions are to output the invalid args[] errors (if any) followed by the two equations.
Each time I run it, the output is listed randomly, sometimes correctly, sometimes not (like below).
I've re-positioned everything countless times but it's still always random!
Please help!
run:
Illegal input at argument 2 with input a. Characters only!
The sum of valid passed arguments is 3
Illegal input at argument 4 with input b. Characters only!
The average of the passed arguments is 2.0
BUILD SUCCESSFUL (total time: 0 seconds)
class Averager {
public static void main(String[] args) {
int sum = 0;
int validArgs = 0;
if (args.length > 0) {
for (int i = 0; i < args.length; i++) {
if (notLegalCharacters(args[i])) {
System.err.print("Illegal input at argument " + (i + 1) + " with input " + args[i] + ". Characters only! \n");
}
else {
sum += Integer.parseInt(args[i]);
validArgs++;
}
}
System.out.println("The sum of valid passed arguments is " + validArgs);
System.out.println("The average of the passed arguments is " + (float) sum / validArgs);
}
else {
System.err.println("You did not provide any arguments!");
}
}
static boolean notLegalCharacters(String str) {
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i)))
return true;
}
return false;
}
}