5 Replies - 357 Views - Last Post: 20 March 2011 - 04:27 AM Rate Topic: -----

#1 tallism  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-March 11

HELP WITH CALCULATORS! PLEASE

Posted 19 March 2011 - 09:49 PM

For the second part of this assignment Im using the code below but my compiler doesnt like the isEmpty or readLine call in the for/while loop...help! Im desperate!

import java.util.*;

public class Calc
{

	public void main (String [] args)

	{

		try {


		Scanner stdin= new Scanner(System.in);
		LLStack <Integer> stack= new LLStack <Integer>();


		 while (! stdin.isEmpty())
		 	{
		            String s = stdin.readLine();
		            if      (s.equals("+")) stack.push(stack.pop() + stack.pop());
		            else if (s.equals("*")) stack.push(stack.pop() * stack.pop());
		            else stack.push(Integer.parseInt(s));
		        }
		        System.out.println(stack.pop());
		    }

		    catch (StackEmptyException e){
				System.out.println(e);
			}
		}
	}

Attached File(s)

  • Attached File  a4.pdf (356.1K)
    Number of downloads: 61

This post has been edited by macosxnerd101: 19 March 2011 - 09:51 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: HELP WITH CALCULATORS! PLEASE

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10574
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: HELP WITH CALCULATORS! PLEASE

Posted 19 March 2011 - 09:52 PM

Those methods don't exist for the Scanner class. Check out the documentation. There is a nextLine() method for Scanner instead of readLine(), as well as hasNext() and hasNextLine() methods in liue of isEmpty().
Was This Post Helpful? 2
  • +
  • -

#3 tallism  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-March 11

Re: HELP WITH CALCULATORS! PLEASE

Posted 19 March 2011 - 10:05 PM

Program still isnt working out for me :(


import java.util.*;

public class Calc
{

	public void main (String [] args)

	{

		try {


		Scanner stdin= new Scanner(System.in);
		LLStack <Integer> stack= new LLStack <Integer>();


		 while (! stdin.hasNextLine())
		 	{
		            String s = stdin.next();
		            if      (s.equals("+")) stack.push(stack.pop() + stack.pop());
		            else if (s.equals("*")) stack.push(stack.pop() * stack.pop());
		            else stack.push(Integer.parseInt(s));
		        }
		        System.out.println(stack.pop());
		    }

		    catch (StackEmptyException e){
				System.out.println(e);
			}
		}
	}


This post has been edited by macosxnerd101: 19 March 2011 - 10:06 PM
Reason for edit:: USE CODE TAGS!!!

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10574
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: HELP WITH CALCULATORS! PLEASE

Posted 19 March 2011 - 10:06 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: HELP WITH CALCULATORS! PLEASE

Posted 19 March 2011 - 10:13 PM

What ???

while (! stdin.hasNextLine())  // if there is NO MORE line available
{  
     String s = stdin.next();  // read a String


Was This Post Helpful? 1
  • +
  • -

#6 audriusa  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 28
  • Joined: 22-September 10

Re: HELP WITH CALCULATORS! PLEASE

Posted 20 March 2011 - 04:27 AM

Depending on what are you doing, it may be easier with the old good BufferedReader:

BufferedReader reader = new BufferedReader(new InputStreamReader(your_stream));
String s;
while ( (s = reader.readLine()) != null) {
  // Do something with the string s.
}


I have used it a lot of times to read from files and URLS, however never tried with System.in. In general this is not the most frequently used stream and if you launch the program from IDE it may not work properly just because of the lack of IDE support.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1