I'm doing an actually simple practice problem from the book, but I am still not sure how to correctly do it.. It's just converting pseudo code.

1st part

num SIZE = 5

num VALID_ITEM_PRICE [SIZE] = 0.59, 0.99, 4.50, 15.99, 17.50, 39.00

stack int validItems

num i

bool foundIt = false

string MSG_YES = “Item available”

string MSG_NO = “Item not found”

push (106, 108, 307, 405, 457, 688) onto validItems

How I would do it:

public class Project
{
public static void main(String args[])
{



// declaring variables

Num int SIZE = 5;


// Array

double VALID_ITEM_PRICE[ ] = { 0.59, 0.99, 4.50, 15.99, 17.50, 39.00 };

// Create a stack of integers and add some numbers
        Valid_Item<Integer> Valid_Item = new Valid_Item<Integer>();


Num = i;


// use boolean to check

boolean foundit = false;


Num  String MSG_YES = "Item available";

Num String MSG_NO = "Item not found";

public void push (double x)
{
if (top == s.lenght)
throw new StackOverFlowException();
else
{
s[top] = x;
top ++;
}
}

//rest of pseudo code goes here - 2nd part


public static void main(String [] arg)
{
String str; //use for output
ArrayValid_Item st= new ArrayValid_Item(5);
str = "Pushing 106, 108, 307, 405, 457, 688 onto the stack.";
System.out.println(str);
st.push(106);
st.push(108);
st.push(307);
st.push(405);
st.push(457);
st.push(688);
str = "Value at top of the stack is:";
System.out.println(str);

}
}

I do not think this is correct, but that's all I can come up with at the moment. Or is this correct?

Does it compile and execute? Is the output correct? That would be a way to see if it is correct.

I'm not able to download a compiler onto this computer. I can't test it right now.

Hard to test without a JDK.

I know it's not complete and some things are missing such as declarations. I just wanted to know if I did the stack somewhat correctly.

What does the compiler say when you compile it? You need to do that first before you post code here so you know if there are any compiler errors in the code and can post them with the code.

The online compiler gives all kinds of errors...

VALID_ITEM<Integer> VALID_ITEM= new VALID_ITEM<Integer>();

"can not find symbol" for VALID_ITEM.

The compiler can not find a definition for the class(??) VALID_ITEM. Is it a class? Where is its definition?

Good question.

Ok, nver-mind. I misplaced it. It should go to the end, correct? Together with the push.

So stack int validItems means I just declare VALID_ITEM as int. I was wondering why it said stack and figured I'd create the stack there.

When you post code you need to copy it from the source and use the proper tags to show that it is code.
When you type in something that is not really from the source, it is confusing. For example you have spelled valid item several different ways.

One general piece of advice:

You do not just throw pieces of code together and hope something works.

I tell that it does not work, or if by some miracle it worked, the amount of debugging it half bugfree would bee twice the effort of starting from beginning and building one addition at time and testing after each addition.

I am not.

import javax.swing.*;

public class Project
{


// declaring variables
int SIZE = 5;
double price = 0.0;
String address, name;
String itemString, quantityString;
int item;
double VALID_ITEMS;
int poppeditem;




//

double VALID_ITEM_PRICE[ ] = { 0.59, 0.99, 4.50, 15.99, 17.50, 39.00 };

int i;


// use boolean to check



boolean foundit = false;


String MSG_YES = "Item available";

String MSG_NO = "Item not found";


// top of stack
private Node top = null; 


// empty method

public boolean empty()
{
    return top == null;

}

public void push (String s)

{
if (top == s.lenght)
throw new StackOverFlowException();
else
{
s[top] = s;
top ++;
}
}

//pop method

public int pop()
{
    if (empty())
        throw new EmptyStackException();
    else
    {
        top--;
        return s[top];
    }
}


// get input



name = JOptionPane.showInputDialog("Enter name: ");

address = JOptionPane.showInputDialog("Enter address: ");

itemString = JOptionPane.showInputDialog("Enter item number: ");
item = Integer.parseInt(itemString);

quantityString = JOptionPane.showInputDialog("Enter quantity: ");
quantity = Integer.parseInt(quantityString);



// initialize the loop 1

int i = 0;


// use the loop to go through the array


while (VALID_ITEM == null)
{

if (item == VALID_ITEM[] ) // popedItem = pop from validItems stack ?

{

foundit = true;


price = VALID_ITEM_PRICE[i];

}


sub = sub + 1;

}





if (foundit == true) 

{
System.out.println (MSG_YES );

System.out.println (quantity + " at " + price + " each " );

total = quantity * price;

System.out.println("The total price is: " + total);

}


else
{

System.out.println ( MSG_NO );
}






public class LinkedStack
{
    public static void main(String [ ] args)
    {
        LinkedStack st = new LinkedStack();


String str; //use for output
str = "Pushing 106, 108, 307, 405, 457, 688 onto the stack.";
System.out.println(str);
st.push(106);
st.push(108);
st.push(307);
st.push(405);
st.push(457);
st.push(688);
System.out.println("Contents of stack:")
System.out.println(st);
}
}

I simply need help with implementing the stack correctly. The rest should work or only require minor imporvements.
I don't know if the push etc. declaration is correct, as well as the stack atthe bottom. The class probably dosent have the correct name, but that is all I know how to do right now. Or do I completely erase all the stack references and declare it at the bottom, as well as the push and print ?

Does the code compile and execute? Is the output what you want?
Copy and post here the full text of any error messages.
If the output is wrong, copy it here and add some comments to it describing what is wrong with it and show what you want the output to be.

I simply need help with implementing the stack correctly.

Please explain what the program is supposed to do and what problems you are having making it do what you want.

No, of course it dosen't excecute. The one loop isn't even finished yet since it's related to the stack., which is the first error message here.

illegal start of type
if (item == VALID_ITEM[] ) // popedItem = pop from validItems stack ?

illegal start of type
while (VALID_ITEM == null)

<identifier> expected
while (VALID_ITEM == null)

illegal start of type
while (VALID_ITEM == null)

class, interface, or enum expected
System.out.println (quantity + " at " + price + " each " );

class, interface, or enum expected
total = quantity * price;

You left off the text of the error message with the line number. Can you copy and paste the error messages as they are output by the compiler?

Please copy full text of error message and paste it here. Here is a sample:

TestSorts.java:138: cannot find symbol
symbol  : variable var
location: class TestSorts
         var = 2;
         ^

Ok

init:
deps-jar:
Created dir: C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\build
Updating property file: C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\build\built-jar.properties
Created dir: C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\build\classes
Created dir: C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\build\empty
Created dir: C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\build\generated-sources\ap-source-output
Compiling 2 source files to C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\build\classes
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:80: <identifier> expected
name = JOptionPane.showInputDialog("Enter name: ");
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:82: <identifier> expected
address = JOptionPane.showInputDialog("Enter address: ");
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:84: <identifier> expected
itemString = JOptionPane.showInputDialog("Enter item number: ");
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:85: <identifier> expected
item = Integer.parseInt(itemString);
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:87: <identifier> expected
quantityString = JOptionPane.showInputDialog("Enter quantity: ");
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:88: <identifier> expected
quantity = Integer.parseInt(quantityString);
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:100: illegal start of type
while (VALID_ITEM == null)
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:100: <identifier> expected
while (VALID_ITEM == null)
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:100: ';' expected
while (VALID_ITEM == null)
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:100: illegal start of type
while (VALID_ITEM == null)
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:100: <identifier> expected
while (VALID_ITEM == null)
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:101: ';' expected
{
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:103: illegal start of type
if (item == VALID_ITEM[] ) // popedItem = pop from validItems stack ?
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:103: ';' expected
if (item == VALID_ITEM[] ) // popedItem = pop from validItems stack ?
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:103: <identifier> expected
if (item == VALID_ITEM[] ) // popedItem = pop from validItems stack ?
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:115: <identifier> expected
sub = sub + 1;
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:123: class, interface, or enum expected
if (foundit == true) 
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:128: class, interface, or enum expected
System.out.println (quantity + " at " + price + " each " );
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:130: class, interface, or enum expected
total = quantity * price;
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:132: class, interface, or enum expected
System.out.println("The total price is: " + total);
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:134: class, interface, or enum expected
}
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:141: class, interface, or enum expected
}
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\src\javaapplication01\Project.java:164: ';' expected
System.out.println("Contents of stack:")
23 errors
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\nbproject\build-impl.xml:605: The following error occurred while executing this line:
C:\Users\computer\Documents\NetBeansProjects\JavaApplication01\nbproject\build-impl.xml:246: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

At line 80 the compiler is confused to find some statements outside of a method. Make sure the code starting at around line 80 are inside of a method.

Never-mind then. That line is the least of my problems.

Consider pyTony's advice. Rewrite the code by inputting a few lines, compiling, fixing the errors, compiling, fixing until a clean compile. Then add a few more lines, and do it again. Try to keep from having the huge list of errors all at once.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.