hello people...

simple doubt

class test
{
public static long testApp(long number)
{
return number;
}
}

when i pass "45638" it returns perfectly
but when i pass "23837472"; it's returning not in the range. what's the issue as long has very big value/range..

and how do i pass a number as an argument of any size.

That is because 23837472 is not a long it is an int. This is a long: 23837472L
Try this:

long l = testApp(23837472L);

When you use the 45638, that int number is turned into a long. The reason why you need to add the 'L' is because if you don't, it sees the 23837472 as an int and it is too big for an int.

what if user doesnt know how to give input and he gives like the normal newbie who doesnt knw any programming... like "343548357848573" still i need to handle it w/o problem.

please dont suggest "BigInteger" i am not supposed to use that.

With your latest post, it seems that you need to give use some more information about what is your problem and what you want your program to do.

This: "343548357848573" is a String. If the user enters it from a GUI for example you can retrieve it only as a String.

If you use Integer.parseInt or even Long.parseLong and it is too big you will get an exception.

If you must have that value as a numeric then use the classes BigInteger or similar.

what if user doesnt know how to give input and he gives like the normal newbie who doesnt knw any programming

Cast it to a long, or use nextLong instead of nextInt, if you're reading from a Scanner on the input, or if you're using Integer.parseInt() use Long.parseLong(). As always, use a try block around nextLong() or parseLong(). If it's too big for a Long, you're stuck, but that's pretty big. They'll probably get tired before they enter all those digits.

EDIT:
for the record, maxLong is 9223372036854775807 - about ten digits longer than the average uesr's attention span. :)
You can get that value easily enough, if you need it:

System.out.println(Long.MAX_VALUE);

With your latest post, it seems that you need to give use some more information about what is your problem and what you want your program to do.

This: "343548357848573" is a String. If the use enters it from a GUI for example you can retrieve it only as a String.

If you use Integer.parseInt or even Long.parseLong and it is too big you will get an exception.

If you must have that value as a numeric then use the classes BigInteger or similar.

Sorry I didn't know about BigInteger. Better tell us what your program needs to do. Why must you have that value: "343548357848573" saved as numeric and not as String ?

i got a task to write a method where input is a number of any size. that's it. no limitation of data size. only thing is it's an integer number of any size. so i dnt have control over input. all i need to handle how to carry it into function

Do you have to do anything with the number, or just accept it and return it?

Then save it as a String and then have a method that checks if that String represents a number. Remember the value would be saved as a String.

For validation, you can take each character of that String using for loop.
Check the method charAt(int i) of the String class. Then take each character and check if it is a number using a method from the Character class.

If you find that a character from that String is not a number then display a message and don't continue with the rest of the program.

i can handle using string. but my instructor asked me to take as number only rather than string (for knowledge purpose only - extra task)...


i have some manipulation in function. but leave it asap

it's like writing custom BigInteger function without using default one

I can't imagine what it is he wants you to do. Taking it as a String would be the normal thing - as JA said, you'd take the String, validate it, and pass it back.

There isn't a primitive number type that has arbitrary capacity - that's by definition, a primitive is a fixed number of bytes. So does he want you to write a class that holds an arbitrary-length int? You'd still be taking it in as a String, but everything's a String when it comes in - nextInt() takes a String to begin with, and then converts it to an int.

So that'll be my best guess: write a wrapperclass, call it "ArbitraryInt". Constructor takes a String, declares an array of int, same length as the String, and puts digits into the array, throwing an exception if it finds a non-digit.
toString should be obvious, you could write simple arithmetic routines if you felt particularly up to it. compareTo() shouldn't be hard.
Cheesy, yes, but maybe it's what he's after. Can't think what else it might be.

[EDIT: just re-read previous post, and I think this is actually what he wants you to do. ]

thanks for suggestions and solutions. i have suggested same string solution to him. but he asked me to take this up. will let u know tomorrow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.