有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java丢弃最后一个ArrayList

我有这个程序,它应该存储在一个txt文件的信息所要求的键盘,但这并没有保存最后的要求将是一个ArrayList称为“科尔”,如果我可以帮助这个请

package lab1;
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Lab1 {

public static void main(String[] args) {
     // TODO code application logic here

    int opc;
    Scanner entrada = new Scanner(System.in);
    System.out.println("MENU: Eligue su opcion\n");
    System.out.println("1.- Crear archivo");
    System.out.println("2.- Mas opciones para mostrar");
    System.out.println("3.- Escritura en el archivo");
    System.out.println("4.- Salir\n");
    System.out.println("Introdusca su opcion:");
    opc = entrada.nextInt();

    if (opc == 1){
        FileWriter fichero = null;
        PrintWriter pw = null;
        try{

        //Tiempo al programa de suspencion.
            Thread.sleep(2000);
            //se abre el archivo txt si este esta creado y el TRUE hace que no borre la informacio que este contiene si es que existe el txt.
            fichero = new FileWriter("C:\\Users\\Levenor\\Desktop\\Test\\test.txt", true);
            pw = new PrintWriter(fichero);

            //En estos parametros se pide la informacion para agregarlo en una lista para luego colocar en el txt.

            ArrayList<String> nombre = new ArrayList<String>();
            System.out.println("Ingrese Nombre: ");
            nombre.add(entrada.nextLine());
            Thread.sleep(8000);

            ArrayList<String> rut = new ArrayList<String>();
            System.out.println("Ingrese RUT: ");
            rut.add(entrada.nextLine());
            Thread.sleep(8000);

            ArrayList<String> edad = new ArrayList<String>();
            System.out.println("Ingrese EDAD: ");
            edad.add(entrada.nextLine());
            Thread.sleep(4000);

            // here is arraylist is not save in the archive txt    

            ArrayList<String> cole = new ArrayList<String>();
            System.out.println("Ingrese COLEGIO: ");
            cole.add(entrada.nextLine());
            Thread.sleep(4000);

            //Esta linea hace que las listas se guarden en el archivo de txt.
            pw.println("" +nombre +rut +edad +cole);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
            // Nuevamente aprovechamos el finally para
            // asegurarnos que se cierra el fichero.
            if (null != fichero)
                fichero.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }  
    }
}

共 (1) 个答案

  1. # 1 楼答案

    你在opc = entrada.nextInt();之后缺少对^{}的调用-它没有正确地推进输入。调用^{}时,它使用int值,但不使用末尾的换行符。因此,您需要调用nextLine()以将Scanner移过它

    系统也应该暂停——你不需要使用Thread.sleep()——这是它与你的输入不正确对齐的症状。您还应该确保关闭finally块中的Scanner entrada

    示例代码:

    public static void main(String[] args) {
        Scanner entrada = new Scanner(System.in);
        System.out.println("Introdusca su opcion:");
    
        // Get the int value here, but not the newline
        int opc = entrada.nextInt();
    
        // **** Advance to the next line here ****
        entrada.nextLine();
    
        if (opc == 1){
            try{
                System.out.println("Ingrese Nombre:");
                String nombre = entrada.nextLine();
    
                System.out.println("Ingrese RUT:");
                String rut = entrada.nextLine();
    
                System.out.println("Ingrese EDAD:");
                String edad = entrada.nextLine();
    
                System.out.println("Ingrese COLEGIO:");  
                String cole = entrada.nextLine();
    
                System.out.println("nombre: " + nombre);
                System.out.println("rut: " + rut);
                System.out.println("edad: " + edad);
                System.out.println("cole: " + cole);
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // make sure to close the Scanner
                entrada.close();
            }
        }
        System.exit(0);
    }
    

    示例输出:

    Introdusca su opcion:
    1
    Ingrese Nombre:
    nombre
    Ingrese RUT:
    rut
    Ingrese EDAD:
    edad
    Ingrese COLEGIO:
    colegio
    
    nombre: nombre
    rut: rut
    edad: edad
    cole: colegio