473,416 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Problems with a Java infix converter

I am having trouble getting my infix converter to work properly, I am kinda a newbee at programing and this is really killing me. I havent yet tried to incorporate parentheses yet but that is my next step.

Expand|Select|Wrap|Line Numbers
  1. package infix_postfix;
  2.  
  3. import java.util.*;
  4. public class Main {
  5.  
  6.  
  7.     public static void main(String[] args) {
  8.  Stack   stack  = new  Stack(); //uses java built in stack
  9. String input_string;
  10. Scanner input = new Scanner(System.in);
  11. int order=0;
  12. String temp="";
  13. String out="";
  14.             System.out.println("Infix Converter: V.1.0 Kelly Qualls");
  15.             System.out.println("This program is designed to take binary operators"
  16.                 + " including parenthetical math to be able to show the postfix"
  17.                 + " equivilent");
  18.             System.out.println("Please enter the infix notation:>");
  19.  
  20.             input_string=input.nextLine();
  21.            // String delims = "[+\\-*/\\^ ]+"; // so the delimiters are:  + - * / ^ space
  22.             String[] tokens = input_string.split("(?<=[ +\\-*/\\^ ]+)");
  23.             stack.push("out");
  24. for (int i = 0; i < tokens.length; i++)
  25.             {
  26. if (tokens[i].equals("+"))
  27.                 {
  28.                 temp=(String)stack.peek();
  29.                 if(temp.equals("out"))
  30.                     {
  31.                         stack.push("+");
  32.                     }
  33.                 else{
  34.                     if(temp.equals("*")||temp.equals("/"))
  35.                         {
  36.                         out += stack.pop();
  37.                         stack.push("+");
  38.                         }
  39.                     else
  40.                         stack.push("+");
  41.                 }
  42.                 }
  43. else if(tokens[i].equals("-"))
  44.                  {
  45.                 temp=(String)stack.peek();
  46.                 if(temp.equals("out"))
  47.                     {
  48.                         stack.push("-");
  49.                     }
  50.                 else{
  51.                     if(temp.equals("*")||temp.equals("/"))
  52.                         {
  53.                         out += stack.pop();
  54.                         stack.push("-");
  55.                         }
  56.                     else
  57.                         stack.push("-");
  58.                 }
  59.                 }
  60. else if(tokens[i].equals("/"))
  61.                 {
  62.                 temp=(String)stack.peek();
  63.                 if(temp.equals("out"))
  64.                     {
  65.                         stack.push("/");
  66.                     }
  67.                 else{
  68.                     if(temp.equals("*")||temp.equals("/"))
  69.                         {
  70.                         out += stack.pop();
  71.                         stack.push("/");
  72.                         }
  73.                     else
  74.                         stack.push("/");
  75.                 }
  76.                 }
  77. else if(tokens[i].equals("*"))
  78.                 {
  79.                 temp=(String)stack.peek();
  80.                 if(temp.equals("out"))
  81.                     {
  82.                         stack.push("*");
  83.                     }
  84.                 else{
  85.                     if(temp.equals("*")||temp.equals("/"))
  86.                         {
  87.                         out += stack.pop();
  88.                         stack.push("*");
  89.                         }
  90.                     else
  91.                         stack.push("*");
  92.                 }
  93.                 }
  94.             else {
  95.                 out += tokens[i];
  96.                 temp= (String)stack.peek();
  97.  
  98.             }
  99.  
  100. }
  101.             while(!temp.equals("out"))
  102.                 {
  103.                     out+=stack.pop();
  104.                     temp=(String)stack.peek();
  105.                 }
  106.            // order++;
  107.  
  108.  
  109.             System.out.println(out);
  110.  
  111. }
  112. }
  113.  
  114.  
Oct 19 '10 #1
1 1319
Dheeraj Joshi
1,123 Expert 1GB
Every time your for loop executes else condition in lone number 94. It won't execute any other if conditions. Please debug the code to see your self what is going wrong.

Expand|Select|Wrap|Line Numbers
  1. else {
  2.    out += tokens[i];
  3.    temp= (String)stack.peek();
  4. }
  5.  
Also Please see the variables in tokens array and see the problem with it.

Regards
Dheeraj Joshi
Oct 22 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Ravi | last post by:
Do we have anything that could convert C++ code to java code? To what extent are such tools reliable? TIA. -- main(){char s="sbwjAeftqbnnfe/dpn!ps!CSbwjACjhgppu/dpn"; int...
19
by: caramel | last post by:
i've been working on this program forever! now i'm stuck and going insane because i keep getting a syntax error msg and i just can't see what the compiler is signaling to! #include <stdio.h>...
6
by: PIEBALD | last post by:
Anyone got an infix to postfix (RPN) math notation converter? I've looked around a bit and haven't found anything quite what I want. I just want a method that will take a string in infix notation...
12
by: Jeff Calico | last post by:
I have 2 XML data files that I want to extract data from simultaneously and transform with XSLT to generate a report. The first file is huge and when XSLT builds the DOM tree in memory, it runs...
5
by: Thumbski | last post by:
Alright basically I just have one simple question. I can code perfectly fine and don't have any problems with the .cpp of the infix class but with my header I can't get anything to work really, any...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
4
by: Soren | last post by:
Hi, I want to control some motors using the parallel port.. however, my laptop does not have any parallel ports (very few do). What I do have is a USB->Parallel converter... I thought about...
1
by: aitia | last post by:
this the code. i used ECLIPSE to run this.. it has some codes smells that i can't seem to figure out.. can any one help? import java.io.*; import java.util.*; public class Postfix { private...
2
by: cioccolatina | last post by:
Hey guys, is there anyone who could help me..? I have file ExpressionBinaryTree.java : /** class ExpressionBinaryTree * uses a binary tree to represent binary expressions * does not...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.