1

I should Define four boolean variables as follows:

  • Freshman for students in levels 1 or 2.
  • Sophomore for students in levels between 3 and 5.
  • Junior for students in levels between 6 and 8.
  • Senior for students in levels 9 or 10.

the user enters the course code, then I decide which level is the student (user) and then define the 4 boolean variables depending on the level.

But I don't know how to do the equal() for two thing or more.

this is my code:

import java.util.*;

public class point8 {
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {

        // declaring
        String CourseCode, Level;
        boolean Freshman, Sophomore, Junior, Senior;

        // input
        System.out.println("Course code:");
        CourseCode = input.next().toUpperCase();
        System.out.println("\nCourse Code: " + CourseCode);

        // output
        Level = CourseCode.substring(CourseCode.length() - 1);
        System.out.println("Student Level: " + Level);

        Freshman = Level.equals("1");
        System.out.println("Freshman: " + Freshman);

        Sophomore = Level.equals("3");
        System.out.println("Sophomore: " + Sophomore);

        Junior = Level.equals("6");
        System.out.println("Junior: " + Junior);

        Senior = Level.equals("9");
        System.out.println("Senior: " + Senior);

    }
}

What shall I do to compare from level 1 or 2 for freshman and compare from level 3 to 5 for Sophomore ?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Duha
  • 92
  • 1
  • 9

2 Answers2

2

It seems to me that you're better of using integers, just parse the String to int.

For example:

int myLevel = Integer.parseInt(Level);
if(myLevel >= 3 && myLevel <= 5)
{
  System.out.println("Sophomore: " + Sophomore);
}

You might get an error if the user inserts a letter instead of a number, to avoid this you need to catch the exception and handle it. This however is an entire different story, but you should readup about it: https://docs.oracle.com/javase/tutorial/essential/exceptions/

Oceans
  • 3,445
  • 2
  • 17
  • 38
  • i did exactly as you did but i get: variable Sophomore might not have been initialized – Duha Oct 02 '15 at 15:00
  • Ah yes, I just copied that from your post. You need to change that initialize it first or just replace it with `true`. (Didn't why you wanted to print that in the first place anyway.) – Oceans Oct 02 '15 at 15:05
0
if(Level.equals("9") || Level.equals("10"))
{
  //Senior
}

Update: The OR operator is something you should learn in the first couple weeks. The only thing more basic is to just write out the second if statement.

if(Level.equals("9"))
{
  //Senior
}
else if(Level.equals("10"))
{
 //Senior
}
Seth Kitchen
  • 1,526
  • 19
  • 53