e have a date form that can accept 2 or 4 digit years. I am not trying to code for exceptions…just instances where the year is either 2 or 4 digits long. I’m using lastIndexOf to find the last instance of “/”, and if the third character after the slash is numeric, I assume a 4-digit year. Otherwise it’s a two digit year.
I’ve already tested and validated that this is the only line giving me an issue right now,
if (inputLine.lastIndexOf.isNumeric("/"))+3 {
year = inputLine.trim().substring(inputLine.lastIndexOf("/")+1,input.lastIndexOf("/")+4).trim);
else year = "0000";
I keep getting compilation errors. For the lines in which I’m adding to the index Value, I’ve got a bad operand for the binary operator. Additionally, the variable lastIndexOf can’t be found.
I’m calling the list below at the head of the program, and as far as I can tell from the java documentation, io and lang pull in the appropriate methods and classes.
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.lang.*;
For the if statement, although my parens are balanced, I don’t know if adding the additional characters (e.g., index value + 3) is being done within the right level of the nested parens. That being said, I’ve tried top add those integers pretty much to no avail.
if (inputLine.lastIndexOf.isNumeric("/"))+3 {
year = inputLine.trim().substring(inputLine.lastIndexOf("/")+1,input.lastIndexOf("/")+4).trim);
else year = "00/00/0000";
I’m wondering a couple thigs: First, can I do this in a single statement, or is it better to define the index in one statement, then define index+offset-value in a subsequent statement?
When I’m using multiple methods and classes operating on a single field (e.g., .trim().substring.Indexof() etc., what is the order in which Java parses those? I’d like to undestand how these statements are being parsed so I’ve got a better understanding of the best way to manipulate the variables and test the output.
You are using the +3 incorrectly. You’re putting it outside of the if(condition) statement, somewhere where it doesn’t make any syntactic sense – it’s gobbledygook in your code.
Also, if you’re trying to test if the result of inputline.lastIndexOf(‘/’) is numeric, it won’t mean anything. If the specified character does not occur, lastIndexOf won’t throw an exception; it will just return -1. You want to test if the result is greater than/equal to zero.