Jump to content

counting and storing

- - - - -

  • Please log in to reply
6 replies to this topic

#1
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
hi guys,
i'm stuck on this assignment i've got.
what i'm meant to do is ask the user how many vowels they would like to enter. read the vowel from the user and print how many times the vowel was put in a repetition. and stop either wen the loop is over or the user inputs #
an example of how its ment to be:

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!

this is my code so far:
B102 was provided by university so that i can use Scree.out.println(); and use max= Keybd.in.readInt(); to read max from the println.


import B102.*;

public class CountVowel {

	

	int aFirst, eFirst, iFirst, oFirst, uFirst;

	int aNum, eNum, iNum, oNum, uNum;

	int max;

	int ReadVowel;

	

public static void main(String [] args){

	CountVowel cv = new CountVowel();

	cv.readMaxCount();

	cv.readVowels();

	cv.printOutPut();

	

}


public void printOutPut() {

	


	Screen.out.println("there are  a's in the array, the first appearing in index .");

	Screen.out.println("there are  e's in the array, the first appearing in index .");

	Screen.out.println("there are  i's in the array, the first appearing in index .");

	Screen.out.println("there are  o's in the array, the first appearing in index .");

	Screen.out.println("there are  u's in the array, the first appearing in the index .");

}


public void readVowels() {

	Screen.out.println("Please enter a vowel (a,e,i,o,u)  ('+max' max), # to quit:" +ReadVowel);

	ReadVowel=Keybd.in.readInt();

	if(ReadVowel=='a')//how would i store this into an array and output it above

	if(ReadVowel=='e')//how would i store this into an array and output it above

	if(ReadVowel=='i')//how would i store this into an array and output it above

	if(ReadVowel=='o')//how would i store this into an array and output it above

	if(ReadVowel=='u');//how would i store this into an array and output it above

}


public void readMaxCount() {

	

	max= Keybd.in.readInt();

	Screen.out.println("How many vowels would you like to read in?" +max);

}

}


how do i store 'a' in the array and store it. then output it later?
if the user input's a store in array x or something like that.

I'm very new to java and understand very little of it.
so even my explanation above might be hard to understand.

#2
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts

pindo said:

my explanation above might be hard to understand
you are right there.
anyway when I was just as new to java I would do it like this.
this might use somework too I think you have to have a method that recieves a String then within your class you can do...


public void readVowels() 

{

                char[] vowels = {'a','e','i','o','u'};//thats where you store or strory the letters i think should be within the class

                aNum = 0;//and others as well

	Screen.out.println("Please enter a vowel (a,e,i,o,u)  ('+max' max), # to quit:" +ReadVowel);

	ReadVowel=Keybd.in.readInt();

	if(ReadVowel==vowels[0])//how would i store this into an array and output it above

                  aNum++;

	if(ReadVowel==vowels[1])//how would i store this into an array and output it above

                   bNum++;

	if(ReadVowel==vowels[2])//how would i store this into an array and output it above

                   cNum++;

	if(ReadVowel==vowels[3])//how would i store this into an array and output it above

                   dNum++;

	if(ReadVowel==vowels[4]);//how would i store this into an array and output it above

                     eNum++;//don't mistake this for an enum

}



#3
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
thank you, that is the part where i count how many vowels i have. how do i return where in the array the first a/e/i/o/u is?
and also how do i go about the printing?
would it be:

Screen.out.println("there are '+a' a's in the array, the first appearing in the index .");

to say there are a certain number of a's?

also how would i validate that only a e i o or u is entered and if not an error is printed?

Edited by pindo, 03 May 2011 - 02:47 AM.


#4
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts



import B102.*;

public class CountVowel {

	

	int aFirst, eFirst, iFirst, oFirst, uFirst;

	static int aNum;

	static int eNum;

	static int iNum;

	static int oNum;

	static int uNum;


	static int max;

	static int ReadVowel;

	

public static void main(String [] args){

	int input;

	readMaxCount();

	input=readVowels();

	printOutPut(input);

	

}



public static void printOutPut(int input) {

	

	Screen.out.println("there are " +aNum+" a's in the array, the first appearing in index .");

	Screen.out.println("there are " +eNum +" e's in the array, the first appearing in index .");

	Screen.out.println("there are " +iNum +" i's in the array, the first appearing in index .");

	Screen.out.println("there are " +oNum +" o's in the array, the first appearing in index .");

	Screen.out.println("there are " +uNum+" u's in the array, the first appearing in the index .");

}


public static int readVowels() 

{

	int[] vowels = {'a','e','i','o','u'};

	Screen.out.println("Please enter a vowel (a,e,i,o,u) "+"("+max+" max), # to quit:" +ReadVowel);

	ReadVowel=Keybd.in.readInt();

	if(ReadVowel==vowels[0])

                  aNum++;

	if(ReadVowel==vowels[1])

                   eNum++;

	if(ReadVowel==vowels[2])

                   iNum++;

	if(ReadVowel==vowels[3])

                   oNum++;

	if(ReadVowel==vowels[4]);

                    uNum++;

                    return ; //i do not know what to return


}


public static void readMaxCount() {

	

	Screen.out.println("How many vowels would you like to read in?" +max);

	max= Keybd.in.readInt();

	return;

}

}





thats my new code
i propably have done a lot wrong
i still cant find the index.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Did you learn about the String class yet?

#6
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
we have apparently done basics to java.

i just dont know what to do exactly. how to do it.

#7
pindo

pindo

    Newbie

  • Members
  • PipPip
  • 15 posts
solved!
thanks for your helps though




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users