JAVA-Correct way not to read enter key

Asked

Viewed 77 times

0

I am making the problem 1168 - LED and would like to know if there is a way to make my in.nextLine() do not read the enter key,I managed creating a variable to read this enter,but I’m sure it is not the most correct way to do this.

https://www.urionlinejudge.com.br/judge/pt/problems/view/1168 (Uri problem for anyone who wants to see, I accept suggestions on code improvement as well).

import java.util.Scanner;

public class Uri1168 {
    public static void main(String[]args){

    int i,n;
    int total = 0;
    String enter;


    Scanner in = new Scanner(System.in);
    n = in.nextInt();

    enter = in.nextLine();
    for(i = 0;n>i;n--){
        total = 0;

    String v;
    v = in.nextLine();
    char[] num = v.toCharArray();

    for (char c : num) { // divide o numero em cada numero separado
        int f = Integer.parseInt(c+"");
        if (f==1){
            total += 2;
        }else if (f==2){
            total += 5;
        }else if (f==3){
            total += 5;
        }else if (f==4){
            total += 4;
        }else if (f==5){
            total += 5;
        }else if (f==6){
            total += 6;
        }else if (f==7){
            total += 3;
        }else if (f==8){
            total += 7;
        }else if (f==9){
            total += 6;
        }else if (f==0){
            total += 6;
        }




    }
    System.out.println(total+" leds\n");
    }

    in.close();
}
}
  • I’m sorry to close your question as a duplicate, but there in the question Linkei (along with their answers) he explains why you are having this problem.

  • On the improvement of the code, I suggest taking a look at the structure switch instead of if

  • 1

    Or better yet, instead of switch or if, use a table in the form of an array to find the numbers to be summed. int[] tabela = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; total += tabela[f]; - CC @Tuxpilgrim

  • Beauty Victor,I will take a look. Thank you,had not found. Thanks also Tuxpilgrim ,the switch really is better

No answers

Browser other questions tagged

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