Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting an Array and Outputting Components of Array

Status
Not open for further replies.

jeak

Technical User
Oct 9, 2002
16
0
0
US
This program prompts a user to enter a complete sentence (including a period). The program should read in the sentence and output a list of all letters the sentence contains in alphabetical order. It uses an array to store the letters that the program reads in from the sentence but should not contain the same letter twice. The program should then print out the letters with a comma and a space between each character. Finally, the program should print out the number of characters in the sentence (not including spaces) and the number of words in the sentence on separate lines. The following is my attempt at this. Does anyone have any comments or suggestions on my code? I don't think I did the portion correct where I am trying to print out each character only once. Also, I'm not sure how to search the string for the number of words.

Thanks for your help!!!


import java.util.Arrays;


public class Project
{
public static void main(String[] args)
{

int[] newArray = new int[100];//Initializing array
int count = 0, location = 0, numLetters = 0, numChars = 0;//Initializing variables


String input = JOptionPane.showInputDialog ("Please enter a complete sentence (using punctuation). ");


inputEntered = Integer.parseInt(input);
}



{
inputArray(inputEntered, count)
}


{
int[] newArray = new int [inputArray.Length - 1];
for (i = 0 < newArray.Length - 1; i++){
newArray = inputArray;
}
}


{
printOutput(numChars, numLetters, numWords, location)

}


public static void inputArray(inputEntered, count);{
char[] inputArray = new char[100];
string inputEntered;

while string inputEntered.equals nonWhiteChar;//Put sentence in to array without doubling characters
inputArray[count] = inputEntered;
count ++;//Keep count for number of characters
}


public static void printOutput(numChars, numLetters, numWords){
Arrays.sort(newArray);//Sort Array alphabetically

While (newArray != (o){
JOptionPane.showMessageDialog(
null, newArray[location] &quot;, &quot;);//Prints our characters with a comma separating them
location ++;
}

numLetters = count;
JOptionPane.showMessageDialog(
null, &quot;The number of characters are: &quot;+ numLetters);
numWords = //search string for number of words
JOptionPane.showMessageDialog(
null, &quot;The number of words are: &quot;+ numWords);
}




JOptionPane.showMessageDialog(
null, &quot;Click \&quot;OK\&quot; to end program.&quot;);
String junk;
junk = SavitchIn.readLine();//Ending Program

System.exit(0);
}
}
 
First, if you need to seperate out lower case and upper case letters than you will need a larger array to hold a maximum possibility of (A-Za-z1-0)*
Second, are you required to use an array? I would consider using a binary tree to solve this. Create an object called node that has references to two more null nodes (leftNode, rightNode) and a single variable to hold the character. Then as your parsing through the sentance you can recurse through the tree:
Call comparison function on the new letter and the current node your visiting (starting with root node, which is first letter read).
If the node is null, create a new one with that letter.
If the letter in the node is less, then call your comparison again on rightNode,
if the letter in the node is greater than your new letter, call a comparison on the left node.
If they are equivalent, throw it out (it's already in the tree).
Move to the next letter in the sentance.

What this will do is build a pretty tree for you that you can easily recurse into to get the letters in alphabetical order:
Code:
Read first letter (q):
Set to root node
tree:     q
         /       null null

Read next letter (u)
Compare(rootnode,letter)
   //compares to q inside rootNode
   rootNode calls Compare(Rightnode,letter) //u > q
      Rightnode is null
      Rightnode gets letter u and two null children

tree:    q
        /     null   u
          /        null null

Read Next Letter (i)
Compare(rootNode,letter)
   rootnode calls Compare(LeftNode,letter) //i < q
      Leftnode is null
      leftNode gets letter i and two null nodes

tree:    q
        /        i   u
     / |   /  null nullnull null

Read Next Letter (c)
Compare(rootNode,letter)
   rootnode calls Compare(LeftNode,letter) //c < q
      leftnode calls Compare(LeftNode,letter)   //c < i
         node is null, it gets letter c and 2 null nodes

tree: (using -'s instead of nulls, not enough space :) )
         q
        /        i   u
     / |   /    c   -  -   -
  /  -   -

Read Next letter (k):
Compare(rootNode,letter)
   rootnode calls Compare(LeftNode,letter) //k < q
      leftnode calls Compare(RightNode,letter)   //k > i         node is null, it gets letter k and 2 null nodes

tree: (using -'s instead of nulls, not enough space :) )
         q
        /        i   u
     / |  /    c   k -   -
  / \  / -   --  -

Now obviously this should be done in a loop, ie, read letter, call compare function until you hit end of line. At the same time you could be counting words by incrementing a counter each time you hit a non-space character preceded by a space. (add 1 for the first word).

Now to get the letter out all you have to do is define a method in the node, we will call it returnAlphabet

Code:
public string returnAlphabet(){
   String s = new string();
   s = &quot;&quot;;
   If leftNode is not null then
      s = leftNode.returnAlphabet + &quot;, &quot;;
   
   s = s + myLetter + &quot;, &quot;

   If rightNode is not null then 
      s = s + rightnode.returnAlphabet + &quot;, &quot;;

   return s;
}

So basically this function is going to dig all the way down the left side it can, and then come back up. SO in our above case it will get:
rootNode.returnAlphabet
rootnode checks left and calls leftNode.returnAlphabet
i node checks left and call leftNode.returnAlphabet
c node checks left, finds null
c node adds &quot;c, &quot; to it's string
c node checks right and finds nothing
c node returns string up
i receives &quot;c, &quot; from c node
i adds &quot;i, &quot; to have &quot;c, i, &quot;
i checks right and calls rightNode.returnAlphabet
k node checks left, finds null
k node adds self - &quot;k, &quot;
k node checks right, finds null
k returns it's string
i receives &quot;k, &quot; from rightNode
i now returns it's string &quot;c, i, k, &quot;
rootNode receives &quot;c, i, k, &quot; from it's left node
rootNode adds its letter: &quot;q, &quot; to get &quot;c, i, k, q, &quot;
rootNode checks right, calls rightNode.returnAlphabet()
u node checks left, finds null
u node adds itself: &quot;u, &quot;
u node checks right, finds null
u node returns it's string &quot;u, &quot;
rootNode receive &quot;u, &quot; from right
returns &quot;c, i, k, q, u&quot;
[/code]

Sorry for the length of the post, but I got carried away with my example :) Haven't played with binary trees in a while.


------------------------- Back to the above code -----------
you seem to be missing quote a few semi-colons, as well as having bloacks of code in {}'s that don't appear to be methods, or loops, or...
Is it possible you missed some of the code when you pasted it in? I attempted to compile and received errors on 19, 25, 27, 28, 32, 37, 41, ... most of them seem to just be mis-matched brackets or semi-colons though. Could you repost the working copy?

-Tarwn
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top