Mar 10, 2020 07:02 PM
(This post was last modified: Mar 10, 2020 07:04 PM by Juice..)
I am trying to do an assignment in my Java class. I currently have it working, but I am using a linked list and it's methods in Java. To learn better, our professor wants us to make our own linked list and methods with it. It is for a calculator that uses a stack. I will post part of the code that shows it working using the predefined linked list, but I don't know how to make it myself and was going to see if I could get some help.
Code
LinkedList<Constant> constantlist=new LinkedList<Constant>(); //can't use this
else if ((s.substring(0,2).equals(":=")) && (s.length() > 2)) {
char c = s.charAt(2);
if ((c>='a') && (c<= 'z') || (c>='A') && (c<= 'Z'))
{
s=s.substring(2);
for (int i=0; i< s.length(); i++)
{
if(!Character.isJavaIdentifierPart(s.charAt(i))) {
System.out.println("Error: invalid identifier.");
System.exit(1);
}
}
if (constantlist != null)
{
for (int i = 0; i < constantlist.size(); i++) {
if (s.equals(constantlist.get(i).name)==true)
{
System.out.println("ERROR: name already defined.");
System.exit(1);
}
}
constantlist.add(new Constant(s, stack.peek() , true));
System.out.println("Added constant " + s + " with value " + stack.peek());
}
}
}
else {
for (int i = 0; i < constantlist.size(); i++) {
if (s.equals(constantlist.get(i).name)==true) // check if exists as a constant
{
foundconstant=true;
stack.push(constantlist.get(i).data); // if it does, use that value
}
}
if (foundconstant==false) {
System.out.println("Didn't recognize input. Exiting.");
System.exit(1);
}
class Constant{
public String name;
public double data;
public boolean isconstant;
Constant(String n, double d, boolean c) {
name = n;
data = d;
isconstant = c;
}
}
