1

This code is calculator for prifix. first input means number of line to calculate, and then input expression.

e.g) 2\n 12+3*\n 31/2-4*2+\n

And It works well in eclipse but did not work in scoring site. Scoring site said that

Exception: java.util.NoSuchElementException Maybe at: at Calculation.main(Calculation.java:16)

line 16 is 'String string=scan2.nextLine();'

But I cannot find out where is wrong.

    import java.util.*;
    public class Calculation{

        public static void main(String[] args){

            System.out.print("Enter a number: ");
            Scanner scan1=new Scanner(System.in);
            int n=scan1.nextInt();

            ArrayList<String> input=new ArrayList<String>();

            for(int i=0;i<n;i++){

                System.out.print("Enter a expression in postfix: ");
                Scanner scan2=new Scanner(System.in);
                String string=scan2.nextLine();
                input.add(i,string);

            }

            Stack<Integer> stack=new Stack<Integer>();

            for(int i=0;i<=input.size()-1;i++){
                for(int j=0;j<=input.get(i).length()-1;j++){
                    if(input.get(i).charAt(j)>=48&&input.get(i).charAt(j)<=57)
                        stack.push(Integer.parseInt(input.get(i).valueOf(input.get(i).charAt(j))));
                    else{
                        int n2=stack.pop();
                        int n1=stack.pop();

                        if(input.get(i).charAt(j)=='+')
                            stack.push(n1+n2);
                        if(input.get(i).charAt(j)=='-')
                            stack.push(n1-n2);
                        if(input.get(i).charAt(j)=='/')
                            stack.push(n1/n2);
                        if(input.get(i).charAt(j)=='*')
                            stack.push(n1*n2);          
                    }   

                }

                int output=stack.pop();
                System.out.println(output);
            }

        }

    }
HaveNoDisplayName
  • 7,711
  • 106
  • 32
  • 44
J.Lim
  • 11
  • 1
  • Is the scoring site using test-cases for checking your algorithm or is it just an online compiler? – burglarhobbit Sep 13 '15 at 08:25
  • Don't open more than one scanner for the same input stream. And pay attention to [clearing the scanner after `nextInt`](http://stackoverflow.com/q/13102045/4125191). – RealSkeptic Sep 13 '15 at 08:35

3 Answers3

0

the thing is when you use

String str=scan1.nextLine();

it will only read your input until space after that it won't so to read a string input just use

String str=scan1.next();
Bhawesh Chandola
  • 489
  • 4
  • 18
0

Read a full line to stay in sync with line endings. Don't use another scanner.

  int n=scan1.nextInt();
  scan1.nextLine();
  ArrayList<String> input=new ArrayList<String>();
  for(int i=0;i<n;i++){
       System.out.print("Enter a expression in postfix: ");
       String string=scan1.nextLine();
       input.add(i,string);
  }
laune
  • 30,276
  • 3
  • 26
  • 40
0

When you're doing scan2.nextLine(); previous line break after nextInt() was not processed that's why that newline character still in buffer got read through your scan2.nextLine() instead of your new input.

You can handle it two ways -

  1. Do an empty scan1.nextLine() immediately after that first nextInt() call.

  2. or Instead of using nextInt(), use nextLine() and using Integer.parseInt() convert that string to integer.

Raman Shrivastava
  • 2,827
  • 12
  • 25