Quit command in java string, show name and number after typing QUIT

Asked

Viewed 113 times

-2

Create a program to accept the typing of a person’s name and phone, while the name typed is different from QUIT. When typed EXIT, show the name and phone number of the first and last person typed, and the number of names typed.

I’m having trouble with everything I do, if anyone can help :)

my code:

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package atividade2;

import java.util.Scanner;

/**
 *
 * @author mojan
 */
public class Atividade2 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String nome;
        int numero;
        String sair;
        int count;

       while(true){ 
        Scanner leia = new Scanner(System.in);
        System.out.println("Digite um nome");
        nome = leia.nextLine();        
        System.out.println("Digite um número de telefone");
        numero = leia.nextInt();
        if(nome.equals("sair") || nome.isEmpty());
            break;
    }
  }
}   
  • 1

    Could you please [Dit] the question and add a better title that describes what your question is? " help here please" does not say what the question is about. Take advantage in editing and add in the body of the question also a description of your question. Error? Which one? Not working properly? When? Which entries did you put in? What outputs did you generate? What outputs were expected?

  • sorry, ready

  • 2

    "I’m having trouble with everything I do"?

  • Not directly related, but finally: telephone is not a number (in the sense of being a numerical value that represents a quantity or that can be calculated, etc). It is simply information that happens to use digits (but it can also have letters, not counting zeros on the left, that will disappear if you convert to number). Read here (despite not talking about phone, the problem is the same)

2 answers

0

"I’m having problems in everything", if you direct your doubt it’s easier to help you.

But your code has some problems, your break is being run always, regardless of the return of your if, because it is out, since you pasted a ; at the end. Removing the ; will cause your code to stop only if if it is true.

But you still have another problem, your Scanner will crash and will always skip the name from the second time, search and find out why, but to solve this is only in place of

System.out.println("Digite um número de telefone");
numero = leia.nextInt();

you use

System.out.println("Digite um número de telefone");
numero = Integer.valueOf(leia.nextLine());

You receive a String, but pass the value to the number by converting to integer.

0

You also need to declare names and numbers as a list in order to count, show the quantity and also take the first value and last value entered, in case it would look like this:

public static void main(String[] args) {
    String nome;
    List<Integer> numeros = new ArrayList<>();
    List<String> nomes = new ArrayList<>();
    final String SAIR = "SAIR";
    Scanner leia = new Scanner(System.in);
    
    while (true) {
        System.out.println("Digite um nome");
        nome = leia.nextLine();
        if (nome.equalsIgnoreCase(SAIR))
            break;
        nomes.add(nome);
        
        System.out.println("Digite um número de telefone");
        numeros.add(Integer.valueOf(leia.nextLine()));
        
    }
    
    System.out.println("Primeira pessoa digitada: \nNome:" + nomes.get(0) + ", Telefone: " + numeros.get(0));
    System.out.println("Última pessoa digitada: \nNome:" + nomes.get(nomes.size() - 1) + ", Telefone: " + numeros.get(numeros.size() - 1));
    System.out.println("Quantidade de nomes: " + nomes.size());
}

Browser other questions tagged

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