Reputation: 115
I have a text file that contains the following:
Hello
1
2
3
4
5.6
LOL
23.5
34.6
23
456
Rofl.
I wrote down the code in java that would read the contents of this text file and distinguish between the 3 data types.I used try catch statements and my code works(kinda). The only problem is that it converts any whole numbers to doubles as well. For example the following is what my code is outputting:
List of integers in the textfile: [1, 2, 3, 4, 23, 456]
List of doubles in the textfile: [1.0, 2.0, 3.0, 4.0, 5.6, 23.5, 34.6, 23.0, 456.0]
List of Strings in the textfile: [Hello, 5.6, LOL, 23.5, 34.6, Rofl]
I want to prevent that from happening. Any suggestions would be appreciated.
ArrayList<Integer> data_int=new ArrayList<Integer>();
ArrayList<String> data_String=new ArrayList<String>();
ArrayList<Double> data_double=new ArrayList<Double>();
while(file.hasNext())
{
String s=file.next();
System.out.println(s);
try
{
Integer.parseInt(s);
data_int.add(Integer.parseInt(s));
}
catch(NumberFormatException e)
{
data_String.add(s);
}
try
{
Double.parseDouble(s);
data_double.add(Double.parseDouble(s));
}
catch(NumberFormatException e)
{
}
}
System.out.println("List of integers in the textfile: "+data_int);
System.out.println("List of doubles in the textfile: "+data_double);
System.out.println("List of Strings in the textfile: "+data_String);
Upvotes: 2
Views: 123
Reputation: 95578
Instead of calling parseInt
or parseDouble
and then dealing with the exception, perhaps it would be better to use a regex with String#matches
to see if the value looks like an integer or a double. Exceptions are for exceptional circumstances that happen outside normal flow; you don't want to use them to control the regular flow of your program. Since you can expect there to be non-numerical values, it is better to check the value and see what it "looks like".
To see if it is an integer, you can just use -?\\d+
.
For doubles, use the regex -?\\d*\\.\\d+
. This will even match strings of the form .5
.
If none of these match, it is probably a string.
Upvotes: 0
Reputation: 2155
Use nested Try Catch Block:
while (file.hasNext()) {
String s = file.next();
System.out.println(s);
try {
Integer.parseInt(s);
data_int.add(Integer.parseInt(s));
} catch (Exception e) {
try {
Double.parseDouble(s);
data_double.add(Double.parseDouble(s));
} catch (Exception e1) {
// TODO Auto-generated catch block
data_String.add(s);
}
}
}
Upvotes: 0
Reputation: 35096
Put your double check in the catch block for the integer check
try {
Integer.parseInt(s);
data_int.add(Integer.parseInt(s));
} catch(NumberFormatException e) {
try {
Double.parseDouble(s);
data_double.add(Double.parseDouble(s));
} catch(NumberFormatException e) {
data_String.add(s);
}
}
Upvotes: 1
Reputation: 38541
Try the following with each token received:
int
- if that passes do not attempt to parse it as an int
or String
.double
. If that passes do not attempt to parse it as a String
.String
.Remove the code where you add the data to the String datset when each prior parse attempt fails.
Upvotes: 1
Reputation: 7196
I think below should work for you.
while(file.hasNext()){
String s=file.next();
System.out.println(s);
try
{
try
{
Integer.parseInt(s);
data_int.add(Integer.parseInt(s));
}
catch(NumberFormatException e){
//do nothing
}
Double.parseDouble(s);
data_double.add(Double.parseDouble(s));
}
catch(NumberFormatException e){
data_String.add(s);
}
}
Upvotes: 0