-3

I'm New to java, and have been trying out different things in learning, but recently I ran into this error

error: cannot find symbol

I've searched for answers on google but I still am not able to fully grasp the problem and it's solution. Maybe more experienced people here can help me out.

This is the code that I've been trying to run:

public class simple
{
    public static void main(String arg[])
    {
        BigInteger largeValue= new BigInteger(Integer.MAX_VALUE +1);
        for( int i=0; i < 4 ; i++)
        {
            System.out.println(largeValue);
            largeValue = largeValue.add(BigInteger.ONE);   
        }
    }
}

This is the error that I'm getting:

simple.java:5: error: cannot find symbol
                BigInteger largeValue= new BigInteger(Integer.MAX_VALUE +1);
                ^
  symbol:   class BigInteger
  location: class simple
simple.java:5: error: cannot find symbol
                BigInteger largeValue= new BigInteger(Integer.MAX_VALUE +1);
                                           ^
  symbol:   class BigInteger
  location: class simple
simple.java:9: error: cannot find symbol
                        largeValue = largeValue.add(BigInteger.ONE);
                                                    ^
  symbol:   variable BigInteger
  location: class simple
3 errors
Holger
  • 285,553
  • 42
  • 434
  • 765
Samar Khan
  • 11
  • 1
  • 3
    In java, types have a fully qualified name. For example, it's `java.math.BigInteger`, not just `BigInteger`. It's a lot to type, so java has an alias system specifically for types: You stick `import java.math.BigInteger;` at the top, which doesn't do anything, except, that says: Whereever I type `BigInteger` in this file where a type is needed, assume I meant to type `java.math.BigInteger`. You didn't do that - so java has no idea what the heck `BigInteger` might be. String works because all java files have a silent `import java.lang.*;` at the top. Because spec says so. – rzwitserloot Aug 26 '23 at 20:51

1 Answers1

1

The code utilizes the BigInteger class to handle large integer values. Instead of directly attempting to create a BigInteger from Integer.MAX_VALUE + 1, it first converts the maximum value of an int to a string and then constructs a BigInteger from that string. Then, within a for loop, the code gradually increments the value of largeValue by one using the add(BigInteger.ONE) method and prints it in each iteration. This ensures that the numbers are handled correctly by BigInteger and prevents errors due to value overflow. Ejemplo:

   import java.math.BigInteger;

public class Simple {
    public static void main(String[] args) {
        BigInteger largeValue = new BigInteger(Integer.toString(Integer.MAX_VALUE)).add(BigInteger.ONE);
        for (int i = 0; i < 4; i++) {
            System.out.println(largeValue);
            largeValue = largeValue.add(BigInteger.ONE);
        }
    }
}
  • 1
    There is no need for a `String` detour. Instead of `new BigInteger(Integer.toString( Integer.MAX_VALUE))` you can use `BigInteger.valueOf(Integer.MAX_VALUE)`. Or you replace the entire `new BigInteger(Integer.toString(Integer.MAX_VALUE)).add(BigInteger.ONE)` with `BigInteger.valueOf(Integer.MAX_VALUE + 1L)` (mind the `L`). – Holger Aug 28 '23 at 08:11