Stack Java Problem

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
I'm using eclipse with acm.jar and i can't seem to print out my stack of objects, here's the code:

Code:
public void run () {
	int i = 1;
	Stack Number = new Stack();
	while(i!=0)
	{
		String oper = readLine("Enter Number or Operator: ");
		boolean Digit = checkForDigit(oper);
		boolean Operator = checkForOperator(oper);
		if (Digit==true)
			{
			double number = Double.parseDouble(oper);
			Number.push(number);
                        println(Number);
			}
          }
    }

If i add a ; on the if statement i am able to print out my stack but i need it to work without the ; since i can't do anything after this.

The boolean variables just check wheter or not a string is a digit or an operator (+-/*).

Simply put, i want to print out the stack every time i add or change something in it (for every loop count, the stack needs to be printed out in the end).
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Would need more of the code.
Like that, it's going to loop forever as i is never set to something else in the loop...

Ohter things:

println(Number) like that is not a function. Unless you used static import which doesn't seem to be the case here.
readLine() if your class extends BufferedReader it's a function but if your class doesn't, it's not a function.
println(Number.toString()) might want to put .toString() after just to say you dont get the object serialized form.

EDIT: You extend Program, I guess. You should have mentionned it...
 

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
Code:
import acm.program.*;
import java.util.*;


public class Program1Calc extends ConsoleProgram {	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7291875403816121715L;
	
	private double performOperation(double x,double y,String z) 
	{
		double result = 0;
		
		if (z.equalsIgnoreCase("+"))
		{
		    result = x+y;
		}
		else if (z.equalsIgnoreCase("/"))
		{
		    result = x/y;
		}
		else if (z.equalsIgnoreCase("-"))
		{
		    result = x-y;
		}
		else if (z.equalsIgnoreCase("*"))
		{
		    result = x*y;
		}
		else {
			result = 0.12345678987654321;
		}
		return result;	
	}
	
	public boolean checkForDigit(String s) //Check if String is digit.
	{
	boolean b = false;
	int check = 0;
	for (int i = 0; i < s.length(); i++)
	{
	if (Character.isDigit(i)==true)
	    {
		   check = check+1;
	    } 
	 } 
	if (check==s.length())
	{
		b = true;
	}
		return b;
	} // Method Check if String is digit End.
	
	public boolean checkForOperator(String s) //Check if String is operator.
	{
	boolean b = false;
	if (s.equalsIgnoreCase("*") || s.equalsIgnoreCase("+") || s.equalsIgnoreCase("-") || s.equalsIgnoreCase("/"))
		    {
			b = true;
		    }
		return b;
	} // Method Check if String is operator End.
	
	
	
	public void run () {
	int i = 1;
	Stack<Double> Number = new Stack<Double>();
	while(i!=0)
	{
		String oper = readLine("Enter Number or Operator: ");
		boolean Digit = checkForDigit(oper);
		boolean Operator = checkForOperator(oper);
		if (Digit==true)
			{
			double number = Double.parseDouble(oper);
			Number.push(number);
			println(Number);
			}
    }
  }
}

The i is supposed to loop infinitely (it reads one line at a time and adds it to the stack, hence i'm not worried about infinite loops).
EDIT: You extend Program, I guess. You should have mentionned it...
Sorry, i'm just beginning to work in java >< There is the code in it's entirety and it really works fine (readline, println replace the system out and the buffer read in the console program (hence the void run)). I'm just not sure about the stack (cause that is imported from java.util).

Why does it print out only after the if function is ended, and not if left open? >>
I have to use println because i need to print out into the console program, i'll try using system.out.print and see if it prints in as the system message, maybe the console is screwing up.

Edit2: Same problem with the system.out.print. if i do this:
Code:
if (Digit==true);
			double number = Double.parseDouble(oper);
			Number.push(number);
			println(Number);
			System.out.println(Number);
it prints, but i need to continue with further coding and this makes it impossible.

Adding to this, if i put the println outside of the if without closing it, it returns empty [] brackets (usually it would return [50.0] if the digit entered is 50, etc.).
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Character.isDigit(i)==true here you're giving a value between 0 and str.length(), not the actual character. Character.isDigit(str.charAt(i)) this would be better

Code:
this
	public boolean checkForOperator(String s) //Check if String is operator.
	{
	boolean b = false;
	if (s.equalsIgnoreCase("*") || s.equalsIgnoreCase("+") || s.equalsIgnoreCase("-") || s.equalsIgnoreCase("/"))
		    {
			b = true;
		    }
		return b;
	} // Method Check if String is operator End.
    
could be

    public boolean checkForOperator(String s) {
        return (s.equalsIgnoreCase("*") || s.equalsIgnoreCase("+") || s.equalsIgnoreCase("-") || s.equalsIgnoreCase("/"));
    }
    
// ===================================================================== //
    
this
    public boolean checkForDigit(String s) //Check if String is digit.
	{
	boolean b = false;
	int check = 0;
	for (int i = 0; i < s.length(); i++)
	{
	if (Character.isDigit(i)==true)
	    {
		   check = check+1;
	    } 
	 } 
	if (check==s.length())
	{
		b = true;
	}
		return b;
	} // Method Check if String is digit End.
    
could be

    public boolean checkForDigit(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (Character.isDigit(s.chatAt(i)))
                continue;
            else
                return false;
        }
        return true;
    }
The i is supposed to loop infinitely (it reads one line at a time and adds it to the stack, hence i'm not worried about infinite loops).
Just use while(1){} and use the break keyword to stop.
 

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
Thank you x) I learnt something now XP, but still have a problem with my stack not printing out properly...

Edit: oh...it seems to work now? why? XD

Seems like it was something with the digit method! Thank you XD
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top