Click here to Skip to main content
15,615,155 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
so is there a way to separate a user input String to a stack ?
can you please guide me

like if a user inputs Hello each character would be put in a stack
and method to pop the vowels and only consonants would be left then print the outputs
like this

String: hello
Vowels: eo
Consonants: hll

What I have tried:

can please help me I can only do it to print hello,hello,hello,hello,hello on my code

Java
import java.util.Stack;
import java.util.Scanner;
class aeiou
{
	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		
		System.out.print("Input a String: ");
		String str = sc.nextLine();
		
		Stack<string> stack = new Stack();
		
		 for (int i = 0; i < str.length(); i++)
		    {
		          char ch = str.charAt(i);
		          stack.add(str); 

		          
		    }
		 System.out.print("The stack is: " + stack);
	}
}
Posted
Updated 8-Sep-21 22:39pm
v2
Comments
Patrice T 8-Sep-21 17:28pm    
Show your work.
Nico Nico Ni 8-Sep-21 21:56pm    
oh yeah i forgot
but heres my code

import java.util.Stack;
import java.util.Scanner;
class aeiou
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print("Input a String: ");
String str = sc.nextLine();

Stack<string> stack = new Stack();

for (int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
stack.add(str);


}
System.out.print("The stack is: " + stack);
}
}
Patrice T 8-Sep-21 22:01pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

You need two stacks, one for consonants and one for vowels. You also need to push the actual character each time, not the string; something like:
Java
Stack<Character> vowels = new Stack<Character>();
Stack<Character> consonants = new Stack<Character>();
for (int i = 0; i < str.length(); i++) {
    char letter = str.charAt(i);
    char c = Character.toLowerCase(letter);
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
        vowels.push(letter);
    }
    else {
        consonants.push(letter);
    }
}
System.out.print("Vowels: ");
while (!vowels.empty()) {
    System.out.print(vowels.pop());
}
System.out.println("");
System.out.print("Consonants: ");
while (!consonants.empty()) {
    System.out.print(consonants.pop());
}
System.out.println("");
 
Share this answer
 
Well ... it's pretty obvious why.
And since the code you show doesn't compile I have to start having doubts how much you have done for yourself ... In Java, string is not the same as String so this line doesn't compile:
Stack<string> stack = new Stack();

Fix that, and then look at your loop.
Where do you decide that a character is a vowel or a consonant?
Where are the two stracks you need to store them separately?
What do you do with ch inside the loop?

It looks to me as if you have thrown that together in the hope it might work, and failing that someone might write the correct code for you. That's not going to happen. We can't do all the work, it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900