Repetitive array

Asked

Viewed 42 times

0

Good morning guys,

I am new here and new also in JAVA. I have a problem trying to solve it and I am not getting.

When I run the program, the array within the open catalog method repeats countless times even if I put a parameter of the file I opened and read the size of the . db.

When generating the catalog in a function, the catalog becomes huge and there are only 10 items inside.

Could you help me?

package javaapplication6;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;


public class JavaApplication6 {

public static int[] codigo;
public static String[] nome;
public static double[] preco;
public static String[] descricao;

public static void main(String[] args) throws IOException{

    Scanner in = new Scanner(System.in);

    System.out.println("Muito obrigado pelo Acesso! O que deseja fazer primeiro?");
        System.out.println("\n===========================");
        System.out.println("|     1 - Catálogo        |");
        System.out.println("|     2 - Carrinho        |");
        System.out.println("|     3 - Finalizar       |");
        System.out.println("|     4 - Voltar          |");
        System.out.println("|     0 - Sair            |");
        System.out.println("===========================\n");

        int opcao = in.nextInt();
            switch (opcao){
                case 1:
                carregarcatalogo();
                break;
                case 3: 
                finalizarcompra();
            }

}



public static void carregarcatalogo() throws IOException{
      Scanner in = new Scanner (System.in);

    DataInputStream br = new DataInputStream(new FileInputStream("produto.db"));

        int tamanho = br.readInt();
        codigo = new int[tamanho];
        nome = new String[tamanho];
        preco = new double[tamanho];
        descricao = new String[tamanho];

        for (int i = 0; i < tamanho; i++) {
        codigo[i] = br.readInt();
        nome[i] = br.readUTF();
        preco[i] = br.readDouble();
        descricao[i] = br.readUTF();

        abrircatalogo(tamanho, codigo, nome, preco, descricao);


}
 }
    public static void abrircatalogo(int tamanho, int[] codigo, String[] nome, double[] preco, String[] descricao){
    Scanner in = new Scanner (System.in);

            System.out.printf("+-----+------+------+------+---|\n");
        for (int i = 0; i < tamanho; i++) {
            System.out.printf("| %06d | %-15s | R$%7.2f | %-35s |\n",
                codigo[i], nome[i], preco[i], descricao[i]);
            System.out.printf("+-----+------+------+------+---|\n");
        }

        System.out.println("Digite o código para selcionar o produto e pressione enter para confirmar");
}

public static void finalizarcompra(){

    Scanner in = new Scanner(System.in);

    System.out.println("Para finalizar sua compra, preencha os dados");

    System.out.print("Nome completo que está no Cartão de Crédito: ");
    String cliente = in.next();
    System.out.print("Numero do Cartão de Crédito: ");
    int numCredito = in.nextInt();
    System.out.print("Data de vencimento: ");
    int vencimento = in.nextInt();
    System.out.print("CVV: ");
    int cvv = in.nextInt();
}
}
  • 1

    Within the carregarcatalogo() has a for who calls the abrircatalogo(), which also has a for. I believe this call to abrircatalogo() should be out of the for.

  • That’s right! Thank you so much for your help

  • I am happy to have helped. I put as an answer to finish this question.

  • No problem. Thank you very much

1 answer

1


There is a nested loop: the abrircatalogo() is inside a loop, but internally executes another loop.

Remove the abrircatalogo() from within the for

Browser other questions tagged

You are not signed in. Login or sign up in order to post.