-3

StackOverflow I am a CS student who is having trouble on an assignment who needs someone to assist me with this assignment. The assignment has me take a input file which has data that looks like this:

FirstNameFXO|LastFXO|2510 Main Street|Suite 101D|City100|GA|72249|$280.80
FirstNamePNR|LastPNR|396 Main Street|Suite 100A|City102|GA|24501|$346.01
FirstNameXZU|LastXZU|2585 Main Street|Suite 107C|City101|GA|21285|$859.40
FirstNameHWD|LastHWD|1019 Main Street|Suite 102D|City105|GA|28273|$317.12
FirstNameGHP|LastGHP|2097 Main Street|Suite 109B|City106|GA|72621|$279.28
FirstNameUKM|LastUKM|1087 Main Street|Suite 109C|City106|GA|81463|$711.91
FirstNameKUV|LastKUV|2685 Main Street|Suite 106B|City102|GA|37141|$951.47
FirstNameTBO|LastTBO|1971 Main Street|Suite 103C|City101|GA|63018|$851.35  

There are 1000 lines, I am trying to take this data and create an output file that would be laid out like a letter but the problem I am having is I do not know how to take each individual part of the input so I could have a value that I could use like this:

output.write("Dear" + firstName + lastName, but I do not know how to make them variables. I have created an array that takes each individual line but I am stuck here. Any assistance would be amazing. I am very new to File I/O in general so this is very challenging for me.

import java.io.*;
    import java.util.Scanner;
    public class CollectionLetter {
        public static void main(String[] args) {
            File file = new File("dataCollection.txt");
            String data[] = new String[1000];
            try {
                Scanner s = new Scanner(file);
                while (s.hasNextLine()) {
                    for (int i = 0; i < data.length;i++){
                        data[i] = s.nextLine();
                    }
                    System.out.println(data[1]);
                }s.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
  • Welcome to Stack Overflow! It looks like you may be asking for homework help. While we have no issues with that per se, please observe these [dos and don'ts](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions/338845#338845), and edit your question accordingly. (Even if this isn't homework, please consider the advice anyway.) – Joe C Mar 24 '17 at 22:15
  • Possible duplicate of [Trying to store values from a a text file but it outputting incorrectly](http://stackoverflow.com/questions/42982158/trying-to-store-values-from-a-a-text-file-but-it-outputting-incorrectly) – Oghli Mar 25 '17 at 00:40

1 Answers1

1

String has a method split(String) which, well, splits a string and returns its parts as an array. For every line you could do:

final String[] parts = line.split("|");

Which gives you an array of length 8 (given that every line is structured the same way). Now depending on how you want the individual parts to look like you may want to use methods like substring(int) or substring(int, int) which return only a part of the string (e.g. to cut out "FirstName").

Sogomn
  • 304
  • 3
  • 16