-2

here's my code. i wanted to display the name of the month instead of number. but there's an error in my code.

import java.util.Scanner;
import java.util.Date;

public class myIDCard{
    public static void main(String[] args){
        long myID;
        long a,b,c,a1,a2,a3;
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter your Name: ");
        String name = in.nextLine();
        System.out.print("Enter your IC number without dash: ");
        myID = in.nextLong();
        in.nextLine();
        
        a = myID/1000000;
        b = myID/10000%100;
        c = myID%10000;
        a1 = a%100;
        a2 = a/100%100;
        a3 = a/10000;
        String[] months = {"Jan", "Feb", "Mac", "Apr", "May", "Jun", "Jly", "Aug", "Sep", "Oct", "Nov", "Dis"};
        String month = months[a2 - 1];
        System.out.println();
        System.out.println("Your name : " + name);
        System.out.println("Your IC number : " + a + "-" + b + "-" + c);
        System.out.println("Your Birth date : " + a1 + "/" + month + "/" + a3);
        
        if (c % 2 == 0)
            System.out.println("Your Gender is : Female");
        else
            System.out.println("Your Gender is : Male");
    }
}

it said that "incompatible types: possible lossy conversion from long to int". the error in my code was:-

String month = months[a2 - 1];
  • 2
    Cast it to `int`. – shmosel Mar 09 '22 at 02:56
  • (See *"Possible lossy conversion" when subscripting."* in the [duplink](https://stackoverflow.com/questions/51632152/what-does-possible-lossy-conversion-mean-and-how-do-i-fix-it).) – Stephen C Mar 09 '22 at 03:06
  • Neither of the answers to this question really explained *why* this is a lossy conversion. The reason is that array subscripts in Java are converted to `int` values. Converting a `long` expression to an `int` is lossy. – Stephen C Mar 09 '22 at 03:10

2 Answers2

2

A long uses more bits than an int, so it can hold larger numbers than an int. It has more digits so to speak. Just like you can express a larger number with 3 digits (0 to 999) than 2 digits (0 to 99), this is true for long compared to int except the digits are in base 2 instead of base 10. The digits are called bits.

A common source of programmer error is using a long where an int is requested. In some languages, the compiler will generate code that implicitly transforms the long into an int. If you're lucky, you might get a compiler warning. If you're luckier, the value in the long will fit in the int, so the logic of your program remains correct. If you're unlucky, you truncate some of the digits in your long, and you have a tough bug to find and fix.

Java has design goals to simplify things and prevent programmer error. The specification mandates that an automatic transformation cannot take place, turning a long into an int. Instead, the specification mandates that a compiler error happen.

If you're extra sure that the value in your long fits in your int, you can manually cast it from a long to an int like so: someInteger = (int) someLong;. In your case, it might look like String month = months[(int)a2 - 1];.

user904963
  • 1,520
  • 2
  • 15
  • 33
1

You need to cast it to int

String month = months[(int)a2 - 1];
VinuXD
  • 40
  • 9