0

so i am reading in a file thats in csv format and then formatting it and outputting it to the console my only problem is it works fine till i hit a string that has spaces in it so i was wondering if there was a way to read the data into a list and have the elements increment if there is a comma so if i have this text file im reading in

000001, Pineweed, Long Bottom Leaf, 600.0 000002, Lembas, Elven Wayfare Bread, 200.0 000003, Wine, Woodland Elf Wine, 400.0 000004, Mushrooms, Farmer Took's Finest, 125.0 000005, Mithril, Enchanted Dwarven Armor, 3000.0

how would i take Long Bottom Leaf as one string if that makes since this is what ive tried

import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import java.util.ArrayList;
import java.util.Scanner;

public class ProductReader {
    public static void main (String[] args) {
        ArrayList<String> reader = new ArrayList<String>();
        JFileChooser file = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
        int returnValue = file.showOpenDialog(null);
        int line = 0;
        int end = 0;
        int counterID = 0;
        int counterProduct = 1;
        int counterDescription = 2;
        int counterCost = 3;

        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = file.getSelectedFile();
            String fileName = selectedFile.getName();

            try {
                Scanner fin = new Scanner (selectedFile);

                while (fin.hasNext()) {
                        String data = fin.next();
                        reader.add (data.replace(",", ""));
                }
                fin.close();
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        System.out.printf ("%s", "ID#");
        System.out.printf ("%15s", "Product");
        System.out.printf ("%15s", "Description");
        System.out.printf ("%15s", "Cost\n");
        System.out.print ("==============================================================\n");

        for (int x = 0; x < reader.size(); x++) { 
            if (line == 0) {
                System.out.printf ("%-9s", reader.get(counterID));
                counterID += 4;
            }

            if (line == 1) {
                System.out.printf ("%-16s", reader.get(counterProduct));
                counterProduct += 4;
            }

            if (line == 2) {
                System.out.printf ("%-18s", reader.get(counterDescription));
                counterDescription += 4;
            }

            if (line == 3) {
                System.out.print (reader.get(counterCost));
                counterCost += 4;
            }

            end++;

            if (reader.size() > end) {
                line++;
            }

            if (line == 4) {
                System.out.println();
                line = 0;
            }
        }
    }
}
mikeal200
  • 1
  • 1
  • What do you get for `String data = fin.next();`? It may be the case that the default token for `Scanner` is `" "`, you could pass in `","` as a specified pattern like so `fin.next(",")` – DarceVader Jan 30 '19 at 20:03
  • @DarceVader see thats what i thought man wouldnt that be easy if you could do that i get this error when trying that Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.next(Scanner.java:1418) at ProductReader.main(ProductReader.java:27) – mikeal200 Jan 30 '19 at 20:07
  • That Error might be the end of your file, try `fin.hasNext(",");`, if that misses the last value, do one last `fin.next();` – DarceVader Jan 30 '19 at 20:10
  • i know i could read the file into an string array and use the split function and then read the array into a list but im not sure how to read a file into a string array – mikeal200 Jan 30 '19 at 20:11
  • @DarceVader like this while (fin.hasNext()) { String data = fin.next(","); reader.add (data.replace(",", "")); data = fin.next(); } or do you mean add it before the fin.close() statement – mikeal200 Jan 30 '19 at 20:12
  • [how do i create a java string from the contents of a file](https://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) and [reading from a text file and storing in a string](https://stackoverflow.com/questions/16027229/reading-from-a-text-file-and-storing-in-a-string) should get you a string per line of a `File` – DarceVader Jan 30 '19 at 20:15
  • Almost, this is what I had in mind `while (fin.hasNext(",")) { reader.add (fin.next(",").replace(",", "")); } reader.add (fin.next(""));` – DarceVader Jan 30 '19 at 20:21
  • 1
    @DarceVader i tired this and get a inputmismatch ill look at the links you sent and thanks for the help man this is what i love about coding we are all learning together no one knows everything – mikeal200 Jan 30 '19 at 20:33

0 Answers0