0

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(" ");          
        }
             
    }
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by imclumsy
0

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

0

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.