Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing String Integers Issue
    primarykey
    data
    text
    <p>I have a scanner that reads a 7 character alphanumeric code (inputted by the user). the String variable is called "code". </p> <p>The last character of the code (7th character, 6th index) MUST BE NUMERIC, while the rest may be either numeric or alphabetical. </p> <p>So, I sought ought to make a catch, which would stop the rest of the method from executing if the last character in the code was anything but a number (from 0 - 9).</p> <p>However, my code does not work as expected, seeing as even if my code ends in an integer between 0 and 9, the if statement will be met, and print out "last character in code is non-numerical).</p> <p>example code: 45m4av7</p> <p>CharacterAtEnd prints out as the string character 7, as it should. however my program still tells me my code ends non-numerically. I'm aware that my number values are string characters, but it shouldnt matter, should it? also I apparently cannot compare actual integer values with an "|", which is mainly why im using String.valueOf, and taking the string characters of 0-9.</p> <pre><code>String characterAtEnd = String.valueOf(code.charAt(code.length()-1)); System.out.println(characterAtEnd); if(!characterAtEnd.equals(String.valueOf(0|1|2|3|4|5|6|7|8|9))){ System.out.println("INVALID CRC CODE: last character in code in non-numerical."); System.exit(0); </code></pre> <p>I cannot for the life of me, figure out why my program is telling me my code (that has a 7 at the end) ends non-numerically. It should skip the if statement and continue on. right?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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