Convert String to Long using BufferReader - long-integer

I'm trying to convert my input read Strings to long (Long). I've tried the valueof() and Long.Parslong(String s) but no luck. Im not sure what is going on. I can certainly print the String but cannot convert to Long ):
while ((line = br.readLine()) != null) {
String[] sheet = line.split(cvsSplitBy); //comma as separator
if(isNumeric(sheet[3])){ //parse into integer, only if first col is num
int n = Integer.parseInt(sheet[0]);
int pop = Integer.parseInt(sheet[3]);
System.out.println(sheet[4]);
System.out.println(sheet[4].getClass().getName());
Long lon = Long.valueOf(sheet[4]); //THIS IS WHERE THE ERROR OCCURS
......skip
The Error is the usual NumberFormatException
Exception in thread "main" java.lang.NumberFormatException: For input string: "34.95"
Any ideas? Thanks Aj

Focus on the description of the exception.
For input string: "34.95"
It has a Decimal value, which can't convert into a Long. You could either use Double.valueOf("34.95") or do a validation for non integer values.

Related

How to get the 2nd and 3rd character of a String as Integer - C89

So if I have a String
char string[4];
string = "A10";
How Can I get 10 as an Integer.
I tried getting 10 by itself using this but it didn't work.
char string[4];
char string2[2];
string = "A10";
string2[0] = string[1];
string2[1] = string[2];
I don't need to worry about the A, I know how to get that, I need to be able to get the 10 as an integer.
In java you can use the following code to convert string to integer,
int i=Integer.parseInt(s2);
for more information view this website,
https://www.javatpoint.com/java-string-to-int

Groovy: Converting a string to integer gives NumberFormatException

I am trying to get a date from this text:
{InstantSeconds=1581504140},ISO,Europe/Paris resolved to 2020-02-12T11:42:20
I tried doing
def text = "{InstantSeconds=1581504140},ISO,Europe/Paris resolved to 2020-02-12T11:42:20"
text = text.replaceAll("[^\\d.]", "")
text = text.substring(10)
println "${text}"
int result= Integer.parseInt("${text}");
println result
But I'm getting
java.lang.NumberFormatException: For input string: "20200212114220"
I'm using this (for practice) https://groovyconsole.appspot.com/
Does anyone know why that happens?
The value is too long for an integer. Use a Long datatype:
Long result = text.toLong()
assert result.class.name == 'java.lang.Long'
assert result == 20200212114220

Error in using indexOf not finding char in Arduino String

I have some code that I have no clue why it isn't working.
The code takes a serial input in the form of "xxx,yyy,zzz", where digits can range from 1 to 3 in each number. Because of an odd quirk in an app, it needs to be read as a char, then converted to a string to be handled. The intention is to split into 3 ints, red green and blue, from "RRR,GGG,BBB".
Now this works fine when I manually define String str (see commented code), but when I go and enter it from the serial console, it doesn't want to work. It seems to be coming from the indexOf(',') part, as while using Serial.print(c1);, I found that when I manually entered a string, it returned an index of the comma, but when I used the serial console, it returned -1 (not found).
And yes, the entered string into the console is in the correct format of "RRR,GGG,BBB", I've confirmed that by printing both phone and str independently.
while (Serial.available() > 0) {
char phone = Serial.read();
String str = String(phone);
//String str = "87,189,183";
int ln = str.length()-1;
int c1 = str.indexOf(','); //first place to cut string
int c2 = str.indexOf(',',c1+1); //second place
red = str.substring(0,c1).toInt();
green = str.substring(c1,c2).toInt();
blue = str.substring(c2,ln).toInt();
Serial.print(red);
Edit: With the Arduino String class, creating a string from a char is returning more than just one character, eleven in fact.
This:
char phone = Serial.read();
String str = String(phone);
will never create a string in str that has more than 1 character, since that's what you say you want.
This is the code for the Arduino's String(char) constructor:
String::String(char c)
{
init();
char buf[2];
buf[0] = c;
buf[1] = 0;
*this = buf;
}
So clearly your code will create a 1-character long string.
Also, beware of using indexes computed on the full string, on substrings later.
I'm try to guess that you are using these serial API http://playground.arduino.cc/Interfacing/CPPWindows.
while (Serial.available() > 0) {
char buf[12];
int len = Serial.ReadData(buf,11);
String str = String(buf);
//String str = "87,189,183";
int ln = str.length()-1;
int c1 = str.indexOf(','); //first place to cut string
int c2 = str.indexOf(',',c1+1); //second place
red = str.substring(0,c1).toInt();
green = str.substring(c1,c2).toInt();
blue = str.substring(c2,ln).toInt();
Serial.print(red);
If you are using other API like http://arduino.cc/en/Serial/Read you should follow these API where Serial is a Stream and read() return just the first available char.
Code was fixed by using a different function.
while (Serial.available() > 0) {
char phone = Serial.read();
str += phone;
//String str = "87,189,183";
int ln = str.length()-1;
int c1 = str.indexOf(','); //first place to cut string
int c2 = str.indexOf(',',c1+1); //second place
red = str.substring(0,c1).toInt();
green = str.substring(c1,c2).toInt();
blue = str.substring(c2,ln).toInt();
Serial.print(red);
I'm not sure why this works, and why before I was getting a string with more than one character. But it works!

Convert System::String^ to int32 in Visual C++

I have dataGridView and I need to check dataGridView->Rows[j]->Cells[i]->Value. If its >0 make some work, else - do nothing. I tried
int x = Int32::Parse(dataGridView1->Rows[j]->Cells[i]->Value->ToString());
if(x > 0 )count++;
but have Format exception, Input string was not in a correct format.
int x = Convert::ToInt32(dataGridView1->Rows[j]->Cells[i]->Value->ToString());
also Format exception, Input string was not in a correct format.
int x = safe_cast<int>(dataGridView1->Rows[j]->Cells[i]->Value);
InvalidCast exception, specified cast is not valid.
What I'm doing wrong?
Update: The right way is
double x = Convert::ToDouble(dataGridView1->Rows[j]->Cells[i]->Value);

Java-Servlets: String null or "null"

String says its not null but then later throw NullPointerException
I had this problem(see the link), where I was sure that a String is null, but in fact the String was "null"
jcasso told me:
Since you get the string from a
servlet i can say that this is normal.
Java converts a null string to a
"null" string on some conditions.
When this situations appear?
Mostly when you use string concatenation or String.valueOf():
String x = null;
String y = x + ""; // y = "null"
String z = String.valueOf(x); // z = "null"
(There are similar variants, such as using StringBuilder.append((String) null).)

Resources