Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: What does this code output?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question What does this code output?

    I can't seems to get the output of this code to display in the console window, evening after implementing a System.out.println() for each variable. Could someone give me some suggestion or possible output for the code, thanks.

     public class Evaluate
    {
    public static void main(String[] args)
    {
    Stack<String> ops = new Stack<String>();
    Stack<Double> vals = new Stack<Double>();
    while (!StdIn.isEmpty())
    { // Read token, push if operator.
    String s = StdIn.readString();
    if (s.equals("(")) ;
    else if (s.equals("+")) ops.push(s);
    else if (s.equals("-")) ops.push(s);
    else if (s.equals("*")) ops.push(s);
    else if (s.equals("/")) ops.push(s);
    else if (s.equals("sqrt")) ops.push(s);
    else if (s.equals(")"))
    { // Pop, evaluate, and push result if token is ")".
    String op = ops.pop();
    double v = vals.pop();
    if (op.equals("+")) v = vals.pop() + v;
    else if (op.equals("-")) v = vals.pop() - v;
    else if (op.equals("*")) v = vals.pop() * v;
    else if (op.equals("/")) v = vals.pop() / v;
    else if (op.equals("sqrt")) v = Math.sqrt(v);
    vals.push(v);
    } // Token not operator or paren: push double value.
    else vals.push(Double.parseDouble(s));
    }
    StdOut.println(vals.pop());
    }
    }
    Last edited by helloworld922; January 14th, 2012 at 02:37 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What does this code output?

    These forums don't work like that. Perhaps you should be getting to the root of the problem - like why you see no output (does it compile?) - rather than asking someone to do your work for you

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What does this code output?

    Step 1: Format your code to be easily readable (i.e. add tabs).

    Step 2: What issue are you running into specifically? Compiler errors? Runtime errors? Which ones?

    fyi, StdOut and StdIn are not defined by default in Java, use System.out and System.in instead.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What does this code output?

    Quote Originally Posted by copeg View Post
    These forums don't work like that. Perhaps you should be getting to the root of the problem - like why you see no output (does it compile?) - rather than asking someone to do your work for you

    That what I really meant to ask? I tried compiling this program several times but did't see any output. So, copeg or anyone could you give me any suggestion on why I'm not seeing any output when I run this code. Hey, I any revise it a little to showing that I am really putting effort into this. Here is the revise version:
    import java.io.BufferedInputStream;
    import java.lang.*;
    import java.util.*;
    import java.util.Scanner;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.Locale;
     
     
    public class StdIn {
     
    	 private static Scanner scanner = new Scanner(new BufferedInputStream(System.in));
     
     
    	 public static boolean isEmpty() {
    	        return !scanner.hasNext();
    	    }
     
    	 public static String readString() {
    	        return scanner.next();
    	    }
     
     
     
    	public  static void main (String args[]){ 
     
     
    		//InputStreamReader input = new InputStreamReader(System.in);
    		//BufferedReader reader = new BufferedReader(input);
     
    		Stack<String> ops = new Stack<String>();
     
    		Stack<Double> vals = new Stack<Double>();
    		//String s = reader.readLine();
     
     
     
    		while (!StdIn.isEmpty())
    		{ // Read token, push if operator.
    			String s = StdIn.readString();
    		if (s.equals("(")) ;
    		else if (s.equals("+")) ops.push(s);
    		else if (s.equals("-")) ops.push(s);
    		else if (s.equals("*")) ops.push(s);
    		else if (s.equals("/")) ops.push(s);
    		else if (s.equals("sqrt")) ops.push(s);
    		else if (s.equals(")"))
     
    		{ // Pop, evaluate, and push result if token is ")".
    		String op = ops.pop();
    		double v = vals.pop();
    		if (op.equals("+")) v = vals.pop() + v;
    		else if (op.equals("-")) v = vals.pop() - v;
    		else if (op.equals("*")) v = vals.pop() * v;
    		else if (op.equals("/")) v = vals.pop() / v;
    		else if (op.equals("sqrt")) v = Math.sqrt(v);
    		vals.push(v);
    		System.out.println(vals.push(v));
    		} // Token not operator or paren: push double value.
    		else vals.push(Double.parseDouble(s));
    		System.out.println(Double.parseDouble(s));
     
    		}
    		System.out.println(vals.pop());	
     
     
     
    	}
     
     
     
     
     
    	}
    Last edited by helloworld922; January 15th, 2012 at 05:46 PM.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What does this code output?

    The current issue I am having with this code is: figuring out why don't I see any output. I take some of your suggestion: Here is a revise version of my code:
              //import java.io.*;
    	//import java.math.*;
    	//import java.io.OutputStreamWriter;
    	import java.io.BufferedInputStream;
    	//import java.lang.*;
    	import java.util.*;
    	import java.util.Scanner;
    	//import java.io.PrintWriter;
    	//import java.io.UnsupportedEncodingException;
    	//import java.util.Locale;
     
     
    	public class StdIn {
     
    	private static Scanner scanner = new Scanner(new BufferedInputStream(System.in));
     
     
    	 public static boolean isEmpty() 
    		 { return !scanner.hasNext();}
     
     
    	 public static String readString() 
    		 { return scanner.next();}
     
     
     
    	public  static void main (String args[]){ 
     
     
    	Stack<String> ops = new Stack<String>();
     
    	Stack<Double> vals = new Stack<Double>();
     
     
     
    	while (!StdIn.isEmpty())
    	{ // Read token, push if operator.
    		String s = StdIn.readString();
    	if (s.equals("(")) ;
    	else if (s.equals("+")) ops.push(s);
    	else if (s.equals("-")) ops.push(s);
    	else if (s.equals("*")) ops.push(s);
    	else if (s.equals("/")) ops.push(s);
    	else if (s.equals("sqrt")) ops.push(s);
    	else if (s.equals(")"))
     
    	{ // Pop, evaluate, and push result if token is ")".
    	String op = ops.pop();
    	double v = vals.pop();
    	if (op.equals("+")) v = vals.pop() + v;
    	else if (op.equals("-")) v = vals.pop() - v;
    	else if (op.equals("*")) v = vals.pop() * v;
    	else if (op.equals("/")) v = vals.pop() / v;
    	else if (op.equals("sqrt")) v = Math.sqrt(v);
    	vals.push(v);
    	System.out.println(vals.push(v));
    	} // Token not operator or paren: push double value.
    	else vals.push(Double.parseDouble(s));
    	System.out.println(Double.parseDouble(s));
     
    	}
    	System.out.println(vals.pop());	
     
     
    	}
     
     
    	}
    Last edited by helloworld922; January 15th, 2012 at 05:47 PM.

Similar Threads

  1. My code runs forever without output
    By jbinsomnia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 8th, 2011, 07:53 AM
  2. Replies: 0
    Last Post: August 30th, 2011, 08:23 AM
  3. No Error in Code but Output is not desired...!!!
    By Adi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 28th, 2011, 08:56 AM
  4. output to a specific location in html code
    By aketr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 15th, 2011, 11:44 AM
  5. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM

Tags for this Thread