elements in the stack?
Started by couline, Feb 09 2012 05:31 PM
11 replies to this topic
#1
Posted 09 February 2012 - 05:31 PM
ty
#2
Posted 09 February 2012 - 05:39 PM
/
#3
Posted 10 February 2012 - 03:37 AM
Firstly never use a single letter for an object, its ambiguous and as seen from your code hard to know what your using.
Should be
So should be something like this
couline, on 09 February 2012 - 05:31 PM, said:
Stack<String> mark = new Stack<String>(); stack.push(h);
Should be
mark.push(c);
couline, on 09 February 2012 - 05:31 PM, said:
index = mark.search(h); // this needs to be set after the readline and set to s not h
System.out.print("Enter the element you want to search: ");
String s = m.readLine();So should be something like this
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
public class Prefinals_Exer1 {
public static void main(String[] args) throws Exception {
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of elements for Stack: ");
int ele = Integer.parseInt(bufferedreader.readLine());
Stack<String> mark = new Stack<String>();
String inputString = "";
int index = 0;
for (int x = 0; ele > x; x++) {
System.out.print("Enter element: ");
inputString = bufferedreader.readLine();
mark.push(inputString);
}
System.out.print("Enter the element you want to search: ");
String searchString = bufferedreader.readLine();
index = mark.search(searchString);
System.out.print("The element " + searchString + " is located at the index " + index);
}
}
The more you learn, the more you realize how little you know…
#4
Posted 10 February 2012 - 09:25 AM
thanks. but i think it doesnt work. i mean the code works but it doesnt search for the right index. x-x
#5
Posted 10 February 2012 - 01:22 PM
I am guessing the real problem is you don't understand how a stack works or what it is? It is last in first out so the last on in is always at index 1, then you count left.
Enter number of elements for Stack: 5 Enter element: a [a] Enter element: b [a, b] Enter element: c [a, b, c] Enter element: d [a, b, c, d] Enter element: e [a, b, c, d, e] Enter the element you want to search: b The element b is located at the index 4
The more you learn, the more you realize how little you know…
#6
Posted 10 February 2012 - 05:53 PM
.
#7
Posted 13 February 2012 - 08:46 AM
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
public class Prefinals_Exer1 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of elements: ");
int stackSize = Integer.parseInt(br.readLine());
Stack<String> stack = new Stack<String>();
Stack<String> tempStack = new Stack<String>();
for (int x = 0; x < stackSize; x++) {
System.out.print("Enter an element: ");
String c = br.readLine();
stack.push(c);
System.out.println(stack);
}
System.out.print("Enter the element you want to search: ");
String searchString = br.readLine();
for (int x = 0; x < stackSize; x++) {
if (stack.peek() == null ? searchString == null : stack.peek().equals(searchString)) {
System.out.println("found a match at " + x);
}
tempStack.push(stack.pop());
}
}
}
Should show all search items, starting from the right with a 0;
haha to much coding even ending my sentences with a semi colon
The more you learn, the more you realize how little you know…
#8
Posted 13 February 2012 - 08:57 AM
thanks, and sorry for deleting my posts. x-x
#9
Posted 20 February 2012 - 09:16 AM
i dont need the whole code, but a little hint will be appreciated. x-x the output should be
Enter number of element: 3
Enter an element: 4
Enter an element: 5
Enter an element 4
You have entered element 4 2 times
You have entered element 5 1 times
i dont quiet get how the logic works. >.> is it a loop within a loop or an if statement within an if statement?
i know you're still gona use those peek and pop methods. not just sure how.
here's the code ive been working on.
________________________________________________________________
import java.io.*;
import java.util.*;
public class Exercise{
public static void main(String[] args) throws Exception {
BufferedReader m = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of elements: ");
int ele = Integer.parseInt(m.readLine());
Stack<String> stack = new Stack<String>();
Stack<String> stack2 = new Stack<String>();
Stack<String> stack3 = new Stack<String>();
String inputEle ="";
for (int x = 0; x < ele; x++)
{
System.out.print("Enter an element: ");
inputEle = m.readLine();
stack.push(inputEle);
}
for(int x = 0; x < ele; x++)
{
if(stack.peek().equals(inputEle))
{
System.out.println("You have entered element " + stack.peek() +" " + x + " times");
}
stack2.push(stack.pop());
}
}
}
Enter number of element: 3
Enter an element: 4
Enter an element: 5
Enter an element 4
You have entered element 4 2 times
You have entered element 5 1 times
i dont quiet get how the logic works. >.> is it a loop within a loop or an if statement within an if statement?
i know you're still gona use those peek and pop methods. not just sure how.
here's the code ive been working on.
________________________________________________________________
import java.io.*;
import java.util.*;
public class Exercise{
public static void main(String[] args) throws Exception {
BufferedReader m = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of elements: ");
int ele = Integer.parseInt(m.readLine());
Stack<String> stack = new Stack<String>();
Stack<String> stack2 = new Stack<String>();
Stack<String> stack3 = new Stack<String>();
String inputEle ="";
for (int x = 0; x < ele; x++)
{
System.out.print("Enter an element: ");
inputEle = m.readLine();
stack.push(inputEle);
}
for(int x = 0; x < ele; x++)
{
if(stack.peek().equals(inputEle))
{
System.out.println("You have entered element " + stack.peek() +" " + x + " times");
}
stack2.push(stack.pop());
}
}
}
#10
Posted 21 February 2012 - 03:49 AM
Will be a lot of recursion, just pop the first item off the stack in to a second stack, search the stack it came from, if its a match add it too the second stack, if not a match pop it to a third stack, repeat till first stack is empty. Then start again using the third stack. Can you use the inherited Vector methods?
The more you learn, the more you realize how little you know…
#11
Posted 21 February 2012 - 09:42 AM
"just pop the first item off the stack in to a second stack, search the stack it came from, if its a match add it too the second stack, if not a match pop it to a third stack, repeat till first stack is empty."
hmmm. lets assume i got all my pops and peeks right the code should look something like this..
sorry, my logic is really low. x-x
for(int x = 0; x < ele; x++)
{
if(stack.peek().equals(inputEle)) //not sure about what's inside the if statement. x-x
{
stack2.push(stack.peek());
}
else
{
stack3.push(stack.pop());
}
stack2.push(stack.pop());
}
about the inherited vector, i dont think we've tackled that already. ._.
hmmm. lets assume i got all my pops and peeks right the code should look something like this..
sorry, my logic is really low. x-x
for(int x = 0; x < ele; x++)
{
if(stack.peek().equals(inputEle)) //not sure about what's inside the if statement. x-x
{
stack2.push(stack.peek());
}
else
{
stack3.push(stack.pop());
}
stack2.push(stack.pop());
}
about the inherited vector, i dont think we've tackled that already. ._.
#12
Posted 23 February 2012 - 09:24 AM
prof already checked our program. xD but he didnt tell us how the program looks like. >.> he asked us to find it ourselves.
im wondering how the program looks like. =/
im wondering how the program looks like. =/












