I have made a JFormattedTextField that is formatted to only take numbers:
Number num;
JFormattedTextField Length = new JFormattedTextField(num);
Now I want to get the number out of that and multiply it by 23. I know you can do:
String LengthStr = Length.getText();
int LengthInt = Integer.parseInt(LengthStr);
int Total = LengthInt*23;
But Isn't there an easier way? I've tried:
int LengthInt = Length.getText();
int Total = LengthInt * 23;
because it should be a number already.
The above didn't work because it says
Required: int Found: String
What I 'm asking for is that if the JFormattedTextField is formatted to be an int, then surely I can do something like
int LengthInt = Length.getInt();
int Total = LengthInt *23;
No, you can't. The line: int LengthInt = Length.getText(); tries to put a String Object (Length.getText()) into an primitive tipe (int).
There is no implicit conversion from String to integer in Java, because a String can contain anything (like "abc", for example) that is not directly convertible to a number.
Also, the first two lines should be changed to this: JFormattedTextField Length = new JFormattedTextField(NumberFormat.getIntegerInstance()); Instantiate the JFormattedTextField with the NumberFormat instead of Number.
Related
When I'm trying to parse the input1 string to an integer value it's rising a NumberFormatException.I've tried replacing the spaces if any in the string but it didn't work for me.
int number = 0;
String input1 = "12345354987";
try{
ip = Integer.parseInt(input1);
}catch(NumberFormatException e){
System.out.println("not a number");
}
Its because the Number in String is out of Integer range, If you really need this than use BigInteger for large values like this :
String input1 = "12345354";
BigInteger ib =new BigInteger(input1);
System.out.println(ib);
or
For small values that can fit in Long range you can use :
String input1 = "1234535455";
Long ip = Long.parseLong(input1);
System.out.println(ip);
12345354987 > 2,147,483,64
Integer max range is 2,147,483,647 and you are crossing it. You may use Long or BigDecimal for big numbers.
Your number is greater then integer Max value please use long then try parsing.
I know there is quite all of posts for converting characters to integers, strings to BigInteger-s, int-s to BigInteger-s, ... but I just can't figure out why this does not work.
Scanner sc = new Scanner(System.in);
BigInteger sum = 0;
String line = "";
while (sc.hasNext()) {
line = sc.next();
for (char character: vrstica.toCharArray()) {
sum = sum.add(BigInteger.valueOf(Character.getNumericValue(character)));
}
}
I do have Scanner and BigInteger imported. The input data is constructed of lines with numbers, like this: 7218904932283439201\n7218904932283439201 ...
If I understand correctly, addition for BigInteger-s should be written like this: bigInteger1.add(bigInteger2), where both numbers are of type BigInteger. So I should convert that character of type char to type int and then convert that int value to BigInteger with method BigInteger.valueOf(), which takes an int argument.
The error I am getting is the following: incompatible types: int cannot be converted to BigInteger
I am not seeing where I could be wrong so I would appreciate if anyone could point out my mistake.
You're trying to assign an int literal to a BigInteger object.
BigInteger sum = 0;
Use instead the following
BigInteger sum = BigInteger.ZERO;
I was wondering how can I take some numbers in a string and convert them to an integer type? for example if a user entered 12:15pm how can I get 1 and 2 and make an int with value 12?
Given the example above, you could try something like this:
final int value = Integer.parseInt(input.substring(0, input.indexOf(':'))); //value = 12
Where input = 12:15pm in this case.
Generally speaking, just use a combination of String#indexOf(String), String#substring(int, int) and Integer.parseInt(String).
Read the String and Integer API's
You can use the String.split() to get the two numeric strings
You can use Integer.parseInt(...) to convert the String to an int.
Edit: Using the split() you can do something like:
String time = "12:34pm";
int hour = Integer.parseInt( time.split(":")[0] );
I have 4 strings:
str1 = 10110011;(length of all string is:32)
str2 = 00110000;
str3 = 01011000;
str4 = 11110000;
In my project I have to add these string and the result should be:
result[1] = str1[1]+str2[1]+str3[1]+str4[1];
result should be obtained as addition of integer numbers.
For the example above, result = 22341011
I know integer to string conversion in Java is very easy but I found string to integer conversion a little harder.
To parse Integers -2^31 < n < 2^31-1 use:
Integer value = Integer.valueOf("10110011");
For numbers that are larger, use the BigInteger class:
BigInteger value1 = new BigInteger("101100111011001110110011101100111011001110110011");
BigInteger value2 = // etc
BigInteger result = value1.add(value2).add(value3); //etc.
The simplest way to do this is with Integer.parseInt(str1). Returns an int containing the value represented by the string.
valueOf() returns an Integer object, rather than an int primitive.
Because your numbers are so big they will not fit in an int. Use the BigInteger class.
I am not known about your project and what actually your problem is. But I came to guess from your partial information that, you have multiple set of strings in bit representation as you explained.
str1 = "1000110.....11";
str1 = "1110110.....01"; etc
adding those decimal values,gives an ambiguous result as an integer can be the sum of multiple integer values. Just see an example below where there are total 5 possibilities[with positive decimal values] to yield 6.
1+5 = 6;
2+4 = 6;
3+3 = 6;
4+2 = 6;
5+1 = 6;
If you proceed in that way you just do an error,nothing else in your case.
One better solution can be,
compute the decimal values of individual strings. Instead of adding(+) them, just concat(join) them to form a single string.
I am suggesting this approach because, This gives always a unique value and later you may need to know individual strings decimal values.
String strVal1 = String.format(computeDecimal(str1));
String strVal2 = String.format(computeDecimal(str2));
String strVal3 = String.format(computeDecimal(str3));
.
.
.
String strValn = String.format(computeDecimal(strn));
String myVal = String.concate(strVal1,strVal1,strVal1,....strValn);
Now you can treat your string as your wish.
//This will give you a non conflicting result.
Better to implement above approach than BigIntegers.
Hope this helps you greatly.
Which Java data type would be able to store a big numerical value, like 9999999999?
Your concrete example could be stored in long (or java.lang.Long if this is necessary).
If at any point you need bigger numbers, you can try
java.math.BigInteger (if integer), or java.math.BigDecimal (if decimal)
In addition to all the other answers I'd like to note that if you want to write that number as a literal in your Java code, you'll need to append a L or l to tell the compiler that it's a long constant:
long l1 = 9999999999; // this won't compile
long l2 = 9999999999L; // this will work
You can store this in a long. A long can store a value from -9223372036854775808 to 9223372036854775807.
A primitive long or its java.lang.Long wrapper can also store ten digits.
Use BigInt datatype with its implicit operations. The plus point for it is it will not give answers in exponential representation. It will give full length result
Here is an example of addition
BigInteger big1 = new BigInteger("1234567856656567242177779");
BigInteger big2 = new BigInteger("12345565678566567131275737372777569");
BigInteger bigSum = big1.add(big2);
System.out.println(bigSum );
you can use long or double.
You could store by creating an object that hold a string value number to store in an array list.
by example: BigInt objt = new BigInt("999999999999999999999999999999999999999999999999999");
objt is created by the constructor of BigInt class. Inside the class look like.
BigInt{
ArrayList<Integer> myNumber = new ArrayList <Integer>();
public BigInt(){}
public BigInt(String number){ for(int i; i<number.length; i++){ myNumber.add(number.indexOf(i)); } }
}
A wrapper class java.lang.Long can store 10 digit easily.
Long phoneNumber = 1234567890;
It can store more than that also.
Documentation:
public final class Long extends Number implements Comparable<Long> {
/**
* A constant holding the minimum value a {#code long} can
* have, -2<sup>63</sup>.
*/
#Native public static final long MIN_VALUE = 0x8000000000000000L;
/**
* A constant holding the maximum value a {#code long} can
* have, 2<sup>63</sup>-1.
*/
#Native public static final long MAX_VALUE = 0x7fffffffffffffffL;
}
This means it can store values of range 9,223,372,036,854,775,807 to -9,223,372,036,854,775,808.
If you want to take user input then you should take that as string, then convert that into long.
//Take user input using Scanner class
Scanner sc = new Scanner(System.in);
String str = sc.next();
//Convert that to long
long num = Long.parseLong(str);
System.out.println(num); // This is not a string anymore