Scanner Input from Keyboard help

Reply

Join Date: Sep 2008
Posts: 9
Reputation: yingfo is on a distinguished road 
Solved Threads: 0
yingfo yingfo is offline Offline
Newbie Poster

Scanner Input from Keyboard help

 
0
  #1
Apr 13th, 2009
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.


  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,580
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 199
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Scanner Input from Keyboard help

 
0
  #2
Apr 13th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Scanner Input from Keyboard help

 
0
  #3
Apr 14th, 2009
Originally Posted by BestJewSinceJC View Post
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
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC