Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: precision error using long and int
    text
    copied!<p>(Still) a newbie in Java, trying to work with <code>long</code> and <code>int</code> values. I'm trying to deduct 1 from a long value and it keeps telling me there's a possible loss of error?</p> <p>I looked at other questions but all the ones I saw addressed either <code>float</code> precision errors or people did not know why they were using <code>long</code> instead of <code>int</code>. In my case, it is required in the question I'm given.</p> <pre><code> String bookingRequest = ""; int bookingElements = 4; String[] bookingDetails = new String[bookingElements]; long seatNo = 0; for(int x = 1; x &lt;= counter; x++) { bookingRequest = input.nextLine(); bookingDetails = bookingRequest.split(" "); seatNo = Long.parseLong(bookingDetails[0]); seatHolders[seatNo - 1] = bookingDetails[1] + " " + bookingDetails[2] + " " + bookingDetails[3]; } // A bit of code later long check = 0; for (long c = 1; c &lt;= seatChecker; c++) { check = input.nextLong(); //System.out.println(seatHolders[check - 1]); seatFinder[c - 1] = seatHolders[check - 1]; } </code></pre> <p>I do know that there is most probably a better way of writing this code, but as I have to use a <code>long</code> data type to take in the seat number (as according to the question), I only really need to know why it doesn't like minusing one from <code>long</code> values.</p> <p>The error:</p> <pre><code>Test.java:22: error: possible loss of precision seatHolders[seatNo - 1] = bookingDetails[1] + " " + book ingDetails[2] + " " + bookingDetails[3]; ^ required: int found: long Test.java:31: error: possible loss of precision seatFinder[c - 1] = seatHolders[check - 1]; ^ required: int found: long Test.java:31: error: possible loss of precision seatFinder[c - 1] = seatHolders[check - 1]; ^ required: int found: long 3 errors </code></pre> <p>Thanks in advance! :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload