Can someone tell me why this if(string1.equals(string2)) instance isn't working properly? (Trying to check for file extensions)
String extension = ".txt";
if (name1.substring(name1.length()-4).equals(extension))
name1 += ".txt";
System.out.println("Enter a second file name: ");
String name2 = console.nextLine();
if (name2.substring(name2.length()-4).equals(extension))
name2 += ".txt";
Ninja Edit: It is working, so now the question is how can I say is not equal to?
edit again: I just changed it to
String extension = ".txt";
if (name1.substring(name1.length()-4).equals(extension) != true)
name1 += ".txt";
System.out.println("Enter a second file name: ");
String name2 = console.nextLine();
if (name2.substring(name2.length()-4).equals(extension) != true)
name2 += ".txt";
Here's my fully revised code, the next morning:
//This next section just adds .txt if it is not present, skip down to next comment
String extension = ".txt";
if (!name1.endsWith(extension))
name1 += ".txt";
if (!name2.endsWith(extension))
name2 += ".txt";
//end .txt check
just in case anyone was wondering
To sum it up: It was working, I just didn't think about what I was actually comparing. I had to make it not equal instead of equals.
Posted Dec 2, 2011, 1:11 AM UTC · edited
22