#When i put an input of 12 my output is 1 does anyone know why

1 messages · Page 1 of 1 (latest)

candid patrol
#

public String infixToPostfix(){

    String delims = "+-*/() ";
    StringTokenizer strToken = new StringTokenizer(jtfInfix.getText(), delims, true);
    Stack<Character> stack = new Stack<>();
    String postfix = "";

    while(strToken.hasMoreTokens()) {

        String token = strToken.nextToken().trim();
        char c = token.charAt(0);
        //checks to see if the input is a integer
        if(Character.isDigit(c))
            postfix += c;
        //checks for open Parenthesis
        else if(c == '(')
            stack.push(c);
        //checks for closed Parenthesis
        else if(c == ')') {
            while (!stack.isEmpty() && stack.peek() != '(') 
                postfix += stack.pop() + " ";
            if (!stack.isEmpty() && stack.peek() == '(') 
                stack.pop();
        }
        else if(delims.indexOf(c) != -1) {
            if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '*' || stack.peek() == '/')))
                postfix += stack.pop() + " ";
            else if((c == '*' || c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
                stack.push(c);
            else
                postfix += c + " ";
        }

    }

    while (!stack.isEmpty()) {
        postfix += stack.pop() + " ";
    }

    return postfix.trim();

}
#

this is a method that converts infix to postfi

bitter mica
#

;compile java

