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(" ");          
        }
             
    }

Dani AI

Generated

Your code is overwriting the same array on every loop, so only the last line survives. Instead of assigning one array per iteration, accumulate each record into a collection (or store columns into separate collections). For three fixed fields per line, three common approaches are: a List<String[]> (each element is a 3-element array), three parallel lists (one per column), or a small POJO (preferred for clarity) and a List<POJO>.

As pointed out, tokenizing is the right idea. Prefer Scanner.next() or reading lines and parsing—StringTokenizer is legacy. If the file really is "three space-separated tokens per record," the simplest robust pattern is to read tokens in groups of three and add a new record for each group.

Example using a tiny Record class and Scanner.next():

static class Record {
    String f1, f2, f3;
    Record(String f1, String f2, String f3) { this.f1 = f1; this.f2 = f2; this.f3 = f3; }
    public String toString() { return f1 + " " + f2 + " " + f3; }
}

List<Record> records = new ArrayList<>();
try (Scanner sc = new Scanner(new File("data.txt"))) {
    while (sc.hasNext()) {
        String a = sc.next();
        if (!sc.hasNext()) break;
        String b = sc.next();
        if (!sc.hasNext()) break;
        String c = sc.next();
        records.add(new Record(a, b, c));
    }
}

Troubleshooting notes:

  • Validate token counts; guard against truncated lines.
  • If fields can contain spaces, use quoted CSV or another delimiter and parse lines accordingly.
  • For very large files, process each record as you read it instead of storing everything in memory.
  • Use try-with-resources (above) so the file is closed even on exceptions.

For : change from reassigning one array each loop to adding a new element to a collection (as shown). That will keep every line’s data instead of just the last.

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.