permaculture playing cards*
The moose likes Beginning Java and the fly likes java beginner needs help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java beginner needs help!" Watch "java beginner needs help!" New topic
Author

java beginner needs help!

tao ke
Greenhorn

Joined: May 16, 2006
Posts: 3
im trying to create a program that does as the below question asked.

Create a program that reads a list of vowels (a, e, i, o, u) and stores them in an array. The maximum number of vowels to be read should be obtained from the user before beginning to read the vowels, however, the user may cancel the reading of vowels at any time by entering '#'. The algorithm should then count and display the number of times each vowel appears in the array. Finally the algorithm should also output the index in the array where each vowel first appeared or a suitable message if a particular vowel is not in the array at all.


An example run of your program should look something like this: -



How many vowels would you like to read in? 30

Please enter a vowel (a,e,i,o,u) (30 max), # to quit: a
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: a
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: e
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: e
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: i
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: a
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: e
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: i
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: o
Please enter a vowel (a,e,i,o,u) (30 max), # to quit: #
there are 3 a's in the array, the first appearing in index 0.
there are 3 e's in the array, the first appearing in index 2.
there are 2 i's in the array, the first appearing in index 4.
there are 1 o's in the array, the first appearing in index 8.
there are no u's in the array!

below is a small part of the code i did. im trying to first match the input and vowels and print out the result as shown in the example but i dont know what is wrong with it.
import B102. *;

public class prac
{

static int inputnum()
{
int maxnum;
System.out.println("how many vowel would you like to read in?");
maxnum=Keybd.in.readInt();
return maxnum;
}

static void inputletter(char maxletter[], int maxnum)
{
int num;
for(num=0;num<maxnum;num++)
{
System.out.println("please enter a vowel (a,e,i,o,u)" + "(" + maxnum + " max) # to quit");
maxletter[num]=Keybd.in.readChar();
}
}

static void sort(char maxletter[])
{
int num;
int count=0;
char vowels[]={'a', 'e', 'i', 'o', 'u'};
for(num=0;num<vowels.length;num++)
if(vowels[num]==maxletter[])
count++;
System.out.print(vowels[num] + count );
}

public static void main(String args[])
{
int num;
int maxnum=inputnum();
char maxletter[]=new char[maxnum];
for(num=0;num<maxletter.length;num++)
inputletter(maxletter, maxnum);
sort(maxletter);
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41104
    
  45
Welcome to JavaRanch.

So what does the code do so far, and -assuming that it doesn't yet do what you would like it to do- where are you stuck implementing the rest?


Ping & DNS - my free Android networking tools app
tao ke
Greenhorn

Joined: May 16, 2006
Posts: 3
thank you for reply so fast.
the problem is in here.

static void sort(char maxletter[])
{
int num;
int count=0;
char vowels[]={'a', 'e', 'i', 'o', 'u'};
for(num=0;num<vowels.length;num++)
if(vowels[num]==maxletter[])
count++;
System.out.print(vowels[num] + count );
}

im trying to get it to match the vowels with keyboard input and print out how many letters are being inputted like "ther are 4 a and 2 i and 5 e........there is a problem in here but im not sure why and not even sure if im allowed to do that or not "if(vowels[num]==maxletter[])"

the problem said "class expected"
kittu vadde
Greenhorn

Joined: May 13, 2006
Posts: 27
It is syntactically wrong. u need to give something like if(vowels[num]==maxletter[index])

and moreover, there is no Keybd.in.readInt(); or Keybd.in.readChar();

u have to use System.in.read(); it will return int. if u want to store it in char u need to cast explicitly
[ May 16, 2006: Message edited by: kittu vadde ]

SCJP 1.4 <br />SCWCD 1.4 <br />SCBCD 'have start...
tao ke
Greenhorn

Joined: May 16, 2006
Posts: 3
the keybd stuff is from the package B102 which i imported in.

i tried to change to vowels[num]==maxletter[num1] and it says arrayindexoutofboundexception. can someone show me how it is done please??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18522
    
  40

Originally posted by tao ke:

i tried to change to vowels[num]==maxletter[num1] and it says arrayindexoutofboundexception. can someone show me how it is done please??


The command is correct. It is throwing "index out of bound" because either the value of "num" or num1" is incorrect. The exception stack trace should tell you the index that you are trying to use, which should give you a hint of what you did wrong.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java beginner needs help!
 
Similar Threads
program assignment help
If only one vowel is entered in the string, then that vowel should be diplayed on console
Count the number of vowels, words, and sentences.
Passing Vector elements to an array
file output question