2

I'll admit, I've gone through quite a few (similar) questions, but I can't seem to comprehend the contextual usage in the following piece of code I wrote for an elementary SPOJ problem (http://www.spoj.com/problems/ONP/):

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


public class onp1 {

    public static String postfixString(String expression) {

        // Stack <Character> valueStack = new Stack <Character>();
        Stack <Character> operatorStack = new Stack <Character>();
        String output = "";
        char[] tokens = expression.toCharArray();

        for(char c : tokens) {

            if(c == '('){
                continue;
            }

            else if(c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {
                operatorStack.push(c);
                continue;
            }

            else if(c == ')') {
                output += operatorStack.pop();

                continue;
            }

            else {
                    output += String.valueOf(c);
                continue;
            }
        }
        return output;
    }
    public static void main (String [] args)throws java.lang.Exception {

        String inputString = "";
        int n1;
        Scanner in = new Scanner(System.in);
        try
        {
            n1 = in.nextInt();
            StringBuilder[] sb = new StringBuilder[n1];
            for(int i = 0; i < n1; i++) {

                sb[i] = new StringBuilder();



                inputString = in.next();

                sb[i].append(postfixString(inputString)); 

            }


            for(int i = 0; i < n1; i++) {
                System.out.println(String.valueOf(sb[i]));
            }
        }
        catch (Exception e) {
           // System.out.println("");
           System.out.println(e.getMessage());    
            // numberOfTestCases.next();
        }
        System.exit(0);
    }

}

If I use nextLine() instead of next(), the SPOJ engine generates a 'Wrong Answer' response.

Also, there seems to be some issue while using a StringBuilder object instead of a String object in the postfixString function (I was using a StringBuilder object earlier; was returning a string using the 'toString()' method).

Please ignore the logical inconsistencies (I know there are a few). I've already ironed (most) of them out. What's driving me crazy are nextLine() vs next() and StringBuilder vs String issues.

Manuel Allenspach
  • 11,309
  • 13
  • 49
  • 72
AbhyudayaR
  • 51
  • 4
  • 3
    "I can't seem to comprehend the contextual usage" I don't understand what you're asking. `next()` returns the next token (separated by whitespace, by default), and `nextLine()` returns the next line. Unless you show us the code with and without `StringBuilder` I don't see how anyone can help you with that. – David Conrad Feb 23 '15 at 17:18

2 Answers2

2

next() will only return what comes before a space. nextLine() returns the current line and moves the scanner down to the next line.

nLee
  • 1,230
  • 2
  • 10
  • 20
1

if you have space in between your input String like "(a + b) * c" then next method will give ( followed by a then + then b.

While if you use nextLine it will read whole line at once.

String is an immutable class while StringBuilder isn't. Meaning String once created can't be change. So moment you do "str1" + "str2", it creates three string object, "str1" then "str2" and then "str1str2". While if you use StringBuilder's append method, you just keep on adding to same object and then at final once when you are done appending the strings, you call toString on StringBuilder to create final String object just once.

SMA
  • 33,915
  • 6
  • 43
  • 65