0

This program should calculate your points from all given variables. The result should be calculated and after I give the values to these variables. I need to set a limit for Vijueshmeria from 0 to 10, detyrat from 0 to 20, k1 from 0 to 30, k2 from 0 to 30, projekt from 0 to 10.

I am not being able to set a limit for any of the above values for the inputs. Hopefully, somebody will guide me on this.

package studenti;
import java.util.Scanner;
public class Studenti {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // deklarimi i variables, inicializimi behet nga perdoruesi
        int Vijueshmeria;
        // deklarimi i variablave te tipit double
        double detyrat, k1, k2, provimi, projekt, rezultati;
        // deklarimi dhe inicializmi i variablave string
        String emri="Abdurrahman";
        String mbiemri="Cakolli";
        Scanner obj=new Scanner(System.in);
        System.out.println("Shkruani vijueshmerine (int):");
        Vijueshmeria=obj.nextInt();
        System.out.println("Shkruani detyrat(double):");
        detyrat =obj.nextDouble();
        System.out.println("Shkruani detyren e k1(double):");
        k1=obj.nextDouble();
        System.out.println("Shkruani detyrat e k2(double):");
        k2=obj.nextDouble();
        System.out.println("Shkruani piket e projektit :");
        projekt=obj.nextInt();
        provimi=((k1+k2)/2*0.6+detyrat+projekt);
        rezultati=Vijueshmeria+detyrat+provimi;
        System.out.println("\n"+emri+""+mbiemri+",ju keni:"+rezultati+"pike");
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • You can't prevent user from providing incorrect values. All you can do is validate it after value is provided and in case of invalid value either ask for value again or end your application. – Pshemo Dec 24 '19 at 22:24
  • Possibly related: [How to use Scanner to accept only valid int as input](https://stackoverflow.com/q/2912817) – Pshemo Dec 24 '19 at 22:24
  • To expand on @Pshemo's comment, I would recommend using [bean validation](https://javaee.github.io/tutorial/bean-validation001.html) tif possible o verify that inputs are valid. – Turing85 Dec 24 '19 at 22:25
  • @Turing85 Could you clarify this in details ? – Abdu Cakolli Dec 24 '19 at 22:49
  • The tutorial I linked clarifies it in great detail. – Turing85 Dec 24 '19 at 22:56

1 Answers1

0

There isn't a built-in way to do what you want. Instead, you have to write code to read a value, check if it is within the allowed range, and possibly ask the user again to enter a valid value.

Here's an example showing how to do this for one of your variables "vijueshmeria" (note that I edited the variable name to be "vijueshmeria" instead of "Vijueshmeria" – that is, start with a lowercase letter – to follow standard Java naming conventions).

The way this code works is to declare the initial value with "-1", then enter a "while" loop to prompt for a value. The next time through the loop, if the value for "vijueshmeria" isn't less than 0 and isn't greater than 10, it skips ahead.

Scanner obj = new Scanner(System.in);
int vijueshmeria = -1;
while (vijueshmeria < 0 || vijueshmeria > 10) {
    System.out.println("Shkruani vijueshmerine (int):");
    vijueshmeria = obj.nextInt();
}
System.out.println("vijueshmeria ok: " + vijueshmeria);

Here's a sample run showing the output:

Shkruani vijueshmerine (int):
11
Shkruani vijueshmerine (int):
-2
Shkruani vijueshmerine (int):
5
vijueshmeria ok: 5
Kaan
  • 5,434
  • 3
  • 19
  • 41