1

I keep getting this error on my code. I think its to do with my parseInt() command but im not sure. Im trying to create a stack of integers that come from a user input string such as "PUSH 5" and just extract the 5 from the string to push into the stack.

import java.util.*;

public class lab6
{
    public static void main(String []args)
    {
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        
        lab6stack theStack = new lab6stack(size);
        String [] ar = new String [size];
        
        for(int i = 0; i < size; i++)
        {
            ar[i] = sc.next();
            
            if(ar[i].charAt(1) == 'U')
            {
                String sub = ar[i].substring(4);
                int num = Integer.parseInt(sub);
                
                theStack.push(num); 
            }
            
            else if(ar[i].charAt(1) == 'O')
            {
                theStack.pop();
            }
        }
        
        while (!theStack.isEmpty())
        {
            System.out.println(theStack.pop());
        }
    }
}
Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • If your goal is to read entire text `PUSH 5` as one string then you need to use `nextLine()` for that. Just be careful if you are using it after `next()`, `nextInt()` or other `nextABC()` methods since they don't consume line separators which causes `nextLine()` to see them immediately which is interpreted as user pressign enter without providing any data. Solution and longer discussion is at ["Scanner is skipping nextLine() after using next() or nextFoo()?"](https://stackoverflow.com/q/13102045). – Pshemo Nov 18 '20 at 21:21

3 Answers3

1

As others have said, sc.next() only returns "PUSH", not "PUSH 5". In my opinion, @Pshemo's solution works just fine, but you said that it is not how you want to do it. In that case, you can replace your sc.next() with sc.nextLine() to get "PUSH 5". That way, you can keep the rest of your code the same.

Hope this is what you want!

null_awe
  • 408
  • 2
  • 13
  • appreciate the answer but after i changed it to nextLine() i get an out of bounds error from the line "ar[i].charAt(1) == 'U'" – Danial Farhan Nov 18 '20 at 20:19
  • Another day, another person is suffering from ["Scanner is skipping nextLine() after using next() or nextFoo()?"](https://stackoverflow.com/q/13102045)). – Pshemo Nov 18 '20 at 21:23
0

You're referring to the space between PUSH and your number in this line:

String sub = ar[i].substring(4);

As substrings also start counting on zero, your number should be parsed when using substring(5); here instead. Here is an example with indexes:

"P"=0 ; "U"=1 ; "S"=2 ; "H"=3 ; " "=4 ; "3"=5
0

This is because you are not checking the length of the string before grabbing the character.

here:

if(ar[i].charAt(1) == 'U')

and here:

else if(ar[i].charAt(1) == 'O')

Check with ar[i].length() >= 1. For example:

if(ar[i].length() >= 1 && ar[i].charAt(1) == 'U')

You will also need to apply the other two fixes addressed in the other answers.

Use sc.nextLine() when reading the input and use substring(5) in the "PUSH" if case.

John Mercier
  • 1,433
  • 1
  • 15
  • 33