[RESOLVIDO]Problemas com leitura de arquivo

1 resposta
guijocargo

Boa Noite galera do guj, to aqui na aula de Programação de Computadores (Java) e to com problema nesse código, não estou conseguindo ler apena o que está depois do igual para por em um atributo privado da classe e pelo mesmo motivo (creio eu) na hora de setar os atributos booleanos como true se o texto lido for sim, e na hora de alguém pode me ajudar?

O resultado que eu quero, seria o seguinte:
O atributo cor deve guardar "prata", e não "Cor = prata"

public void carregarCarros(String path, String nomeArquivo) {

        try {
            
            File arquivo = new File(path, nomeArquivo);
            Scanner sc = new Scanner(arquivo);

            try {
                carro = sc.next().toString();

                sc.nextLine();

                if (sc.findInLine("Aerofolio").matches("Aerofolio")) {
                    sc.hasNext("=");
                }
                item = sc.nextLine();
                if (item.equalsIgnoreCase("sim")) {
                    itemAdd1 = true;
                } else if (item.equalsIgnoreCase("não")) {
                    itemAdd1 = false;
                }

                if (sc.findInLine("Som USB").matches("Som USB")) {
                    sc.hasNext("=");
                }
                item = sc.nextLine();
                if (item.equalsIgnoreCase("sim")) {
                    itemAdd2 = true;
                } else if (item.equalsIgnoreCase("não")) {
                    itemAdd2 = false;
                }

                if (sc.findInLine("Airbag").matches("Airbag")) {
                    sc.hasNext("=");
                }
                item = sc.nextLine();
                if (item.equalsIgnoreCase("sim")) {
                    itemAdd3 = true;
                } else if (item.equalsIgnoreCase("não")) {
                    itemAdd3 = false;
                }

                sc.hasNext("=");
                banco = sc.nextLine();

                sc.hasNext("=");
                cor = sc.nextLine();

                sc.hasNext("=");
                direcao = sc.nextLine();

                sc.hasNext("=");
                modelo = sc.nextLine();

                sc.hasNext("=");
                motor = sc.nextLine();

                sc.hasNext("=");
                item = sc.nextLine();
                if (item.equalsIgnoreCase("sim")) {
                    ar = true;
                } else if (item.equalsIgnoreCase("não")) {
                    ar = false;
                }

                sc.hasNext("=");
                tipoCombustivel = sc.nextLine();

                sc.hasNext("=");
                portas = sc.nextLine();

                sc.hasNext("=");
                velocidade = sc.nextLine();

            } catch (Exception ex) {
                ex.getMessage();
            } finally {
                sc.close();
            }

        } catch (FileNotFoundException fnfe) {
            System.out.println("Arquivo não encontrado...");
        } catch (NullPointerException npe) {
            System.out.println("Arquivo perdido....");
        } 
    }
Astra : 
   Aerofolio = sim
   Som USB = sim
   Airbag = sim
   Banco = couro
   Cor = prata
   Direcao = hidr
   Modelo = ast
   Motor = 2.0
   Ar = sim
   Tipo do Combustivel = flex
   Portas = 4
   Placa = AAA-0000
   Velocidade Atual = 0
   Velocidade Máxima = 220

obrigado mais uma vez e peço desculpa se não fui mto explicito em minha dúvida, att e no aguarde de respostas :-)

1 Resposta

guijocargo

Consegui resolver meus problemas, segue o código, desculpa ai o post meio q 'inutil', espero q ajude alguem:

public void carregarCarros(String path, String nomeArquivo) {

        try {

            File arquivo = new File(path, nomeArquivo);
            Scanner sc = new Scanner(arquivo);

            try {
                carro = sc.next().toString();

                sc.nextLine();

                if (sc.findInLine("Aerofolio = ").matches("Aerofolio = ")) {

                    item = sc.nextLine();
                    if (item.equalsIgnoreCase("sim")) {
                        itemAdd1 = true;
                    } else if (item.equalsIgnoreCase("não")) {
                        itemAdd1 = false;
                    }
                }

                if (sc.findInLine("Som USB = ").matches("Som USB = ")) {

                    item = sc.nextLine();
                    if (item.equalsIgnoreCase("sim")) {
                        itemAdd2 = true;
                    } else if (item.equalsIgnoreCase("não")) {
                        itemAdd2 = false;
                    }
                }

                if (sc.findInLine("Airbag = ").matches("Airbag = ")) {

                    item = sc.nextLine();
                    if (item.equalsIgnoreCase("sim")) {
                        itemAdd3 = true;
                    } else if (item.equalsIgnoreCase("não")) {
                        itemAdd3 = false;
                    }
                }

                if (sc.findInLine("Banco = ").matches("Banco = ")) {
                    banco = sc.nextLine();
                }

                if (sc.findInLine("Cor = ").matches("Cor = ")) {
                    cor = sc.nextLine();
                }

                if (sc.findInLine("Direcao = ").matches("Direcao = ")) {
                    direcao = sc.nextLine();
                }

                if (sc.findInLine("Modelo = ").matches("Modelo = ")) {
                    modelo = sc.nextLine();
                }

                if (sc.findInLine("Motor = ").matches("Motor = ")) {
                    motor = sc.nextLine();
                }

                if (sc.findInLine("Ar = ").matches("Ar = ")) {

                    item = sc.nextLine();
                    if (item.equalsIgnoreCase("sim")) {
                        ar = true;
                    } else if (item.equalsIgnoreCase("não")) {
                        ar = false;
                    }
                }

                if (sc.findInLine("Tipo do Combustivel = ").matches("Tipo do Combustivel = ")) {

                    tipoCombustivel = sc.nextLine();
                }

                if (sc.findInLine("Portas = ").matches("Portas = ")) {

                    portas = sc.nextLine();
                }

                if (sc.findInLine("Placa = ").matches("Placa = ")){
                    placas = sc.nextLine();
                }

                if (sc.findInLine("Velocidade Atual = ").matches("Velocidade Atual = ")) {
                    velocidade = sc.nextLine();
                }

            } catch (Exception ex) {
                ex.getMessage();
            } finally {
                sc.close();
            }

        } catch (FileNotFoundException fnfe) {
            System.out.println("Arquivo não encontrado...");
        } catch (NullPointerException npe) {
            System.out.println("Arquivo perdido....");
        }
    }
Criado 20 de outubro de 2011
Ultima resposta 20 de out. de 2011
Respostas 1
Participantes 1