Results 1 to 2 of 2
  1. #1
    Lennon-Guru is offline Member
    Join Date
    Dec 2007
    Rep Power

    Smile Reading text file


    I have to read a text file, which the first line contains 2 numbers (integers) and then assign these to variables.

    Here's the first few lines of the text file:

    25 100
    SEND 2228161 Hello Pete
    RECEIVE 2228161 Hello Chris, how are you?
    SEND 2227765 I hate text speak
    RECEIVE 2228876 wot u on bout
    SEND 2227975 The quick brown fox jumped over the lazy dog

    Would I use the String.parseInt(value1) like this

    sizeOfMessage = value1;

    and String.parseInt(value2)


    I then have to go on and read different String lines like SEND and RECEIVE
    where I have used the if (line.startsWith("SEND")

    Thanks for your help

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    You could try something like this (in pseudo code):
    Java Code:
    Scanner scanner = new Scanner(yourFile);
    String line = scanner.nextLine();
    String[] data = line.split("\\s");
    // Check what we have here:
    int sizeOfMessage = Integer.parseInt(data[0]);
    int sizeOfBox = Integer.parseInt(data[1]);
    // carry on reading...

Similar Threads

  1. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 06:45 PM
  2. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 01:51 AM
  3. Applet - reading text files packed into JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:15 AM
  4. Reading text from a URL using BufferedReader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts