java.io.StreamTokenizer.TT_NUMBER: Floating or Integer? - java

is there a way to find if the value parsed and returned by java.io.StreamTokenizer.nval (e.g. 200) was an integer or a floating number ?
Thanks
Edited:
I want to be able to know if the input was '200' or '200.0'.

I don't think it is possible with StringTokenizer, it's too old. You can use Scanner to do the job:
Scanner fi = new Scanner("string 200 200.0");
fi.useLocale(Locale.US);
while (fi.hasNext()) {
if (fi.hasNextInt()) {
System.out.println("Integer: " + fi.nextInt());
} else if (fi.hasNextDouble()) {
System.out.println("Double: " + fi.nextDouble());
} else {
System.out.println("String: " + fi.next());
}
}
Documentation for the class is here

As you are essentially trying to figure out if a double is an int or not, you can create a BigDecimal and test its scale() method. You could also use the Math static method floor() and test the return value against the original.
if(Math.floor(myStreamTokenizer.nval) == myStreamTokenizer.navl)) {
//treat it as an int
} else {
//treat it as a double
}
Of the two, I would use the Math.floor option, as it would be more efficient.

I would use the modulus operator:
if (yourNval % 1 > 0)
{
// nval is floating point.
}
else
{
// nval is an integer.
}
This works because modulus works with floating point numbers in Java as well as integers.
UPDATE: Since you specify a need to distinguish integers from doubles with zero-value decimal portions, I think you'll have to do something like the following:
Double(yourNval).toString().indexOf('.');
So a more-complete function will look like:
if (yourNval % 1 > 0)
{
// nval is floating point.
}
else
{
// nval is an integer.
// Find number of trailing zeroes.
String doubleStr = Double(yourNval).toString();
int decimalLoc = doubleStr.indexOf('.');
int numTrailingZeroes = 0;
if (decimalLoc > 0)
{
numTrailingZeroes = doubleStr.length() - decimalLoc - 1;
}
}

Related

How does recursion work and how can recursion be used to manipulate integer digits?

I'm trying to learn java, and I can't seem to understand recursion. I can understand how recursion can be used to add and do other basic math operations but how can recursion be used to reverse manipulate integers and individual integer digits.
example:
a method takes a single positive integer argument and displays its base five equivalent. 231 returns 1411 but the code below returns 1141. how would I reverse the order of integers put out?
public void base5(int n){
int rem=n%5;
int vis=n/5;
if(n!=0){
// System.out.print(rem/*+"|"*/);
//
// rem=(rem+rem)*10;
// System.out.print("\n||"+n+"||\n");
System.out.print(rem);
base5(vis);
}
else{
return;
}
}
The algorithm for getting individual digits of an integer, from right to left, is well known. See How to get the separate digits of an int number?.
I won't "explain" recursion, but I'll give you one possible solution for first problem:
a method takes a single positive integer and displays it with commas
inserted every three digits
import java.util.Scanner;
class Main {
public static void main( String [] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your positive integer: ");
long number = sc.nextLong();
String result = addCommas(number);
System.out.println(result);
}
public static String addCommas(long num) {
return addCommas(num, 1);
}
public static String addCommas(long num, int counter) {
if (num == 0) {
return ""; // base case ends recursion
}
else {
long digit = num % 10;
num = num / 10;
String comma = (counter%3==0 && num>0) ? "," : "";
// recursive call below because we call addCommas() again
return addCommas(num, counter+1) + comma + digit;
}
}
}
Here's a compact solution to the second problem:
a method takes a single positive integer and displays the result of
reversing its digits
public static String reverseDigits(long num) {
if (num == 0) {
return "";
}
else {
return String.valueOf(num % 10) + reverseDigits(num / 10);
}
}

NumberFormatException when parsing negative integer

I am writing a java program to determine if a number is a palindrome.
My code works properly if the argument passed is a positive integer, but throws a NumberFormatException when passing a negative integer.
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at com.stu.Main.isPalindrome(Main.java:28)
at com.stu.Main.main(Main.java:7)
The following solution I took from another stackoverflow thread, which seems to be what the instructor wants us to use, however in the while loop I assume that since a negative number is always less than 0, it will break out of the loop and not calculate the palindrome:
public static int reverse(int number)
{
int result = 0;
int remainder;
while (number > 0)
{
remainder = number % 10;
number = number / 10;
result = result * 10 + remainder;
}
return result;
}
So, I am using strings in my solution below to solve this issue.
Note: we have not yet got to splits and regexps yet.
public class Main {
public static void main(String[] args) {
isPalindrome(-1221); // throws exception
isPalindrome(707); // works as expected - returns true
isPalindrome(11212); // works as expected - returns false
isPalindrome(123321);// works as expected - returns true
}
public static boolean isPalindrome(int number){
if(number < 10 && number > -10) {
return false;
}
String origNumber = String.valueOf(number);
String reverse = "";
while(number > 0) {
reverse += String.valueOf(number % 10);
number /= 10;
}
if(Integer.parseInt(origNumber) == Integer.parseInt(reverse)) {
System.out.println("The original number was " + origNumber + " and the reverse is " + reverse);
System.out.println("Number is a palindrome!");
return true;
}
else
System.out.println("The original number was " + origNumber + " and the reverse is " + reverse);
System.out.println("Sorry, the number is NOT a palindrome!");
return false;
}
}
I am looking for two things here.
First, how do I solve the issue with the negative number in the while loop in the case of the instructors preferred solution?
Second, how can I solve the NumberFormatException in my solution?
Edit: a third question. Why does my solution return false if I never parse back to an int?
if(Integer.parseInt(origNumber) == Integer.parseInt(reverse)) // works
to
if(origNumber == reverse) // does not work
Thanks!
Ok, the easiest way to solve your first and second problem is just to remove the negative sign by using Math.abs(yourNumber) and that's it.
As,
The java.lang.Math.abs() returns the absolute value of a given argument.
If the argument is not negative, the argument is returned.
If the argument is negative, the negation of the argument is returned.
This will solve both of your first and second problem.
Coming to the third one, if you are not converting back to integer you are getting false as to compare string you neet to use equals() method as,
== tests for reference equality (whether they are the same object).
.equals() tests for value equality (whether they are logically "equal").
Hope that helps!! ;)

How to detect if a number is greater than Long.MAX value

My application will get number as string from end user. If the number is not numeric, i have to throw error message by saying that to provide number. This i can fix by using NumberFormatException. Another scenario is, user entered greater than Long.MAX value. How i can check this case and give error message to the user to enter smaller number than Long.MAX value? I should not use any third party or open source lib to fix this issue. Even if they are providing solution, How they are resolving it?
Use BigInteger to parse user input and compare the result with Long.MAX_VALUE
String userInput = ...;
BigInteger bigInt = new BigInteger(userInput);
if(bigInt.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
throw new Exception(userInput + ": value is too large");
}
If the entered number is greater than Long.MAX value, then what will you do next. It will cause an error as you don't know where to store it.
Better way is to check at the time of user input is in range or not. If it is greater than Long.MAX, store it in BigInteger
Use BigInteger and the longValueExact() method, and catch exceptions:
public static void main(String[] args) {
test("123");
test("9223372036854775807"); // Long.MAX_VALUE
test("-9223372036854775808"); // Long.MIN_VALUE
test("9223372036854775808"); // Long.MAX_VALUE + 1
test("-9223372036854775809"); // Long.MIN_VALUE - 1
test("abc");
}
private static void test(String input) {
long longVal;
try {
longVal = new BigInteger(input).longValueExact();
} catch (NumberFormatException e) {
System.out.println("Value is not a valid integer number: " + input);
return;
} catch (ArithmeticException e) {
System.out.println("Value exceeds range of long: " + input);
return;
}
System.out.println("Got valid long value: " + longVal);
}
OUTPUT
Got valid long value: 123
Got valid long value: 9223372036854775807
Got valid long value: -9223372036854775808
Value exceeds range of long: 9223372036854775808
Value exceeds range of long: -9223372036854775809
Value is not a valid integer number: abc
You can access the max value using Long.MAX_VALUE and check the user entered value in if condition.
Here is another solution without using an extra class other than Java core
public static void main(String[] args) {
System.out.println(isLargerThanLONGMAXVALUE("9223372036854775807")); // false
System.out.println(isLargerThanLONGMAXVALUE("9223372036854775806")); // false
System.out.println(isLargerThanLONGMAXVALUE("9223372036854775808")); // true
System.out.println(isLargerThanLONGMAXVALUE("645459223372036854775807")); // true
System.out.println(isLargerThanLONGMAXVALUE("922")); // false
}
public static boolean isLargerThanLONGMAXVALUE (String number) {
String longMax = String.valueOf(Long.MAX_VALUE);
if (number.length() > longMax.length()) return true;
if (number.length() < longMax.length()) return false;
long a, b = 0;
for (int i = 1 ; i < number.length() ; i++){
a = Long.parseLong(number.substring(0, i));
b = Long.parseLong(longMax.substring(0, i));
if (a > b) return true;
}
if (Integer.parseInt(number.substring(number.length()-1, number.length())) >
Integer.parseInt(longMax.substring(number.length()-1, number.length())))
return true;
return false;
}
Treating the string as a BigInteger and doing the comparison is the best way. But here's another just to show that there's usually more than one way to accomplish something:
public boolean isInRange(String number) {
String maxValue = Long.toString(Long.MAX_VALUE);
number = number.replaceFirst("^0+", ""); // remove leading zeroes
return number.length() < maxValue.length() ||
(number.length() == maxValue.length() &&
number.compareTo(maxValue) <= 0);
}
This assumes that number is composed entirely of digits (no negative sign).
try{
val n = input.toLong()
}catch(e: Exception){
// invalid Long
}

How to check number of digits from BigDecimal?

The requirement is to check if the number of digits is less than 7 digits in that case insert in DB else don't. I have tried the following solutions:
First solution:
public static void checkNoOfDigitVal(BigDecimal bigDecVal) {
BigInteger digits = bigDecVal.toBigInteger();
BigInteger ten = BigInteger.valueOf(10);
int count = 0;
do {
digits = digits.divide(ten);
count++;
} while (!digits.equals(BigInteger.ZERO));
System.out.println("Number of digits : " + count);
}
First solution works fine sometimes but sometimes the condition in while loop is not satisfied and it keeps on increasing the count number leading to endless count.
Second solution:
public static void checkNoOfDigitsVal(BigDecimal bigDecVal) {
String string = bigDecVal.toString();
String[] splitedString = string.split("\\.");
String[] newVal = splitedString[0].split("");
int a = newVal.length - 1;
if (a <= 6) {
System.out.println("correct size insert into DB: " + a);
} else {
System.out.println("Incorrect size insert cancel: " + a);
}
}
For example, if the value is 999999.9999, the second solution will return newVal.length = 6.
Please suggest a better solution to check the number of digits for big decimal where looping overhead can be minimized.
You can get it trivially using:
static int integerDigits(BigDecimal n) {
n = n.stripTrailingZeros();
return n.precision() - n.scale();
}
The precision is the total number of digits, and the scale is how many of those are to the right of the decimal point, so the difference is how many are to the left of the decimal point.
EDIT it's necessary to remove any trailing zeros to get a correct result for e.g. 0.000
EDIT 2 alternatively (and acks to #AdrianShum), since the problem with trailing zeroes only manifests itself with 0.00... you could use:
static int integerDigits(BigDecimal n) {
return n.signum() == 0 ? 1 : n.precision() - n.scale();
}
Live demo at http://ideone.com/uI6iMG
There's a much better solution in Alnitak's answer, posted just after mine (but which I've only seen now). I guess I'll leave this since a couple of people have found it useful, but if I needed to do this, I'd use their approach, not the approach below.
Your second solution is close, but it doesn't have to be quite that complicated:
public static void checkNoOfDigitsVal(BigDecimal bigDecVal) {
String str = bigDecVal.toString();
int wholeNumberLength = str.split("\\.")[0].length();
if (wholeNumberLength <= 6) {
System.out.println("correct size insert into DB: " + wholeNumberLength);
} else {
System.out.println("Incorrect size insert cancel: " + wholeNumberLength);
}
}
Live Example
I'm assuming that your 999999.999 example should result in wholeNumberLnegth of 6 and therefore be inserted in the DB (the question is unclear about that).
Because the current answers are not robust enough IMO, Here's my solution.
This method will scale a BigDecimal to the given length, but only scales the fractional part. It will throw an Exception if the integer part will be scaled. For my use case this is what I want. Tweak it to your liking.
public static BigDecimal scaleBigDecimalToLength(BigDecimal bigDecimal, int length) throws NumbersUtilException {
int digitCount = bigDecimal.toPlainString().replaceAll("[.,-]", "").length();
if (digitCount > length) {
int scale = bigDecimal.scale();
int newScale = length - (digitCount - scale);
if (scale > 0 && newScale >= 0) {
bigDecimal = bigDecimal
.setScale(length - (digitCount - scale), RoundingMode.HALF_UP);
} else {
throw new NumbersUtilException(
String.format("Cannot scale %s to a length of %s", bigDecimal, length));
}
}
return bigDecimal;
}
scaleBigDecimalToLength(BigDecimal.valueOf(0.0000012345600000), 8)
Output: 0.0000012
If you want to ignore the Dot (".") and count. then try this :
int count = 0;
BigDecimal bigDecimal = new BigDecimal("123.1000");
String[] split = bigDecimal.toString()
.split("\\.");
for (String element : split) {
count = count + element.length();
}
System.out.println("Total digits are " + count);

find decimal in a given number

How can i find decimal(dot) in a given number in java.
I am getting input from user, he may give integer or float value.
I need to find he entered integer or float, is it possible?
if yes could u tell me please.
--
Thanks
Assuming you got the digits of the number in a String, it would be
String number = ...;
if (number.indexOf('.') > -1)
...
you can try with yourNumberString.indexOf("."). If it returns a number greater than -1 there's a dot in the input.
Anticipating your need, I would suggest that you use java.util.Scanner for number parsing, and use its hasNextXXX methods instead of dealing with parseInt etc and deal with NumberFormatException.
import java.util.*;
String[] inputs = {
"1",
"100000000000000",
"123.45",
"blah",
" "
};
for (String input : inputs) {
Scanner sc = new Scanner(input);
if (sc.hasNextInt()) {
int i = sc.nextInt();
System.out.println("(int) " + i);
} else if (sc.hasNextLong()) {
long ll = sc.nextLong();
System.out.println("(long) " + ll);
} else if (sc.hasNextDouble()) {
double d = sc.nextDouble();
System.out.println("(double) " + d);
} else if (sc.hasNext()) {
System.out.println("(string) " + sc.next());
}
}
This prints:
(int) 1
(long) 100000000000000
(double) 123.45
(string) blah
You do not need to explicitly search for the location of the decimal point as some answers suggest. Simply parse the String into a double and then check whether that double represents an integer value. This has the advantage of coping with scientific notation for doubles; e.g. "1E-10", as well as failing to parse badly formatted input; e.g. "12.34.56" (whereas searching for a '.' character would not detect this).
String s = ...
Double d = new Double(s);
int i = d.intValue();
if (i != d) {
System.err.println("User entered a real number.");
} else {
System.err.println("User entered an integer.");
}
Some other ways to do this:
given
String input = ...
the following evaluates to true if it's a decimal number
input.split(".").length == 2
or
input.matches(".+\\..+")
or
!input.matches("\\d+")

Categories