Extremely basic -- help with multiple arguments
Hello all.
So my first assignment in my computer science class is to write a program that counts the number of vowels. Basically, running
Code :
java main.Assignment1 "This is one argument" "A second one"
should return
Code :
Argument 1: 1 a, 2 e, 2 i, 1 o, 1 u
Argument 2: 1 a, 2 e, 0 i, 2 o, 0 u
All: 2 a, 4 e, 2 i, 3 o, 1 u
.
I'm very new Java. I have a working code, but it would only work for one argument:
Code :
package main;
public class Assignment1 {
public static void main(String args[]) {
String str = args[0];
int numA = 0;
int numE = 0;
int numI = 0;
int numO = 0;
int numU = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'A' || c == 'a') {
numA++;
}
if (c == 'E' || c == 'e') {
numE++;
}
if (c == 'I' || c == 'i') {
numI++;
}
if (c == 'O' || c == 'o') {
numO++;
}
if (c == 'U' || c == 'u') {
numU++;
}
}
System.out.print("Argument 1: ");
System.out.print(numA + " a, ");
System.out.print(numE + " e, ");
System.out.print(numI + " i, ");
System.out.print(numO + " o, ");
System.out.print(numU + " u");
}
}
My question is how can I make this work for any number of input arguments?
Re: Extremely basic -- help with multiple arguments
Could follow the approach you've already been following, but enclose all of it in a for loop. You could then do String str = args[indexOfEnclosingLoop];.
Re: Extremely basic -- help with multiple arguments
Do what you are currently doing for each element of the args[] array.
Stop and think first. Describe what you will do in real life language before you attempt it in code. Your description will probably begin "For each string in the args[] array...".
Then attempt what you have come up with in code. It depends on what steps you intend taking, but my guess is that your description of the process will involve a second for loop and some more variables.
Re: Extremely basic -- help with multiple arguments
Quote:
Originally Posted by
newbie
Could follow the approach you've already been following, but enclose all of it in a for loop. You could then do String str = args[indexOfEnclosingLoop];.
I was thinking something like this, but how can the program know to use all of the arguments (be it 2 or 5000)?
Re: Extremely basic -- help with multiple arguments
Quote:
Originally Posted by
jhnhskll
I was thinking something like this, but how can the program know to use all of the arguments (be it 2 or 5000)?
Arrays have a length property, discussed in the Arrays page of Oracle's Tutorial.
Re: Extremely basic -- help with multiple arguments
Thanks for the help guys! Here's what I'm working with now:
Code :
package main;
public class Assignment1 {
public static void main(String args[]) {
int numA = 0;
int numE = 0;
int numI = 0;
int numO = 0;
int numU = 0;
for (int idx = 0; idx <= (args.length - 1); idx++) {
String str = args[idx];
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == 'A' || c == 'a') {
numA++;
}
if (c == 'E' || c == 'e') {
numE++;
}
if (c == 'I' || c == 'i') {
numI++;
}
if (c == 'O' || c == 'o') {
numO++;
}
if (c == 'U' || c == 'u') {
numU++;
}
}
System.out.print("Argument " + (idx + 1) + ": ");
System.out.print(numA + " a, ");
System.out.print(numE + " e, ");
System.out.print(numI + " i, ");
System.out.print(numO + " o, ");
System.out.println(numU + " u");
}
System.out.print("All: ");
System.out.print(numA + " a, ");
System.out.print(numE + " e, ");
System.out.print(numI + " i, ");
System.out.print(numO + " o, ");
System.out.println(numU + " u");
}
}
}
This returns
Code :
Argument 1: 1 a, 2 e, 2 i, 1 o, 1 u
Argument 2: 2 a, 4 e, 2 i, 3 o, 1 u
All: 2 a, 4 e, 2 i, 3 o, 1 u
when run with the arguments "This is a string" and "Another one"
Now the only issue I'm having is figuring out how to count each separate argument instead of just increasing the count for all of them. I have no idea how to approach this =/
Re: Extremely basic -- help with multiple arguments
Glad you solved it, and thanks for sharing your solution jhnhskll.
Re: Extremely basic -- help with multiple arguments
Quote:
Originally Posted by
newbie
Glad you solved it, and thanks for sharing your solution jhnhskll.
Well, I haven't quite gotten it yet. I need some way to count the vowels in each argument separately.
Re: Extremely basic -- help with multiple arguments
Apologies yes, I was away with the fairies when I read your post.
A basic way to accomplish what you need is to use more variables, which you also print, then add to final tally, before resetting the new variables back to 0 at the end of each iteration.
Re: Extremely basic -- help with multiple arguments
Quote:
Originally Posted by
newbie
Apologies yes, I was away with the fairies when I read your post.
A basic way to accomplish what you need is to use more variables, which you also print, then add to final tally, before resetting the new variables back to 0 at the end of each iteration.
Ohhh I totally get it now. Code working as intended! Thanks for the help!