scanning integers from a file

Does java have the capablility to scan a line from a document and only print it if it is an integer? All the methods that I am looking at have you declare the variable that you are scanning.

[197 byte] By [pberardi1a] at [2007-11-27 10:53:09]
# 1

while(scanner.hasNextInt()) {

//do something

}

schumachera at 2007-7-29 11:42:18 > top of Java-index,Java Essentials,New To Java...
# 2

easy enough to implement

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Fubar

{

private Scanner sc = null;

private File fooFile = new File(".\\petes\\brief3\\fooFile.txt");

public Fubar()

{

try

{

sc = new Scanner(fooFile);

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

}

public void filterText()

{

if (sc != null)

{

while (sc.hasNextLine())

{

if (sc.hasNextInt())

{

System.out.println(sc.nextInt());

sc.nextLine();

}

else

{

sc.nextLine();

}

}

sc.close();

}

}

public static void main(String[] args)

{

Fubar f = new Fubar();

f.filterText();

}

}

input file, fooFile.txt:

text 1

text 2

text 3

29038

text 4

9232

text 5

-300

0

text 6

43

1021

443

last line

output:

29038

9232

-300

0

43

1021

443

petes1234a at 2007-7-29 11:42:18 > top of Java-index,Java Essentials,New To Java...
# 3

THANKS PETE! That works great. Perhaps you can tell me why it is not reading every integer?

/*

* IntegerInputFile.java

*

* Created on July 17, 2007, 8:50 PM

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

/**

*

* @author pberardi

*/

import java.util.Scanner;

import java.io.*;

public class IntegerInputFile {

public static void main(String []args)throws Exception {

int url;

Scanner fileScan, urlScan;

int count=0;

fileScan = new Scanner(new File("IntegerInputFiledoc.txt"));

while (fileScan.hasNextLine()) {

if (fileScan.hasNextInt()) {

System.out.println(fileScan.nextInt());

count = count +1;

fileScan.nextLine();

} else {

fileScan.nextLine();

}

}

System.out.println("There are a total of "+count+" integers in IntegerInputFiledoc");

}

}

input file:

0 x 0 equals 0

1 x 1 equals 1

2 x 2 equals 4

3 x 3 equals 9

4 x 4 equals 16

5 x 5 equals 25

6 x 6 equals 36

7 x 7 equals 49

8 x 8 equals 64

9 x 9 equals 81

10 x 10 equals 100

11 x 11 equals 121

12 x 12 equals 144

OUTPUT:

init:

deps-jar:

Compiling 1 source file to C:\Practice Java\IntegerIntputFile\build\classes

compile:

run:

0

1

2

3

4

5

6

7

8

9

10

11

12

There are a total of 13 integers in IntegerInputFiledoc

BUILD SUCCESSFUL (total time: 0 seconds)

pberardi1a at 2007-7-29 11:42:18 > top of Java-index,Java Essentials,New To Java...
# 4

I messed up. this may be better, tho my regex is rusty:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class Fubar

{

private Scanner sc = null;

private File fooFile = new File(".\\petes\\brief3\\fooFile.txt");

public Fubar() {

try

{

sc = new Scanner(fooFile);

}

catch (FileNotFoundException e)

{

e.printStackTrace();

}

}

public void filterText()

{

if (sc != null)

{

while (sc.hasNextLine())

{

String line = sc.nextLine();

if (line.matches("(-|)\\d*"))

{

System.out.println(line);

}

}

sc.close();

}

}

public static void main(String[] args)

{

Fubar f = new Fubar();

f.filterText();

}

}

Addendum:

or this may be better:

if (line.matches("(-?)\\d*"))

Message was edited by:

petes1234

petes1234a at 2007-7-29 11:42:18 > top of Java-index,Java Essentials,New To Java...
# 5

Can someone please tell me why I am get a inputmismatch exception?

You can find the file on my previous response.

import java.util.Scanner;

import java.io.*;

public class IntegerInputFile {

public static void main(String []args)throws Exception {

int count=0;

Scanner reader = new Scanner(new File("IntegerInputFiledoc.txt"));

while (reader.hasNextLine())

{

count = count +1;

int i = reader.nextInt();

System.out.println(i);

}

System.out.println("There are a total of "+count+" integers in IntegerInputFiledoc");

}

}

pberardi1a at 2007-7-29 11:42:18 > top of Java-index,Java Essentials,New To Java...
# 6

you need an if/else statement in your while loop. if it is an integer then do your stuff if not then scan next.

import java.util.Scanner;

import java.io.*;

class ManySquaresSkipWords

{

public static void main (String[] args) throws IOException

{

Filefile = new File("myData.txt");// create a File object

Scanner scan = new Scanner( file );// connect a Scanner to the file

int num, square;

while ( scan.hasNext() )// is there more data to process?

{

if ( scan.hasNextInt() ) // if it is an integer, get it

{

num = scan.nextInt();

square = num * num ;

System.out.println("The square of " + num + " is " + square);

}

else

scan.next();// read and discard non-integers

}

}

}

And go to this url on chapter 23

http://chortle.ccsu.edu/java5/cs151java.html

schumachera at 2007-7-29 11:42:18 > top of Java-index,Java Essentials,New To Java...
# 7

> you need an if/else statement in your while loop. if

> it is an integer then do your stuff if not then scan

> next.

>

> class ManySquaresSkipWords

> .......................

> while ( scan.hasNext() )// is there more data to

> process?

>{

> if ( scan.hasNextInt() ) // if it is an integer,

> get it

>{

> ......................

>

> And go to this url on chapter 23

> http://chortle.ccsu.edu/java5/cs151java.html

From what I understand, the OP wants to print a line only if it contains nothing but an integer (I could be wrong?). If this is so, the problem with the code above is that it will print integers if they are the first token on a line, even if non-integer tokens follow. That was my rational for using regex and String.match(regex) trying to see if the entire line matches the pattern for a generic integer.

petes1234a at 2007-7-29 11:42:18 > top of Java-index,Java Essentials,New To Java...