• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help please!

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.io.*;
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("assign0.in"));

// Process the line with 3 integers
integer = fileScan.nextLine();
}
}
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Tom
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic