mercredi 9 janvier 2019

Can't use IF-STATEMENT in comparing String inputted values [duplicate]

This question already has an answer here:

I'm making my class project and I can't seem to figure out what is the problem with my code... I can't seem to compare my Inputted Values in my string variables..

import java.util.Scanner;

public class Exercise1 {
   public static void main(String []a) {

      String Year;
      String Name;
      String SchoolName;
      int Age;

      System.out.print("Name\t:\t");
      Name = out.nextLine();
      System.out.print("Age\t:\t");
      Age = out.nextInt();out.nextLine();
      System.out.print("Name of School\t:\t");
      SchoolName = out.nextLine();
      System.out.print("Year Level\t:\t");
      Year = out.nextLine();

      if(Year == "1") {
         Year = "First";
      }
      if(Year == "2") {
         Year = "Second";
      }
      if(Year == "3") {
         Year = "Third";
      }
      if(Year == "4") {
         Year = "Fourth";
      }
      if(Year == "5") {
         Year = "Fifth";
      }
      if(Year == "6th") {
         Year = "Sixth";
      }
      if(Year == "7th") {
         Year = "Seventh";
      }
      if(Year == "8th") {
         Year = "Eight";
      }

      System.out.print("Student Name\t:\t"+Name+"\n");
      System.out.print("School Enrolled\t:\t"+SchoolName+"\n");
      System.out.print("Year Level\t:\t"+Year+" Grade");
   }
}

I really can't seem to understand the reason why it wont work.. Please help me with this :)

Aucun commentaire:

Enregistrer un commentaire