search the integer

Hi guys can anybody help me with this one?

I have a file with one string or integer in each line. Now i have to search the highest and lowest number from the file and print it to the console. I know i have to assume each string as an integer, but i don't how to code them.

my file (findInt.dat) is like this,

1

one

hello

6

three

java

99

forty

zero

132

end

thanx

322

etc

now i've to print in the console is,

Lowest number is...1

Highest number is.....322

Thanx in advance

[600 byte] By [anantaaustraliaa] at [2007-11-27 2:55:39]
# 1
Use Integer.parseInt() with a try-catch-construction around it (google for the exact syntax of "Integer.parseInt").
EvilBroa at 2007-7-12 3:32:35 > top of Java-index,Java Essentials,Java Programming...
# 2

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileReader;

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

try {

BufferedReader in = new BufferedReader(new FileReader("findInt.dat"));

String str;

int max = Integer.MIN_VALUE;

int min = Integer.MAX_VALUE;

int current = 0;

while ((str = in.readLine()) != null) {

try {

current = Integer.parseInt(str.trim());

if(current>=max){

max = current;

}

if(current<=min){

min = current;

}

} catch (Exception e) {

}

}

in.close();

System.out.println(min);

System.out.println(max);

} catch (Exception e) {

e.printStackTrace();

}

}

}

Hope That Helps

java_2006a at 2007-7-12 3:32:35 > top of Java-index,Java Essentials,Java Programming...
# 3

import java.util.List;

import java.util.ArrayList;

import java.util.Collections;

public class Test{

public boolean numCheck(String str){

boolean flag = false;

try{

Integer.parseInt(str);

flag = true;

} catch(NumberFormatException nexp){

}

return flag;

}

public List getNumbers(String filename){

List alist = new ArrayList();

BufferedReader input = null;

try {

input = new BufferedReader(filename);

String line = null;

while (( line = input.readLine()) != null){

if(numCheck(line))

alist.add(line);

}

}catch (FileNotFoundException ex) {

ex.printStackTrace();

}

catch (IOException ex){

ex.printStackTrace();

}

finally {

try {

if (input!= null) {

input.close();

}

}

catch (IOException ex) {

ex.printStackTrace();

}

}

return alist;

}

public static void main(String sr[]){

Test t = new Test();

List list = t.getNumbers("filename");

String max = (String)Collections.max(list);

String min = (String)Collections.min(list);

System.out.println("MAX: "+max+",MIN: "+min);

}

}

RahulSharnaa at 2007-7-12 3:32:35 > top of Java-index,Java Essentials,Java Programming...
# 4

Personally I would iterate through the string using Character.isDigit (remembering to test for - on the first char if negatives are allowed. If all characters pass then it's an integer.

If your input file were huge then throwing all those exceptions would come at an greater cost in performance.

ChristopherAngela at 2007-7-12 3:32:35 > top of Java-index,Java Essentials,Java Programming...
# 5

> Personally I would iterate through the string using Character.isDigit

007 would have to be admitted, more because your rules allowed it than because it's a sensible number. And -1 would be outlawed.*

> If your input file were huge then throwing all those exceptions would come at an

> greater cost in performance.

Using exceptions to control ordinary program logic is rightly considered to be a Bad Thing. But there's no Integer method to check a String so, unless you're a real fundementalist, the occasional NumberFormatException might be overlooked...

[Edit] * Then again I could actually read your post... Sorry.

pbrockway2a at 2007-7-12 3:32:35 > top of Java-index,Java Essentials,Java Programming...
# 6
007 is perfectly acceptable and would be parsed as 7 by ParseInt.I agree that using exceptions is a perfectly acceptable approach but looking at the OP's example input Strings are far from the exception.
ChristopherAngela at 2007-7-12 3:32:35 > top of Java-index,Java Essentials,Java Programming...
# 7

> I agree that using exceptions is a perfectly acceptable approach but looking at the

> OP's example input Strings are far from the exception.

Just out of interest I made a long string that looked like "foo\n"+6 digit signed int +"\nbar\n" repeated hundreds of thousands of times. With a buffered reader wrapping a StringReader (ie no disk i/o) ChristopherAngel's suggestion of explicitly checking the String is about 10x as fast as catching and ignoring an NFE.

pbrockway2a at 2007-7-12 3:32:35 > top of Java-index,Java Essentials,Java Programming...