SCJP, SCWCD
Originally posted by Ernest Friedman-Hill:
Hi Rory,
Welcome to JavaRanch!
Hmm. This is rather a lot to pack into one assignment.
First, the vowel-counting looks mostly OK to me, if the point is to count all the vowels in all the words; otherwise, it needs a bit of rearranging. In particular, the println() call would move inside the outer loop.
You don't need to construct a StringBuffer to get the length of a String; String has its own perfectly good length() method you can call directly.
That long and scary conditional with all the comparisons-to-vowels could be made shorter in several ways, but none of them are too beginnerish so I'd leave well enough alone.
As far as the string sorting goes, your routine is a good start, but it actually only puts the first element into the right place; you need two nested loops, one varying the "left hand" index, and the other the "right hand" index, to sort the whole array. Think about that for a bit and let's see what you can come up with.
The argument list of sortString() is a little wrong. The "char temp" declaration belongs inside the routine, and the String[] parameter (which you call "name" inside the routine) needs to be named in the parameter list.
Finally, of course, something in main() probably ought to call sortString(), or it won't actually run.
Originally posted by Rory Scott:
First of all, how do I call a method? My book tells me how to call from another class, but that is not what I need.
Also, if I cut the string buffer, would I also cut the lines proceeding it until my loop starts?
Originally posted by Ernest Friedman-Hill:
[QB]
As far as the string sorting goes, your routine is a good start, but it actually only puts the first element into the right place; you need two nested loops, one varying the "left hand" index, and the other the "right hand" index, to sort the whole array. Think about that for a bit and let's see what you can come up with.
The argument list of sortString() is a little wrong. The "char temp" declaration belongs inside the routine, and the String[] parameter (which you call "name" inside the routine) needs to be named in the parameter list.
QB]
Originally posted by Rory Scott:
Oops, forgot the code
class Vowel
{
public static void main(String[] args)throws Exception
{
int counter=0;
int length;
char letter;
String[] name = {"Rory", "Ana", "Carla", "Mom", "Dad"};
sortString(name);
for(int i=0; i<5;i++)
{
length = name[i].length();
for(int j=0; j<length;j++)
{
letter=name[i].charAt(j);
if(letter=='a'|| letter=='e'||letter== 'i'||letter== 'o'||letter=='u'||letter=='A'||letter=='E'||letter=='I'||letter=='O'||letter=='U')
counter++;
}
System.out.println("There were "+counter+" vowels.");
}
}
public static void sortString(String[] args)
{
length = 4;
for(int i=0;i<length;i++)
{
if(name[i].compareTo(name[i+1])>0)
{
char temp = name[i];
name[i] = name[i +1];
name[i+1]=temp;
}
}
for(int i=0; i<5;i++)
System.out.print(name[i]+" ");
}
}
letter=name[i].charAt(j);
if(letter=='a'||letter=='e'||letter=='i'||letter=='o'||letter=='u'||letter=='A'||letter=='E'||letter=='I'||letter=='O'||letter=='U')counter++;
}
Originally posted by Matt Fielder:
If so, would that be more efficient? If not, what would be more efficient than the || tests?
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|