my dog learned polymorphism
The moose likes Beginning Java and the fly likes help please! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help please!" Watch "help please!" New topic

help please!

christopher persinger

Joined: Aug 23, 2005
Posts: 20
hello all. im new to java and i was wondering if you could give me some pointers/direction for a program im trying to write. i need to write a program that will read a text file and then process it. the text file will have a bunch of lines containing different numbers of integers. im interested in trying to read and process the text file, but only catch the lines that have 3 integers and i want to display those 3 integers by using a println. ive got it to where i can read the text file and read the lines but i need some help from there. i know this is possible and im wondering which way to go with it. should i use stringtokenizer? or is there another way to do it? thanks in advance!

import java.util.*;

public class Assignment0Driver
public static void main(String[] args) throws IOException
String integer;

// Read from the file
Scanner fileScan = new Scanner(new File(""));

// Process the line with 3 integers
integer = fileScan.nextLine();
Thomas Paul Bigbee
Ranch Hand

Joined: Jun 28, 2005
Posts: 71
I wouldn't name a String integer, it's too confusing, this looks like a class assignment so I would be remiss, in giving the store away, however, It would seem like Scanner was choosen for a reason (A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods).

My question is this - did the instructor specify, delimiters, etc...

Hint a Scanner can be created from a line of text (String)

Hope this helps
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9058
Are the three digits consecutive? For example,
abc def123gh i jklmnop
or are they randomly scattered throughout the line like
abcd1 efg2hi3jk lm4nopqrs

You could always go char by char and ask each char if it is a digit. Or you could use regex. StringTokenizer processes the String in groups, so it might be somewhat useful if the numbers are in a group separated from the others by spaces or something like that.
abcd 123 efghij 555 klmnop

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
I agree. Here's the link:
subject: help please!
It's not a secret anymore!