So the program I have is supposed to read from a file take that data (which is split into three parts divided by a space). These Strings need to be read and stored in an appropriate field. Now i dont know how to do this, I've tried with an array as you can see which i think is right but it only stores a single line. Someone told me that i should do that and then have spearate array objects or something? but i dont really know how. Help would be massivley appreciated.

import java.util.Scanner;
import java.io.*;

public class DataRead
{  
    
        private Scanner scan; 
        private String[]arraySplit = null;
    
    public DataRead()     
    {  
     
        arraySplit = new String[100];
        
        try
        {  
            FileReader inputFile = new FileReader(//read file);
            scan = new Scanner(inputFile);    
        }
        catch (FileNotFoundException e)
        {
            System.out.println("FileNotFoundException - Could not find file");
        }
        catch (Exception e)
        {
            System.out.println("Unknown error");
        }   
        
         while(scan.hasNext())  //reads through file while there is more to file
        {
                String line = scan.nextLine();      
                arraySplit = line.split(" ");          
        }
             
    }

Recommended Answers

All 2 Replies

i think you can use a StringTokenizer and use the delimiter of " " to divide the string into three parts and put it in three different fields.
The psedocode would be:
initialize a string tokenizer ---> StringTokenizer scT = new StringTokenizer(name of file being read. delimeter);
while (scT.hasmoreTokens())
field one = scT.next
field two = scT.next
filed three = scT.next

i hope that is something along the lines of what you're looking for...i don't see your whole code so i cannot test it

sorry its
StringTokenizer scT = new StringTokenizer(name of file being read, delimeter);

you should also probably look at the java api of the StringTokenizer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.