The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Page 3 -
Homework - Alternating vowels and consonants
Page 3 - Discuss Alternating vowels and consonants in the Java Help forum on Dev Shed. Alternating vowels and consonants Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 24th, 2013, 03:38 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | the string can only contain Vowels and Consonants |
Then the code only needs to test for vowels. If the letter is NOT a vowel, it is a consonant.
The code only needs ONE loop that looks at each letter and the next letter and makes sure they are different.
The type of the first letter is not important. What needs to be tested is that the types of two adjacent letters is different.
|

February 24th, 2013, 03:41 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 39
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR Then the code only needs to test for vowels. If the letter is NOT a vowel, it is a consonant.
The code only needs ONE loop that looks at each letter and the next letter and makes sure they are different.
The type of the first letter is not important. What needs to be tested is that the types of two adjacent letters is different. |
I actually can't do it, this has to be finished tonight and im obviously not getting this, I know what to do but when I look at the codes I just go blank, thank you for trying though.
Best wishes
|

February 24th, 2013, 03:46 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Put the current program to rest for a while.
Write a simpler testing program that takes a String, and in a loop gets each letter of the String and prints out a message saying if that letter is a vowel or a consonant.
When that works, you can move on to the next step.
|

February 24th, 2013, 04:15 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 39
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR Put the current program to rest for a while.
Write a simpler testing program that takes a String, and in a loop gets each letter of the String and prints out a message saying if that letter is a vowel or a consonant.
When that works, you can move on to the next step. |
Okay, I will try do that first
|

February 24th, 2013, 04:28 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 39
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
|
Okay I have that done, I created a program like that a view days ago
|

February 24th, 2013, 04:31 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Post the code and its output.
|

February 24th, 2013, 04:33 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 39
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR Post the code and its output. |
Code:
import javax.swing.JOptionPane;
public class AlternatingVowelsAndConsonants
{
public static void main(String[] args)
{
String userInput;
char d;
int vow = 0, con = 0;
userInput = JOptionPane.showInputDialog(null, "Enter a word ");
String strippedInput = userInput.replaceAll("\\W", "");
String strippedInput1 = strippedInput.toLowerCase();
int c = strippedInput1.length();
if (userInput.equals(""))
{
JOptionPane.showMessageDialog(null, "Invalid. Input is required.");
}
else
for(int index = 0; index <= c-1; index++)
{
d = strippedInput1.charAt(index);
if (d == 'a' || d == 'A' || d == 'e' || d == 'E' || d == 'i' || d == 'I'
|| d == 'o' || d == 'O' || d == 'u' || d == 'U')
{
vow++;
}
else
{
con++;
}
}
System.out.println("The no of vowels are " + vow);
System.out.println("the no of consonants are " + con);
}
}
|

February 24th, 2013, 04:40 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Now add code immediately following where the counters (vow and con) are incremented to look at the next letter (at index+1) and test that its type (vowel or consonant) is different than the type just found. You'll need to change the looping control to use < vs <= to keep from going past the end of the String.
|

February 24th, 2013, 04:48 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 39
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR Now add code immediately following where the counters (vow and con) are incremented to look at the next letter (at index+1) and test that its type (vowel or consonant) is different than the type just found. You'll need to change the looping control to use < vs <= to keep from going past the end of the String. |
I dont know how to do that, how do I test that its type is different from the type just found??
|

February 24th, 2013, 04:53 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | test that its type is different from the type just found?? |
When you have found a vowel and are incrementing the vow variable, you know that the current type is a vowel. The different type will be a consonant. So look at the next letter and see if it a consonant.
If you have found a consonant and are incrementing the con variable, you know that the current type is a consonant. The different type will be a vowel. Look at the next letter and see if it is a vowel.
|

February 24th, 2013, 04:56 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 46
Time spent in forums: 9 h 55 m 44 sec
Reputation Power: 0
|
|
Sorry to interrupt but code below might get you negative score from your instructor
Code:
d = strippedInput1.charAt(index);
if (d == 'a' || d == 'A' || d == 'e' || d == 'E' || d == 'i' || d == 'I'
|| d == 'o' || d == 'O' || d == 'u' || d == 'U')
since d is already LowerCase
|

February 24th, 2013, 04:58 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 39
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR When you have found a vowel and are incrementing the vow variable, you know that the current type is a vowel. The different type will be a consonant. So look at the next letter and see if it a consonant.
If you have found a consonant and are incrementing the con variable, you know that the current type is a consonant. The different type will be a vowel. Look at the next letter and see if it is a vowel. |
I honestly dont know how to code that, you must think im so slow  i use index + 1 yes, where do I put that though? on its on? twice like once after the vow and once after the con, or what? and how do I make it say basically this letter is a vowel, if the next letter is a consonant keep checking the rest to see if the pattern continues?? i dont know how to finish coding this
|

February 24th, 2013, 05:02 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | once after the vow and once after the con |
Yes there needs to be a test after each.
The looping will take care of testing the rest of the letters.
|

February 24th, 2013, 05:05 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 46
Time spent in forums: 9 h 55 m 44 sec
Reputation Power: 0
|
|
|
You Must implement a method, may be static
isVoWel(char c)
or else
isConsonant(char c)
Otherwise, there seems to be no way to a Sol'n
|

February 24th, 2013, 05:05 PM
|
|
Contributing User
|
|
Join Date: Feb 2013
Posts: 39
Time spent in forums: 4 h 15 m 8 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by NormR Yes there needs to be a test after each.
The looping will take care of testing the rest of the letters. |
How would I alter it to say it is alternating, or its not?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|