import java.util.*;
public class M 
{
  public String infixToPostfix()
  {

        String delims = "+-/() ";
        StringTokenizer strToken = new StringTokenizer(jtfInfix.getText(), delims, true);
        Stack<Character> stack = new Stack<>();
        String postfix = "";

        while(strToken.hasMoreTokens()) 
        {
            String token = strToken.nextToken().trim();
            char c = token.charAt(0);
            //checks to see if the input is a integer
            if(Character.isDigit(c))
                postfix += c;
            //checks for open Parenthesis
            else if(c == '(')
                stack.push(c);
            //checks for closed Parenthesis
            else if(c == ')') {
                while (!stack.isEmpty() && stack.peek() != '(') 
                    postfix += stack.pop() + " ";
                if (!stack.isEmpty() && stack.peek() == '(') 
                    stack.pop();
            }
            else if(delims.indexOf(c) != -1) {
                if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' ||  stack.peek() == '/')))
                    postfix += stack.pop() + " ";
                else if((c == '*' ||  c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
                    stack.push(c);
                else
                    postfix += c + " ";
            }

        }

        while (!stack.isEmpty()) {
            postfix += stack.pop() + " ";
        }

        return postfix.trim();

    }
}
terse spireBOT
#
Compiler Output
<source>:44: error: empty character literal
if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' ||  stack.peek() == '/')))
                                                                  ^
<source>:44: error: not a statement
if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' ||  stack.peek() == '/')))
                                                                                      ^
<source>:44: error: ';' expected
if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' ||  stack.peek() == '/')))
                                                                                            ^
<source>:47: error: 'else' without 'if'
else if((c == '*' ||  c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
^
4 errors
<source>:44: error: empty character literal
if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' ||  stack.peek() == '/')))
                                                     
bitter mica
#

try copy-paste with ```
this929441123200548954

#

;compile java

import java.util.*;
public class M 
{
public String infixToPostfix()
{

        String delims = "+-/() ";
        StringTokenizer strToken = new StringTokenizer(jtfInfix.getText(), delims, true);
        Stack<Character> stack = new Stack<Character>();
        String postfix = "";

        while(strToken.hasMoreTokens())
        {
            String token = strToken.nextToken().trim();
            char c = token.charAt(0);
            //checks to see if the input is a integer
            if(Character.isDigit(c))
            {    postfix += c;}
            //checks for open Parenthesis
            else if(c == '(')
            {    stack.push(c);}
            //checks for closed Parenthesis
            else if(c == ')') {
                while (!stack.isEmpty() && stack.peek() != '(') 
                {    postfix += stack.pop() + " ";}
                if (!stack.isEmpty() && stack.peek() == '(') 
                 {   stack.pop();}
            }
            else if(delims.indexOf(c) != -1) {
                if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' || stack.peek() == '/')))
                {    postfix += stack.pop() + " ";}
                else if((c == '*' || c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
                {    stack.push(c);}
                else
                {    postfix += c + " ";}
            }

        }

        while (!stack.isEmpty()) {
            postfix += stack.pop() + " ";
        }

        return postfix.trim();

    }
public static void main(String args[]) { }
}
terse spireBOT
#
Compiler Output
<source>:30: error: empty character literal
                if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' || stack.peek() == '/')))
                                                                                  ^
<source>:30: error: not a statement
                if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' || stack.peek() == '/')))
                                                                                                     ^
<source>:30: error: ';' expected
                if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '' || stack.peek() == '/')))
                                                                                                           ^
<source>:32: error: 'else' without 'if'
                else if((c == '*' || c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
                ^
4 errors
<source>:30: error: empty character literal
                if((c == '-
bitter mica
#

your code does not compile

candid patrol
#
        String jtfInfix = "12+3"
        String delims = "+-/() ";
        StringTokenizer strToken = new StringTokenizer(jtfInfix, delims, true);
        Stack<Character> stack = new Stack<>();
        String postfix = "";

        while(strToken.hasMoreTokens()) {

            String token = strToken.nextToken().trim();
            char c = token.charAt(0);
            //checks to see if the input is a integer
            if(Character.isDigit(c))
                postfix += c;
            //checks for open Parenthesis
            else if(c == '(')
                stack.push(c);
            //checks for closed Parenthesis
            else if(c == ')') {
                while (!stack.isEmpty() && stack.peek() != '(') 
                    postfix += stack.pop() + " ";
                if (!stack.isEmpty() && stack.peek() == '(') 
                    stack.pop();
            }
            else if(delims.indexOf(c) != -1) {
                if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == ''  stack.peek() == '/')))
                    postfix += stack.pop() + " ";
                else if((c == '*'  c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
                    stack.push(c);
                else
                    postfix += c + " ";
            }

        }

        while (!stack.isEmpty()) {
            postfix += stack.pop() + " ";
        }

        return postfix.trim();

    } ```
#

;compile java

terse spireBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

candid patrol
#

;compile java public String infixToPostfix(){
String jtfInfix = "12+3"
String delims = "+-/() ";
StringTokenizer strToken = new StringTokenizer(jtfInfix, delims, true);
Stack<Character> stack = new Stack<>();
String postfix = "";

    while(strToken.hasMoreTokens()) {

        String token = strToken.nextToken().trim();
        char c = token.charAt(0);
        //checks to see if the input is a integer
        if(Character.isDigit(c))
            postfix += c;
        //checks for open Parenthesis
        else if(c == '(')
            stack.push(c);
        //checks for closed Parenthesis
        else if(c == ')') {
            while (!stack.isEmpty() && stack.peek() != '(') 
                postfix += stack.pop() + " ";
            if (!stack.isEmpty() && stack.peek() == '(') 
                stack.pop();
        }
        else if(delims.indexOf(c) != -1) {
            if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == ''  stack.peek() == '/')))
                postfix += stack.pop() + " ";
            else if((c == '*'  c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
                stack.push(c);
            else
                postfix += c + " ";
        }

    }

    while (!stack.isEmpty()) {
        postfix += stack.pop() + " ";
    }

    return postfix.trim();

}
terse spireBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

bitter mica
#

always use braces

candid patrol
#

;compile java [public String infixToPostfix(){
String jtfInfix = "12+3"
String delims = "+-/() ";
StringTokenizer strToken = new StringTokenizer(jtfInfix, delims, true);
Stack<Character> stack = new Stack<>();
String postfix = "";

    while(strToken.hasMoreTokens()) {

        String token = strToken.nextToken().trim();
        char c = token.charAt(0);
        //checks to see if the input is a integer
        if(Character.isDigit(c))
            postfix += c;
        //checks for open Parenthesis
        else if(c == '(')
            stack.push(c);
        //checks for closed Parenthesis
        else if(c == ')') {
            while (!stack.isEmpty() && stack.peek() != '(') 
                postfix += stack.pop() + " ";
            if (!stack.isEmpty() && stack.peek() == '(') 
                stack.pop();
        }
        else if(delims.indexOf(c) != -1) {
            if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == ''  stack.peek() == '/')))
                postfix += stack.pop() + " ";
            else if((c == '*'  c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
                stack.push(c);
            else
                postfix += c + " ";
        }

    }

    while (!stack.isEmpty()) {
        postfix += stack.pop() + " ";
    }

    return postfix.trim();

} ]
terse spireBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

candid patrol
#

wdym

bitter mica
#

;compile java

import java.util.*;
public class M 
{
   public static final String infixToPostfix(String jtfInfixText)
   {
        final String delims = "+-/() ";
        final StringTokenizer strToken = new StringTokenizer(jtfInfixText, delims, true);
        final Stack<Character> stack = new Stack<Character>();
        String postfix = "";

        while(strToken.hasMoreTokens())
        {
            String token = strToken.nextToken().trim();
            char c = token.charAt(0);
            //checks to see if the input is a integer
            if(Character.isDigit(c))
            {    postfix += c;}
            //checks for open Parenthesis
            else if(c == '(')
            {    stack.push(c);}
            //checks for closed Parenthesis
            else if(c == ')') 
            {
                while (!stack.isEmpty() && stack.peek() != '(') 
                {    postfix += stack.pop() + " ";}
                if (!stack.isEmpty() && stack.peek() == '(') 
                 {   stack.pop();}
            }
            else if(delims.indexOf(c) != -1) {
                if((c == '-' || c =='+') && (!stack.isEmpty() && (stack.peek() == '*' || stack.peek() == '/')))
                {    postfix += stack.pop() + " ";}
                else if((c == '*' || c =='/') && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
                {    stack.push(c);}
                else
                {    postfix += c + " ";}
            }

        }

        while (!stack.isEmpty())
        {
            postfix += stack.pop() + " ";
        }

        return postfix.trim();

    }

    public static void main(String args[]) { }
}
terse spireBOT
#
Compilation successful

No output.

bitter mica
#

== '' is wrong

#

wtf is jtfInfix

#

ok passed that as a parameter

#

so what input should I try on this now static method ?

candid patrol
#

i fixed the problem thank u

#

if(Character.isDigit(c))
postfix += token + " ";

#

i forgot to add a space between each token

bitter mica
#

;compile java

import java.util.*;
public class M
{
  public static final String infixToPostfix(final String jtfInfixText)
  {
    final String delims = "+-/() ";
    final StringTokenizer strToken = new StringTokenizer(jtfInfixText, delims, true);
    final Stack <Character> stack = new Stack <Character> ();

    String postfix = "";

    while (strToken.hasMoreTokens())
    {
      final String token = strToken.nextToken().trim();
      final char c = token.charAt(0);
      //checks to see if the input is a integer
      if (Character.isDigit(c))
      {
        postfix += c;
      }
      //checks for open Parenthesis
      else if (c == '(')
      {
        stack.push(c);
      }
      //checks for closed Parenthesis
      else if (c == ')')
      {
        while (!stack.isEmpty() && stack.peek() != '(')
        {
          postfix += stack.pop() + " ";
        }
        
        if (!stack.isEmpty() && stack.peek() == '(')
        {
          stack.pop();
        }
      }
      else if (delims.indexOf(c) != -1)
      {
        if ((c == '-' || c == '+') 
        && (!stack.isEmpty() && (stack.peek() == '*' || stack.peek() == '/')))
        {
          postfix += stack.pop() + " ";
        }
        else if ((c == '*' || c == '/') 
        && (!stack.isEmpty() && (stack.peek() == '-' || stack.peek() == '+')))
        {
          stack.push(c);
        }
        else
        {
          postfix += c + " ";
        }
      }
    }

    while (!stack.isEmpty())
    {
      postfix += stack.pop() + " ";
    }

    return postfix.trim();
  }

  public static void main(String args[])
  {
  }
}
terse spireBOT
#
Compilation successful

No output.

bitter mica
#

postfix should be StringBuilder

candid patrol
#

idk if i can use StringBuilder cuz we never learned it in class

#

what does stringbuilder even do .

bitter mica
#

faster version of String s +=

#

I remember vaguely infix / postfix from school, so I am not sure I can help you

candid patrol
#

its all good bro

#

there isnt much information about this on the internet either

bitter mica