CorDell
CorDell

Reputation: 57

I'm not able to get my stack to work

I am reading in from a file numbers spaced out by _ (1-9) and then with each number you do something to a stack. I'm just trying to get my case to read each item in the array and do something for each number but i can't seem to get it to work.

 public static void main(String[] args) throws FileNotFoundException {
    FileReader file = new FileReader("textfile.txt");
    int[] integers;
    integers = new int[100];
    int i = 0;
    try (Scanner input = new Scanner(file)) {
        while (input.hasNext()) {
            integers[i] = input.nextInt();
            i++;
        }

        Stack<Integer> nums = new Stack<>();
        int number = integers[i];
        switch (number) {
            case '1':
                nums.push(5);
                System.out.println(nums.peek());
                break;
        }
    } catch (Exception e) {
    }
}

Upvotes: 0

Views: 42

Answers (1)

Riaz
Riaz

Reputation: 874

In your switch statement, take the single quotes out from the number 1.

'1' is of type char

1 is of type int

Also, when you try to get a number here:

int number = integers[i];

It's always going to be 0 because i is now an index greater than what you've actually populated in your array.

Upvotes: 2

Related Questions