Type a value with "." dot. Ex: 7.5

Asked

Viewed 35 times

-4

Good evening, I started using JAVA a little while ago, I wanted to know how to make the user can type the value with "." point. In case anyone can help, I’d appreciate it!!

import java.io.Console;
import java.util.Scanner;

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

  Scanner sc = new Scanner(System.in);
  System.out.println("Entre com o numero de notas");
  int quantidadeNotas = sc.nextInt();

  int notas[] = new int[quantidadeNotas];

  for (int i = 0; i < quantidadeNotas; i++){
  System.out.println ("Entre com a nota " + (i+1));
  notas[i] = sc.nextInt();
  }
  for (int i = 0; i < quantidadeNotas; i++){
  System.out.println (notas[i]);
  }
  }
  }
  • "how do I get the user to type the value with "."" cannot "force" the user to type, but can validate and ask to type again, now it is no use doing this if you are reading an integer, integer has no point nextInt

1 answer

0


No need to leave this question useless already, not everyone knows much about programming. He is a New Contributor.

import java.util.Scanner;

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

        Scanner sc = new Scanner(System.in);
        System.out.println("Entre com o numero de notas: ");
        int quantidadeNotas = sc.nextInt();

        double notas[] = new double[quantidadeNotas];

        for (int i = 0; i < quantidadeNotas; i++){
            System.out.println ("Entre com a nota [" + (i+1) + "]: ");
            notas[i] = sc.nextDouble();
        }
        for (int i = 0; i < quantidadeNotas; i++){
            System.out.println (notas[i]);
        }
    }
}

I made an adjustment and at the time of putting the decimal note put ",". 10,0 thus.

"how do I get the user to type the value with "."" can’t "force" the user to type, but can validate and ask to type again, now it’s no use doing this if you’re reading an integer, integer has no nextInt point - Ricardo Punctual 5 hours ago

It may not require but rather perform a validation check. Furthermore, depending on the language of the country, USA uses "." and Brazil uses ",". For example if you put only 6, we can make a transformation to 6.0. Everything is in its time. Ok?

  • "Tanto Faz is a new user. Be kind to ask for clarification, comment, and reply".

  • 1

    "USA uses "." and Brazil uses ","" - you can define if the Scanner will accept comma or dot, simply set the locale, see here and here - if it does not define a locale, he uses the default that is configured in JVM

Browser other questions tagged

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