英文:
Crash before a method in Java
问题
package com.app;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
    // cmd_in
    static Scanner entradaConsola = new Scanner(System.in);
    public static void main(String[] args) throws FileNotFoundException {
        int cantMax = 10;
        String[] nombresVendedores = new String[cantMax];
        String[] passVendedores = new String[cantMax];
        String[] codPeliculas = new String[cantMax];
        String[] nombrePeliculas = new String[cantMax];
        String[] salaPeliculas = new String[cantMax];
        int[] dispAsientos = new int[cantMax];
        int[] precioEntrada = new int[cantMax];
        String opcion = "1";
        int cantVendedores = leerArchivos("vendedores.txt", opcion, nombresVendedores, passVendedores, codPeliculas, nombrePeliculas,
                salaPeliculas, dispAsientos, precioEntrada);
        opcion = "2";
        int cantPeliculas = leerArchivos("peliculas.txt", opcion, nombresVendedores, passVendedores, codPeliculas, nombrePeliculas,
                salaPeliculas, dispAsientos, precioEntrada);
        while (true) {
            System.out.println("=== WELCOME TO CINE HOYTS ===");
            System.out.println("Choose one option: ");
            System.out.println("A) Login");
            System.out.println("B) Exit");
            String eleccion = entradaConsola.nextLine();
            if (eleccion.equalsIgnoreCase("a")) {
                String user = entradaConsola.nextLine();
                String pass = entradaConsola.nextLine();
                if (verificarUsuario(nombresVendedores, passVendedores, cantVendedores, user, pass)) {
                    System.out.println("Sucesfull Login");
                    System.out.println(cantPeliculas);
                } else
                    System.out.println("User/Password incorrect.");
            } else if (eleccion.equalsIgnoreCase("b")) {
                System.out.println("Good Bye. EXITING!");
                break;
            } else
                System.out.println("Wrong options");
        }
    }
    // read file
    public static int leerArchivos(String nombreArchivo, String opcion, String[] nombresVendedores, String[] passVendedores,
                                   String[] codPeliculas, String[] nombrePeliculas, String[] salaPeliculas, int[] dispAsientos,
                                   int[] precioEntradas) throws FileNotFoundException {
        File archivo = new File(nombreArchivo);
        Scanner lector = new Scanner(archivo);
        int cant = 0;
        switch (opcion) {
            case "1" -> {
                while (lector.hasNextLine()) {
                    String linea = lector.nextLine();
                    String[] partes = linea.split(",");
                    nombresVendedores[cant] = partes[0];
                    passVendedores[cant] = partes[1];
                    cant++;
                }
            }
            case "2" -> {
                while (lector.hasNextLine()) {
                    String linea = lector.nextLine();
                    String[] partes = linea.split(",");
                    codPeliculas[cant] = partes[0];
                    nombrePeliculas[cant] = partes[1];
                    salaPeliculas[cant] = partes[2];
                    dispAsientos[cant] = Integer.parseInt(partes[3]);
                    precioEntradas[cant] = Integer.parseInt(partes[4]);
                    cant++;
                }
            }
        }
        return cant;
    }
    // Verify if user exists
    public static boolean verificarUsuario(String[] nombresVendedores, String[] passVendedores, int cantVendedores, String user, String pass) {
        for (int i = 0; i < cantVendedores; i++) {
            if (nombresVendedores[i].equalsIgnoreCase(user)) {
                if (passVendedores[i].equalsIgnoreCase(pass)) {
                    return true;
                }
            }
        }
        return false;
    }
}
英文:
I'm working in activity for College, I'm trying to do a menu with options clearly, but when i put option "A" to introduce user and pass to verify the user and login in the system, the program doesnt move, doesnt ask the user. Just before system.out.blablaba option A or B.
I just want to know if the keyboard input is correlativo to some element in the user and password arrays
package com.app;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
//cmd_in
static Scanner entradaConsola = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
int cantMax = 10;
String[] nombresVendedores = new String[cantMax];
String[] passVendedores = new String[cantMax];
String[] codPeliculas = new String[cantMax];
String[] nombrePeliculas = new String[cantMax];
String[] salaPeliculas = new String[cantMax];
int[] dispAsientos = new int[cantMax];
int[] precioEntrada = new int[cantMax];
String opcion = "1";
int cantVendedores = leerArchivos("vendedores.txt", opcion,nombresVendedores,passVendedores,codPeliculas,nombrePeliculas,
salaPeliculas,dispAsientos,precioEntrada);
opcion = "2";
int cantPeliculas = leerArchivos("peliculas.txt", opcion,nombresVendedores,passVendedores,codPeliculas,nombrePeliculas,
salaPeliculas,dispAsientos,precioEntrada);
while (true){
System.out.println("=== WELCOME TO CINE HOYTS ===");
System.out.println("Choose one option: ");
System.out.println("A) Login");
System.out.println("B) Exit");
String eleccion = entradaConsola.nextLine();
if (eleccion.equalsIgnoreCase("a")){
String user = entradaConsola.nextLine();
String pass = entradaConsola.nextLine();
if (verificarUsuario(nombresVendedores,passVendedores,cantVendedores,user,pass)){
System.out.println("Sucesfull Login");
System.out.println(cantPeliculas);
}else
System.out.println("User/Password incorrect.");
}else if (eleccion.equalsIgnoreCase("b")){
System.out.println("Good Bye. EXITING!");
break;
}else
System.out.println("Wrong options");
}
}
// read file
public static int leerArchivos(String nombreArchivo, String opcion, String[] nombresVendedores, String[] passVendedores,
String[] codPeliculas, String[] nombrePeliculas, String[] salaPeliculas, int[] dispAsientos,
int[] precioEntradas) throws FileNotFoundException {
File archivo = new File(nombreArchivo);
Scanner lector = new Scanner(archivo);
int cant = 0;
switch (opcion){
case "1" -> {
while (lector.hasNextLine()){
String linea = lector.nextLine();
String[] partes = linea.split(",");
nombresVendedores[cant] = partes[0];
passVendedores[cant] = partes[1];
cant++;
}
}
case "2" -> {
while (lector.hasNextLine()){
String linea = lector.nextLine();
String[] partes = linea.split(",");
codPeliculas[cant] = partes[0];
nombrePeliculas[cant] = partes[1];
salaPeliculas[cant] = partes[2];
dispAsientos[cant] = Integer.parseInt(partes[3]);
precioEntradas[cant] = Integer.parseInt(partes[4]);
cant++;
}
}
}
return cant;
}
// Verify if user exists
public static boolean verificarUsuario(String[] nombresVendedores, String[] passVendedores, int cantVendedores,String user,String pass){
for (int i = 0; i < cantVendedores; i++){
if (nombresVendedores[i].equalsIgnoreCase(user)){
if (passVendedores[i].equalsIgnoreCase(pass)){
return true;
}
}
}
return false;
}
}
答案1
得分: 1
我认为你的程序已经在运行...
只需要在获取用户名和密码之前打印一段文本。然后就会更清晰...
System.out.println("输入用户名:");
String user = entradaConsola.nextLine();
System.out.println("输入密码:");
String pass = entradaConsola.nextLine();
英文:
I think your program is already working...
Just print a text before taking user and pass. Then it will be more clear...
System.out.println("Enter username:");
String user = entradaConsola.nextLine();
System.out.println("Enter password:");
String pass = entradaConsola.nextLine();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论