• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting Array of Strings

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings to all. This is my first time using this forum, and it is kind of an emergency, so I hope for quick responses. I believe this is a beginner problem.

I am enrolled in an Introductory Java Programming class, and seem to be having issues grasping some of the concepts. Our teacher recently gave us an assignment of making a program that contains an array of names. It must count and display number of vowels in the string (name). It also must display in alphabetical order the names. The name sort is to be in a seperate method. I think I have it thus for, but it seems not to be working. Could someone help me work the kinks out?


Rory Scott




[ EJFH -- Added CODE tags to preserve formatting. ]
[ November 15, 2004: Message edited by: Ernest Friedman-Hill ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There seems to be some serious trouble with your sortString method. What is/are the formal parameters? (Also the parens don't match.)

Further you never actually try to call sortString from the main method. And when you do, after you straighten out its parameter list, you will get a compile error.

HTH getting you started on figuring out what's wrong with your program.
 
Rory Scott
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.







Thank you very much for your reply. It is much appreciated. However, I am confused on some things. 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? I think that is all of my questions. Again thank you for your help.

Rory
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Mark sortStrings as static, just as main() is so marked, and then you can just call it like

sortStrings(name);


Also, if I cut the string buffer, would I also cut the lines proceeding it until my loop starts?



You just want to replace

StringBuffer word = new StringBuffer(name[i]);
length=word.length();

with

length = name[i].length();
 
Rory Scott
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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]



I feel I have conquered most of these concepts. Thank you again for your help. But how do I fix my "char temp" declaration in the second method? I have attached my revisions, but am not sure what else I need in the begining of the second method. I know it is lacking, but I dont know what.

Also could you further explain your definition of the sort, and how I should fix it. I do not think I am quite comprehending.

Rory
 
Rory Scott
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]+" ");
}
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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]+" ");
}
}



You should modify sortString method following:


public static void sortString(String[] name)
{

for(int i=0;i<name.length;i++)


for(int j=i+1;j<name.length;j++)

{
if(name[i].compareTo(name[j])>0)
{
String temp = name[i];
name[i] = name[j];
name[j]=temp;
}
}

for(int i=0; i<name.length;i++)
System.out.print(name[i]+" ");
}

Have funs
 
Rory Scott
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just want to thank everyone for all of their speedy help. My program seems to be working fine, and I have a better understanding of what I am doing. (Still got a lot of work to do though). For any future reference, I have posted my final code.



Rory





//Class declaration
class Vowel
{
//main method
public static void main(String[] args)throws Exception
{
//Declaring variables and array
int counter=0;
int length;
char letter;
String[] name = {"Rory", "Ana", "Carla", "Mom", "Dad"};
sortString(name);
//This loop helps to count the vowels and compare them throughout the strings
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++;
}
//The print statement is in the loop so it can print for each string in the array, the total amount of Vowels thus far
System.out.println(+counter+" vowels were total have been counted.");
}
}
//This method is specifically for sorting.
public static void sortString(String[] name)
{
for(int i=0;i<name.length;i++)

for(int j=i+1;j<name.length;j++)
{
if(name[i].compareTo(name[j])>0)
{
String temp = name[i];
name[i] = name[j];
name[j]=temp;
}
}
for(int i=0; i<name.length;i++)
System.out.print(name[i]+" ");
}
}
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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++;
}



instead of all of these || tests, could a switch be used instead?
If so, would that be more efficient? If not, what would be more efficient than the || tests?

[ November 16, 2004: Message edited by: Matt Fielder ]
[ November 16, 2004: Message edited by: Matt Fielder ]
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Matt Fielder:
If so, would that be more efficient? If not, what would be more efficient than the || tests?



People worry too much about efficiency, and not enough about ugly.

I'd probably rewrite this as

if ("aeiouAEIOU".indexOf(c) > -1) ...
 
reply
    Bookmark Topic Watch Topic
  • New Topic