943,740 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 10477
  • Java RSS
Apr 13th, 2009
0

Scanner Input from Keyboard help

Expand Post »
Hi, I'm trying to write a java program that will read in the scanner input from a user and outputs an postfix expression and answer
so if you typed in:
A = 5
B = 7
$PART2
AB+

the output would be
AB+ = 12

I'm having trouble with the scanner input I can make it so a user
can enter a letter and a number, but I'm having trouble trying to make the program shift so when the user enters $PART2 it will then begin to read in the post fix expressions.

If anyone could help I would greatly appreciate it.


Java Syntax (Toggle Plain Text)
  1.  
  2. import java.util.*;
  3. import java.io.*;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7.  
  8. public class PostEval
  9. {
  10.  
  11. static public void main(String args[])
  12. {
  13.  
  14. Scanner scanner = new Scanner(System.in);
  15. Stack<String> postFix = new Stack<String>();
  16. String part2 = "$PART2";
  17. System.out.println("Input Letter and Number it equals: ");
  18. while(scanner.hasNext())
  19. {
  20. String letter = scanner.next();
  21. char equals=(scanner.next()).charAt(0);
  22. int number = scanner.nextInt();
  23. System.out.println("You Inputted:"+ letter+ equals+ number);
  24. if(letter == "$PART2")
  25. {
  26. while(scanner.hasNext())
  27. {
  28. String postfix = scanner.nextLine();
  29. System.out.println(postfix);
  30. }
  31. }
  32.  
  33. }
Similar Threads
Reputation Points: 46
Solved Threads: 0
Newbie Poster
yingfo is offline Offline
13 posts
since Sep 2008
Apr 13th, 2009
0

Re: Scanner Input from Keyboard help

if(letter == "$PART2")

You can't test a String's contents by using '=='. It only works for primitive types, String is an Object. Use the String class's equal method
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Apr 14th, 2009
0

Re: Scanner Input from Keyboard help

if(letter == "$PART2")

You can't test a String's contents by using '=='. It only works for primitive types, String is an Object. Use the String class's equal method
Just correcting, he means the equals() method of the String class
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Problem with List<E>
Next Thread in Java Forum Timeline: Full-Screen Exclusive Mode: "Display Modes" question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC