简体   繁体   中英

How can i call directly Java Arraylist to be printed to a txt file?

This is my code, this is not a specific program. I just exercise myself with the java ArrayLists and making txt File and to store Arraylist variables in this file. I try to make class where i make method for scanner to make a input and then to store it to arraylist. The second method is to make txt file. And the third method is to add thing to this txt file. All methods work, but i don't know how to import Arraylist data to the txt file. I am a begginer and question maybe stupid for some people, i am sorry.

package com.company;

import java.util.ArrayList;
import java.util.Scanner;

import java.io.File;  // Import the File class
import java.io.IOException;  // Import the IOException class to handle errors


import java.io.FileWriter;   // Import the FileWriter class



public class Bon2Scanner {





    public  static void Scaner() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Write String name:");

        ArrayList<String> Name = new ArrayList<>();
        Name.add(scanner.next());

        System.out.println("Write age int :");
        ArrayList<Integer> Age = new ArrayList<>();
        Age.add(scanner.nextInt());

        System.out.println("Array list with name" + " " + Name);
        System.out.println("Array list with age " + " " + Age);

    }




    // method for making a file


    public void bonMetodScanner() {

        try {
            File bon2 = new File("Bon2Scanner.txt");
            if (bon2.createNewFile()) {
                System.out.println("File created: " + bon2.getName());
            } else {
//                        System.out.println("File already exists. ");
                return ;
            }
        } catch (IOException e) {
            System.out.println("An error occured. ");
            e.printStackTrace();
        }

    }



    // method for writing in a file



    public  void bonMetodWriteScanner() {

        try {
            FileWriter Bon1Write = new FileWriter("Bon2Scanner.txt");
//            Bon1Write.write(getYearCar() + System.lineSeparator());

//
            Bon1Write.write("some text 2" + System.lineSeparator());

            Bon1Write.write( ?Here i need to add my ArrayList? + System.lineSeparator()); ?????

            Bon1Write.close();
            System.out.println("Successfully wrote to the file.");
        } catch (IOException e) {
            System.out.println("An error occured.");
            e.printStackTrace();
        }

    }




}

You've made a good start with reading in data and adding it to your ArrayList.

I would recommend leaving I/O until you've got everything working, using System.out instead.

It looks like you want to use the data entered into the ArrayLists you create in the method Scaner() in another method (bonMetodWriteScanner()).

I would recommend making these ArrayLists global variables (meaning they can be used by all methods of your program) by declaring the age and name ArrayLists outside of the methods at the top of the class:

public class Bon2Scanner{
    public static ArrayList<String> name = new ArrayList<>();
    public static ArrayList<Integer> age = new ArrayList<>();

From here you can access and modify these variables in any method by using

public static void Scaner(){
    Scanner scanner = new Scanner(System.in);
    System.out.println("Write String name:");

    name.add(scanner.next());

Now when you're writing to the file, you can use the ArrayLists.

Some notes:

Scattering static variables throughout a program is considered pretty bad practice, but for the purposes of learning java syntax and variable scope I think it's fine.

Standard practice for naming variables is usually a lower-case character to begin with, which is why I changed your ArrayList variable names to all lower case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM