lxtrxi
lxtrxi

Reputation: 55

Boolean Operators not working

I'm having a bit of trouble with Java (still learning). I'm trying to make a program that calculates the number of students and then the number of tablets that would be required for registration, but I'm not getting anywhere fast. I've tried using Boolean operators but I'm just getting Syntax errors.

The actual question I'm trying to work with is:

If there are 40 or fewer students, then only one tablet is required. If the number is over 40 then a further tablet is required for every additional 30 students in the class. So, a class of 41-70 students would require 2 tablets, a class of 70-100 would require 3, and so on.

Your output should include the number of tablets needed and the number of students in the class:

It's not going how I expected and I'm quite stuck, any help would be very appreciated!

import javax.swing.JOptionPane;
public class Students {

public static void main(String[] args) {

    int numberOfStudents;
    int tablets = 1;

    numberOfStudents=Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students in the class: "));

    if (numberOfStudents <= 40){
        System.out.println("There are a total of " + numberOfStudents + " students in this class");
        System.out.println("You will need " + tablets + " tablet for the e-register");

    else if (numberOfStudents => 41 || <= 70){
        System.out.println("There are a total of " + numberOfStudents + " students in this class");
        System.out.println("You will need " + tablets + 1 + " tablets for the e-register");

    else if (numberOfStudents => 71 || <= 100){
        System.out.println("There are a total of " + numberOfStudents + " students in this class");
        System.out.println("You will need " + tablets + 2 + " tablets for the e-register");

    else if (numberOfStudents => 101 || <= 120){
        System.out.println("There are a total of " + numberOfStudents + " students in this class");
        System.out.println("You will need " + tablets + 3 + " tablets for the e-register");

        {   }
    }

}

Upvotes: 0

Views: 274

Answers (5)

Richard Irons
Richard Irons

Reputation: 1473

Your biggest issue here is that you're approaching the problem badly. Yes, I could help you resolve your problems with Boolean operators, but how many times are you going to type out near-identical if statements to allow for more and more students? Once you've done 20 of them, what would happen if you want to change it to print out "You need Y tablets for X students."? You'll have to go through all those places where you've printed out the message and change it. Or what happens if there are 30,000 students and your code only includes if conditions that allow for 1,000?

Here's what your program should do, and it's what you'd do in real life if you had to write a sign to stick on the wall:

  1. Decide how many tablets are necessary
  2. Output a message telling the user of this

First let's look at the rules:

If there are 40 or fewer students, then only one tablet is required. If the number is over 40 then a further tablet is required for every additional 30 students in the class. So, a class of 41-70 students would require 2 tablets, a class of 70-100 would require 3, and so on.

For a start, that's ambiguous. 40 or fewer needs one tablet: fine. Then it says that for over 40, then a further tablet is required for every additional 30 students in the class, which makes sense, except for the next example. It says that for 41 students, 2 tablets are required. However, 41 is not "an additional 30" above 40. It's one above. It seems the rule is really "if the number is above 40, then an extra tablet is required, along with further extra tablet is required for every additional 30 students". Finally, it says that for 41-70 students, 2 tablets are required, and for 70-100 students, 3 tablets are required. Uh, so how many tablets are required for 70 students? 2 or 3?

I think we'll have to assume that, since 41 is the point where we need 2 tablets, 71 is the point where we hit 3 tablets.

Now let's look at all that code. This line gets you your number of students:

numberOfStudents=Integer.parseInt(
    JOptionPane.showInputDialog("Enter the number of students in the class: "));

That's fine. Now, instead of putting in all those ifs, how about using maths to get the computer to decide how many tablets we need? Computers are pretty good at maths.

We know we always need one tablet, right? Because "40 or fewer" students require one tablet. So we can get started there:

int tablets = 1;

And now we can do the next bit: if there are more than 40 students, then we have one extra tablet, plus one further extra tablet for every 30 students beyond the original 40. Like this:

if(numberOfStudents > 40) {
    tablets += 1 + (numberOfStudents - 41) / 30;
}

Now we know how many tablets we need, we can tell the user. You've already done this bit - but now you only need to do it once.

System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");

Another advantage of doing it like this is that now your tablet variable holds the number of tablets you actually need. So if you were going to use that value again soon, it's ready. In the code you originally posted, the tablets variable always just contains 1. It may as well be a constant.

Ordinarily, the hard-coded numbers 40 and 30 would be turned into constants defined at the top of the code. This would make the numbers more understandable to a future reader of the code.

edit: just to show what all the code looks like together:

numberOfStudents=Integer.parseInt(
    JOptionPane.showInputDialog("Enter the number of students in the class: "));

int tablets = 1;
if(numberOfStudents > 40) {
    tablets += 1 + (numberOfStudents - 41) / 30;
}

System.out.println("There are a total of " + numberOfStudents + " students in this class");
System.out.println("You will need " + tablets + " tablet for the e-register");

Upvotes: 2

Tho
Tho

Reputation: 25100

Is it missing close-bracket?

if (numberOfStudents <= 40) {
        System.out.println("There are a total of " + numberOfStudents + " students in this class");
        System.out.println("You will need " + tablets + " tablet for the e-register");
} else if (numberOfStudents >= 41 && numberOfStudents <= 70) {
        System.out.println("There are a total of " + numberOfStudents + " students in this class");
        System.out.println("You will need " + tablets + 1 + " tablets for the e-register");
} else if (numberOfStudents >= 71 && numberOfStudents <= 100) {
        System.out.println("There are a total of " + numberOfStudents + " students in this class");
        System.out.println("You will need " + tablets + 2 + " tablets for the e-register");
} else if (numberOfStudents >= 101 && numberOfStudents <= 120) {
        System.out.println("There are a total of " + numberOfStudents + " students in this class");
        System.out.println("You will need " + tablets + 3 + " tablets for the e-register");
}

Update: please notice that:

  1. Syntax error: Use <= instead of =<
  2. Syntax error: Use numberOfStudents >= 41 || numberOfStudents <= 70 instead of numberOfStudents >= 41 || <= 70
  3. Logic error: In your case, you must use && instead of ||

Upvotes: 2

xxxvodnikxxx
xxxvodnikxxx

Reputation: 1277

1) You have bad conditions:

else if (numberOfStudents => 41 || <= 70){ 
  //code
 }

should be (you need exactly interval between):

else if (numberOfStudents => 41 && numberOfStudents  <= 70){ 
   //code
}

2) You have little trash in bracket too

if(condition){
 //code condition match
}else if(cond2){
 //code condition match 2
}

Btw:

import javax.swing.JOptionPane;
public class Students {

public static void main(String[] args) {

    int numberOfStudents;
    int tablets = 1;
    int limitForOne = 40;
    int limitForEveryNext = 30;


    numberOfStudents=Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students in the class: "));

 System.out.println("There are a total of " + numberOfStudents + " students in this class");

    if (numberOfStudents >limitForOne){
      //get how much more we need
      int diffCount = (numberOfStudents-limitForOne)/limitForEveryNext;
      //currenct count is above limit for one, but diff is smaller than limitForEveryNext (in fact we need 1 more)
       //maybe diffCount=diffCount+1; instead if, not sure
      if(diffCount == 0){
          diffCount=1;
      }
      tablets= tablets+diffCount;
    }

      System.out.println("You will need " + tablets + " tablet(s) for the e-register");
  //end main   
  }
  //end class
}

Should also work :)

Upvotes: 1

user3437460
user3437460

Reputation: 17474

There are 3 issues in your codes..

  1. You should use && instead of ||
  2. Your operator is typed wrongly (Use >= instead of =>)
  3. Your braces are wrong (Missing closing braces after each if-statements) There is also an extra open curly bracket after all if-statements.

Your if control structure should look like this..

if (numberOfStudents >= 40){
}
else if (numberOfStudents >= 41 && <= 70){
}
else if (numberOfStudents >= 71 && <= 100){ 
}      
else if (numberOfStudents >= 101 && <= 120){
}

Upvotes: 1

a.ndrea
a.ndrea

Reputation: 536

You have to consider BOTH the condition together, and to type again the var name. Also, place an ending brace(}) just before the starting of every else if statement. Alos, you don't need a { } at the end. That's the correct code.

if (numberOfStudents <= 40){
    System.out.println("There are a total of " + numberOfStudents + " students in this class");
    System.out.println("You will need " + tablets + " tablet for the e-register");
}
else if (numberOfStudents >= 41 && numberOfStudents <= 70){
    System.out.println("There are a total of " + numberOfStudents + " students in this class");
    System.out.println("You will need " + (tablets + 1) + " tablets for the e-register");
}
else if (numberOfStudents >= 71 && numberOfStudents <= 100){
    System.out.println("There are a total of " + numberOfStudents + " students in this class");
    System.out.println("You will need " + (tablets + 2) + " tablets for the e-register");
}
else if (numberOfStudents >= 101 && numberOfStudents <= 120){
    System.out.println("There are a total of " + numberOfStudents + " students in this class");
    System.out.println("You will need " + (tablets + 3) + " tablets for the e-register");
}

Upvotes: 2

Related Questions