0

The second line: [Ljava.lang.Object;@197a37c looks like what would print for an array of Object:
Object[] objArray ...

What did you try to print? If you want to see the contents of an array, use the Arrays class's toString() method: S.o.p("theArray=" + Arrays.toString(theArray));

Include an id string to describe what is being printed.

Edited by NormR1

0

No, it is not an array of Object but he is attempting to print out the Object itself. Look at line 134, the toString() is being called for Object class because stack[i] is an Object. He needs to cast the Object to something else...

0

No, it is not an array of Object

Not sure what you are saying. Try the following code:

      Object[] objArr = new Object[1];        // define an array of Objects
      System.out.println("objArr=" + objArr); // objArr=[Ljava.lang.Object;@1a79071

What about line 122? Without an ID String with the println, hard to know which statement was executed.

Edited by NormR1

0

That's correct too. However, an Object has default toString() which will display the name of object + hash value. An array is included. Either way, he needs to do something with the Object he is trying to display.

0

@NormR1 @Taywin,..I am confuse of my code i don't know how to ouptut the postfix value.

public class InfixToPostfix
{
     Object[] stack;
     int top;
     int size;

    public InfixToPostfix(){
    }

    public InfixToPostfix(int size)
    {
        stack=new Object[size];
        top=0;
    }

    public boolean isEmpty()
    {
        return top==0;
    }

    public boolean isFull()
    {
        return top==stack.length;
    }

    public boolean push(Object item)
    {
        boolean ok=!isFull();
        if(ok)
        {
            stack[top++]=item;
            ok= true;
        }

        return ok;
    }

    public Object peek()
    {
    //  return stack[top-1];

       Object s =null;

       if(!this.isEmpty())
          s = stack[top];
       else
         System.out.print("Stack is empty");      
      return s;
    }

    public Object pop()
    {
       Object item=peek();
       if(item!=null)
       {
         stack[top-1]=null;
         top--;

       }
        return item;
    }

    public int priority(char c)
    {
        int p=999;

        switch(c)
        {
            case '*': case  '/': p=0; break;
            case '+': case  '-': p=1; break;
        }
        return p;
    }


    public static void main(String...args)
    {
        InfixToPostfix s =new InfixToPostfix(10);
       Object[] Infix;
        Object[] postfix;

        int count=0;
        char ch='\u0000';

         System.out.print("Enter infix: ");

            String str =new java.util.Scanner(System.in).next();

         Infix=new Object[str.length()];

         postfix=new Object[str.length()];


         char [] cc = Infix.toString().toCharArray();
         for(int i=0;i < cc.length;i++)
         {

             ch=cc[i];

            if(Character.isDigit(ch)) 
                postfix[count++]=ch;

            else
            {
              if(!s.isEmpty())//stack is not empty...
              {
                 Character x = (char)s.peek();//Method s.peek() will be cast to char...

                  while(s.priority(x.charValue())<=s.priority(ch))//I used method charValue to get the value of x
                  {
                    postfix[count++]=s.pop();
                  }

                  s.push(ch);   
              }     
            }
         }


         while(!s.isEmpty())
         {
            postfix[count++]=s.pop();
         }
         postfix[count]=null;
         System.out.print(postfix.toString());// Displaying the postfix value

    }



}

I hope you will help me to print the postfix value...

0

Line 107, you cannot cast an object to primitive and assign to another object that way. Change that to Character x = (Character)s.peek(); in order to get it compiled.

I have to apologize to NormR1 about his/her method to display your postfix array. The Arrays class is smart enough to display the value inside Object class. So what you need to do is...

// Change line 124
 System.out.print(postfix.toString());
// to
 System.out.println(Arrays.toString(postfix));
// Also you need to add the line below at the top of your file (above
// the line 'class InfixToPostFix'
import java.util.Arrays;

You will see that your program is not working as expected though... Hope this help...

Edited by Taywin

0

I would add an ID String to line 4 so you can know what was printed in case there are more than one print out. Saves confusion:

System.out.println("postfix=" + Arrays.toString(postfix));
0

@NormR1 @Taywin,This is the output..

Enter infix: 1+2+3
Postfix= [4, 4, 1, 3, null]

Process completed.

0

And that's why I said it is not working as expected... What is the expectation of the result if your input is 1+2+3?

0

You should go back to the algorithm you are using and make sure it works. Then check that the code in the program follows the algorithm.
Can you post the steps the code should take to solve this problem?

0

@NormR1 @Taywin,This is would be the output 12+3+

I don't have algorithm.

0

OK, that may be the problem for you... The algorithm for converting infix to postfix could be found here. Also, let me comment a bit on your current code...

  • The input is a String
  • The output should also be a String
  • The program should be using Stack as a tempory container for sign symbol from the input
  • The class should be designed as static because it does not need to store anything
  • Using Object is way too complicated to deal with this problem because the problem itself is very specific -- expecting only digit characters & sign symbols as input.

Hope you read the link and try to understand what you should do next. Don't get discourage if you have to restart all over again. It could be even easier and simplier.

0

@Taywin...Thank you for this and the advice..I hope i can get this and understand easily.i will write back again to you as soon as i can.

0

@Taywin @NormR1..can i ask what is the difference in casting like this.

Character x = (Character)s.peek();

char x = (char)s.peek();

0

peek() returns an Object. char is a primitive. Character is a class.
You can not cast an object/class instance to a primitive like char.

0

Norm's answer was correct up to Java 1,5 Since then boxing and unboxing casts (ie between primitives and the appropriate wrapper class, eg char and Character) are explicitly defined and allowed in Java language spec:

5.5 Casting Conversion
Casting conversion is applied to the operand of a cast operator (§15.16): the type
of the operand expression must be converted to the type explicitly named by the
cast operator.
Casting contexts allow the use of one of:
• an identity conversion (§5.1.1)
• a widening primitive conversion (§5.1.2)
• a narrowing primitive conversion (§5.1.3)
• a widening and narrowing primitive conversion (§5.1.4)
• a widening reference conversion (§5.1.5) optionally followed by either an
unboxing conversion (§5.1.8) or an unchecked conversion (§5.1.9)
• a narrowing reference conversion (§5.1.6) optionally followed by either an
unboxing conversion (§5.1.8) or an unchecked conversion (§5.1.9)
a boxing conversion (§5.1.7) optionally followed by a widening reference
conversion (§5.1.5)
an unboxing conversion (§5.1.8) optionally followed by a widening primitive
conversion (§5.1.2).

...
** • An expression of a primitive type may undergo casting conversion to a reference
type without error, by boxing conversion.
• An expression of a reference type may undergo casting conversion to a primitive
type without error, by unboxing conversion.**

(my highlighting). So the answer for current Java versions is that casting between primities and referfence types is just like any other cast - it depends on the exact type of the operand and the cast operator, but for compatible types is perfectly legal.

Edited by JamesCherrill

0

Since peek() returns an Object, extra code would be needed.

     Character ac = 'a';    // boxed
     char cac1 = (char)ac;  // unbox   OK
     Object oac = ac;       // Change variable type to Object
//     char cac2 = (char)oac;  //inconvertible types
     char cac3 = (char)((Character)oac);  // cast then unbox
     System.out.println("ac=" + ac + ", cac1=" + cac1 + ", cac3=" + cac3);  // ac=a, cac1=a, cac3=a

Edited by NormR1

0

Did you try that with line 4?
The cast is a checked cast because oac is an Object reference, so it will compile without error, and at runtime it is found to contain a Character and unboxes it without error. That cast is OK, both at compile time and at runtime.
I just ran it (line 4 included) on Java Windows 64 Java 1.7 and it compiles correctly and runs correctly - just like the JLS says it should.
If you change line 3 to Object oac = "abc";
then it compiles correctly, but throws a runtime java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Character, also just as specified by the JLS for a checked cast.

Edited by JamesCherrill

0

@NormR1 @Taywin @JamesCherrill...I got now the answer i hope my code is correct...please correct me if i am wrong...also to @Taywin,i did not use static for my class i will just observe if this code will run without using static class..also i am still using Object and i want to observe if what should be the difference on using Object...

So here is my code

import java.util.Arrays;

public class InfixToPostfix
{
     Object[] stack;
     int top;
     int size;

    public InfixToPostfix(){
    }

    public InfixToPostfix(int size)
    {
        stack=new Object[size];
        top=0;
    }

    public boolean isEmpty()
    {
        return top==0;
    }

    public boolean isFull()
    {
        return top==stack.length;
    }

    public boolean push(Object item)
    {
        boolean ok=!isFull();
        if(ok)
        {
            stack[top++]=item;
            ok= true;
        }

        return ok;
    }

    public Object peek()
    {
    //  return stack[top-1];

       Object s =null;

       if(!this.isEmpty())
          s = stack[top-1];

      return s;
    }

    public Object pop()
    {
       Object item=peek();
       if(item!=null)
       {
         stack[top-1]=null;
         top--;

       }
        return item;
    }

    public int priority(char c)
    {
        int p=999;

        switch(c)
        {
            case '*': case  '/': p=0; break;
            case '+': case  '-': p=1; break;
        }
        return p;
    }


    public static void main(String...args)
    {
        InfixToPostfix s =new InfixToPostfix(10);
        Object[] Infix;
        Object[] postfix;

        int count=0;
        char ch='\u0000';

         System.out.print("Enter infix: ");

            String str =new java.util.Scanner(System.in).next();

         Infix=new Object[str.length()];
         postfix=new Object[str.length()];


         char [] cc = str.toCharArray();
         for(int i=0;i < Infix.length;i++)
         {

             ch=cc[i];

            if(Character.isDigit(ch)) 
                postfix[count++]=ch;

            else
            {


               if(!s.isEmpty())
               {
                  Character x = (Character)s.peek();
                  while(x!=null)
                  {
                    if(s.priority(x.charValue())<=s.priority(ch))
                     {
                        postfix[count++]=s.pop();
                        x=(Character)s.peek();
                         if(s.isEmpty())
                            s.push(ch);
                     }
                  }

               }
               else  
                  s.push(ch);   

            }
         }


         while(!s.isEmpty())
         {
            postfix[count++]=s.pop();
         }

         System.out.println("Postfix= " + Arrays.toString(postfix));

    }



}

And the output:

Enter infix: 1+2-4
Postfix= [1, 2, +, 4, -]

Process completed.

I hope you will correct my code if there is something wrong in usage...Thank you in advance.

0

@JamesCherill @NormR1...May i excuse?...i just want to ask what is boxing and unboxing?Thank you.

0

It compiles with errors with 1.6:

Running: D:\Java\jdk1.6.0_29\bin\javac.exe -Xlint  TestCode9.java

TestCode9.java:293: inconvertible types
found   : java.lang.Object
required: char
     char cac2 = (char)oac;  //inconvertible types
                       ^
1 error

1 error(s)

No errors with 1.7

0

@JamesCherill, I tried to change this...

 if(!s.isEmpty())
               {
                  Character x = (Character)s.peek();//this.....
                  while(x!=null)
                  {
                    if(s.priority(x.charValue())<=s.priority(ch))
                     {
                        postfix[count++]=s.pop();
                        x=(Character)s.peek();
                         if(s.isEmpty())
                            s.push(ch);
                     }
                  }

               }

to this....

 if(!s.isEmpty())
               {
                  char x = (char)s.peek();
                  while(x!=null)
                  {
                    if(s.priority(x.charValue())<=s.priority(ch))
                     {
                        postfix[count++]=s.pop();
                        x=(Character)s.peek();
                         if(s.isEmpty())
                            s.push(ch);
                     }
                  }

               }

When i tried to compile...

C:\InfixToPostfix.java:110: error: incomparable types: char and <null>
                  while(x!=null)
                         ^
C:\InfixToPostfix.java:112: error: char cannot be dereferenced
                    if(s.priority(x.charValue())<=s.priority(ch))
                                   ^
2 errors

Process completed.
0

So it's a 1.7 thing? I thought all the boxing/unboxing stuff was completed with 1.5, but obviously not (the JLS I quoted was 1.7 of course). Problem solved!
J

0

@NormR1 @JamesCherrill @Taywin...does my code is correct i got the right output now...please correct me if i am wrong.Thank you..

0

You would know if your code is correct by trying it with different input and see if the output is correct.
i.e.
input: 1+2+3 | output: 12+3+
input: 1+5*6/5-8+9 | output: 156*5/+8-9+
and so on...
Try it at least 4 different input to see if your code is correct. I tried and implemented your problem in a different way though. Didn't create or use a Stack class but simply a char[] with top index...

Edited by Taywin

0

@Taywin, I tried this

1+5*6/5-8+9

but there is no output,i think it is having infinite loop in the background...I don't know why...can you help me please.

0

Inside the if statement (line 107), you are a bit off there.

if(!s.isEmpty()) {
  Character x = (Character)s.peek();
  while(x!=null) {
    if(s.priority(x.charValue())<=s.priority(ch)) {
      postfix[count++]=s.pop();
      x=(Character)s.peek();
      if(s.isEmpty())
      s.push(ch);
    }
  }
}

The algorithm said you need to keep looping until either the priority of the operand inside the stack is less than the priority of the checking character or the stack is empty. If the condition is not satisfied, break out of the loop.

What you are doing is checking if the stack is empty first, and then priority. If the priority is not satisfied, the x keeps its current value. As a result, it goes into an infinit loop because x is never changed to be null.

Also, you need to push the operand regardless you pop any of the operand from the stack.

0

@Taywin,is this what you mean?..i am confuse with the algorithm i don't understand.

  if(!s.isEmpty())
               {
                  Character x = (Character)s.peek();

                while(s.isEmpty() || x!=null)
                 {


                    if(s.priority(x.charValue())<=s.priority(ch))
                     {
                        postfix[count++]=s.pop();
                        x=(Character)s.peek();
                         if(s.isEmpty())
                            s.push(ch);
                     }
                     else 
                     {
                        s.push(ch); 

                     }


                  }

               }
               else  
                  s.push(ch);   

            }
         }

Enter infix: 1+2*3
Postfix= [1, 2, 3, +, null]

Process completed.

0

Hmm... Maybe the site I gave you is not giving a clear algorithm. Here is the list of what you should be doing...

Define a stack (empty)
Go through each character in the string
    If it is between 0 to 9, append it to output string.
    Else If it is operator *+-/ then
        If the stack is empty push it to the stack
        Else (the stack is not empty) then start a loop:
            If the top of the stack has higher or equal priority
                Then pop and append to output string
            Else break
            Push the operator to the stack
    Else (invalid character)
        Handle the invalid character (either ignore or throw an error)
    End If
If there is any input in the stack pop and append to the output string.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.