0

I am new to JAVA and trying to learn. Apologies in advance if my question is basic and not in the correct format.

I am trying to understand how I can get the max int value in a stack please?

private Stack<Integer> stack = new Stack<>();
stack.push(2456785434);
stack.push(1);

essentially, the stack has two integers but I want to return the maximum (2456785434) into a variable. How do I do it please?

Below is the method which is adding the two numbers the user types (these are parsed into int values) - firstNumber and secondNumber.

private int performCalc(int firstNumber, int secondNumber, String op) {
    if (s.equals("+")) {
        return secondNumber + firstNumber;
    } else if (s.equals("-")) {
        return secondNumber - firstNumber;
    } else if (s.equals("*")) {
        return secondNumber * firstNumber;
    } else if (s.equals("/")) {
        return secondNumber / firstNumber;
    } else if (s.equals("%")) {
        return secondNumber % firstNumber;
    } else {
        return 0;
    }
}
AM_86
  • 115
  • 7
  • I'd like to retain the stack please. – AM_86 Nov 08 '20 at 17:44
  • You seem to be learning Java from some very old material? The `Stack` class is legacy and no longer used. I’d use `ArrayList` or `ArrayDeque`. `Stack` works, you can use it if you insist. – Ole V.V. Nov 08 '20 at 18:27
  • What did your search bring up? I’m sure that similar questions have been asked and answered many times already. – Ole V.V. Nov 08 '20 at 18:29

2 Answers2

2

You can use the Collections class for this

Integer maxValue = Collections.max(stack)
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • 1
    Joakim Danielson - I agree with you. IMHO, marking a question as a duplicate is fine but the genuine effort should not be devalued. – Arvind Kumar Avinash Nov 08 '20 at 19:58
  • *Why the downvote?* Just guessing: Because someone thinks that we shouldn’t answer such a poorly researched question (again)? Because from the discussion under the other answer there is doubt whether the OP really wants the max element from the entire stack always? If this is indeed desired, your way is the way I prefer. – Ole V.V. Nov 08 '20 at 19:58
  • @JoakimDanielson It wasn't me who voted it down. – AM_86 Nov 08 '20 at 20:43
0

First of all, 2456785434 is out of range for int which can have a value only from -2147483648 to 2147483647.

You can use the Stream API to find the maximum value from the stack.

import java.util.Comparator;
import java.util.Optional;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(245678544);
        stack.push(1);

        Optional<Integer> max = stack.stream().max(Comparator.naturalOrder());
        System.out.println(max.get());
    }
}

Output:

245678544
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Sorry no need to store in a variable then i.e. int. Just want to return the largest value in the stack. – AM_86 Nov 08 '20 at 17:50
  • @AM_86 - When you return a value from some method, it has to be collected into some variable and that is why you need to declare the return type. If you already know that your numbers can be out of the range of `int`, you can choose a bigger type like `long` (i.e. `Stack`) or even bigger `BigInteger` as per your requirement. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash Nov 08 '20 at 17:54
  • @AM_86 That’s not really clear to me, sorry. Are you saying that you want to write a method that takes the `Stack` as an argument and returns the largest value in the `Stack`? And if so, would the `Stack` be allowed to be empty, and if so, what should happen in that situation? You are correct, to return the max from your method you would not need to store it in a variable first. – Ole V.V. Nov 08 '20 at 18:56
  • @OleV.V. - I haven't said that you have to explicitly declare a variable to collect the return value. I've said that it has to be collected into some variable (which may be implicit variable provide by JVM) and that is why you need to declare the return type. Isn't it a correct statement? – Arvind Kumar Avinash Nov 08 '20 at 19:05
  • @OleV.V. basically, I am trying to build a SRPN calculator where by the user adds to the stack via manual input (readline method). The user types in 2147483674 and then types in 1. Then the user types in + and then =. The result I get as per my current code is -2147483648. The actual result returned should be 2147483647 (note 7 at the end and not 8). I am not able to match this functionality of the SRPN so far. I hope this helps and if you could help please. – AM_86 Nov 08 '20 at 19:06
  • Oh yes, @Arvind, there’s nothing wrong with your statements. I’m just trying to understand better what AM_86 is after so we may be able to help better. – Ole V.V. Nov 08 '20 at 19:15
  • OleV.V. - Thanks for confirming. @AM_86 - When the value exceeds the maximum range, it goes to the other end e.g. `Integer.MAX_VALUE + 1` will become `Integer.MIN_VALUE`. – Arvind Kumar Avinash Nov 08 '20 at 19:20
  • @ArvindKumarAvinash Thanks understood. However, not sure how to make the change so that the max value is printed and not the min value. – AM_86 Nov 08 '20 at 19:27
  • @AM_86 - As I said earlier, you can use `Stack` or `Stack` for operation on bigger integers. – Arvind Kumar Avinash Nov 08 '20 at 19:53
  • Do you always want the max by overflow? And the ordinary sum if it doesn’t overflow? If the user tries to add 2 000 000 000 and 1 000 000 000, then the result should be 2 000 000 000? (Not 2147483647 and certainly not -1294967296.) Also if the stack is high, do you want just the greater of the two topmost elements, or the max element from the entire stack? – Ole V.V. Nov 08 '20 at 19:54
  • 1
    @OleV.V.Yes your assumption is correct. If the sum exceeds 2147483647 then return 2147483647. I will amend my question and provide my method. I hope that helps and you can explain a possible solution. – AM_86 Nov 08 '20 at 20:02
  • You may use `Math.addExact()` for an addition that checks for overflow. In case of an `ArithmeticException` from that method you may look at the sign of one of the addends to determine whether the result was larger than `Integer.MAX_VALUE` or less than `Integer.MIN_VALUE` and return one of those constant values as appropriate. – Ole V.V. Nov 08 '20 at 20:24