We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,382 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Scanner Input from Keyboard help

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.

import java.util.*;
import java.io.*;
import java.util.Scanner;
import java.util.Stack;


public class PostEval
{

 static public void main(String args[])
 {

     Scanner scanner = new Scanner(System.in);
  Stack<String> postFix = new Stack<String>();
  String part2 = "$PART2";
     System.out.println("Input Letter and Number it equals: ");
  while(scanner.hasNext())
  {
        String letter = scanner.next();
     char equals=(scanner.next()).charAt(0);
        int number = scanner.nextInt();
     System.out.println("You Inputted:"+ letter+ equals+ number);
     if(letter == "$PART2")
     {
         while(scanner.hasNext())
         {
           String postfix = scanner.nextLine();
           System.out.println(postfix);
         }
     }
   
  }
3
Contributors
2
Replies
7 Hours
Discussion Span
4 Years Ago
Last Updated
3
Views
yingfo
Newbie Poster
13 posts since Sep 2008
Reputation Points: 46
Solved Threads: 0
Skill Endorsements: 0

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

BestJewSinceJC
Posting Maven
2,774 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Skill Endorsements: 13

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

stephen84s
Nearly a Posting Virtuoso
1,444 posts since Jul 2007
Reputation Points: 668
Solved Threads: 156
Skill Endorsements: 10

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0722 seconds using 2.66MB