-1

Here is my code:

I want to create a csv file by using scanner.

The input be like:

Milk

30

and I hope the output will be like: enter image description here

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class AddItem {

  private static String list;
  private static String val;

public static void main(String[] args) throws Exception {
    Map<String, List<String>> map = new TreeMap<String, List<String>>();
    
    
    FileWriter add = new FileWriter("test.csv");
    System.out.println("please input number: ");
    Scanner in = new Scanner(System.in);
    ArrayList<String> input = new ArrayList<String>();
    input.add(in.nextLine());
    input.add(in.nextLine());

    for (String val : input) {
         add.write(val);
         add.write("\n");
    }
  }
}
OWO
  • 17
  • 3
  • possible duplicate of https://stackoverflow.com/questions/30073980/java-writing-strings-to-a-csv-file – Nitin Singhal Jun 25 '20 at 14:26
  • 1
    "Java using Scanner to write CSV file" ```class Scanner``` is for taking inputs ***not*** for writing into files... – Kitswas Jun 26 '20 at 07:26

2 Answers2

0

There are so many problems in your code, that I don't really know where to start. Please, find out some good book about Java to get basics.

Here is code example which do something like you want, and demonstrate some important things: Closeable Interface, input/output Streams, file encoding.

import java.io.BufferedWriter;
import java.io.FileOutputStream;

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class AddCourse {

    public static void main(String... args) {

        System.out.println("please input two values: ");

        try (   Scanner in = new Scanner(System.in);
                FileOutputStream file = new FileOutputStream("test.csv");
                OutputStreamWriter out = new OutputStreamWriter(file, "UTF-8");
                BufferedWriter buf = new BufferedWriter(out);
                PrintWriter writer = new PrintWriter(buf)) {

            String first = in.nextLine();
            String second = in.nextLine();

            writer.print(first);
            writer.print("\t");
            writer.print(second);
            writer.println();

        } catch (Throwable t) {
            t.printStackTrace();
        }

        System.out.println("done.");
    }
}
Gmugra
  • 468
  • 7
  • 10
-1
Map<String, List<String>> map = new TreeMap<String, List<String>>();

list = in.nextLine();

Those 2 lines show that you have no clue what you are doing. You probably want to do something like this.

ArrayList<String> input = new ArrayList<String>();
input.add(in.nextLine());
input.add(in.nextLine());

for (String val : input) {
     add.write(val);
     add.write("\n");
}
HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37