• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java beginner needs help!

 
tao ke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
tao ke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
tao ke
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic