Reputation: 133
I have a text file that looks something like this:
p : 10001 : Unity
c : 20007 : Witch : Morgana: 10001 : 10 : 15 : 0
t : 30001 : Gold : 20004 : 50 : 2000
a : 40002 : Wand : 20006
I want to use ":" as a delimiter and do things with each line according to what the first char is.
try {
Scanner scanner = new Scanner(chooser.getSelectedFile()).useDelimiter(":");
int index;
String type;
String name;
String identifier = scanner.next();
if (identifier == "p") {
index = scanner.nextInt();
name = scanner.next();
partyList.add(new Party(index, name));
} else if (identifier == "c") {
index = scanner.nextInt();
type = scanner.next();
name = scanner.next();
int partyC = scanner.nextInt();
int empathyC = scanner.nextInt();
double carryingCapacityC = scanner.nextDouble();
creatureList.add(new Creature(index, type, name, partyC, empathyC,carryingCapacityC));
} else if (identifier == "t") {
index = scanner.nextInt();
type = scanner.next();
int creatureT = scanner.nextInt();
double weightT = scanner.nextDouble();
int valueT = scanner.nextInt();
treasureList.add(new Treasure(index, type, creatureT, weightT, valueT));
} else if (identifier == "a") {
index = scanner.nextInt();
type = scanner.next();
int creatureA = scanner.nextInt();
artifactList.add(new Artifact(index, type, creatureA));
} else {
System.out.println("This is not a valid line of input");
}
System.out.println("Identifier: " + identifier);
} catch (IOException e) {
e.printStackTrace();
}
This shouldn't be too difficult... However, when I run the program I get something like this:
You chose to open this file: testFileSC.txt
This is not a valid line of input
Identifier: p
This is not a valid line of input
Identifier: 10002
//etc. (you get the point) all the way through my text file.
So it's reading the file okay, but getting stuck on the first char. Furthermore making everything an 'identifier'. Any ideas? I've been staring at this for way too long!
Upvotes: 0
Views: 299
Reputation: 5306
As others have pointed out, you need to use .equals()
instead of ==
to check the strings. Another thing is that you're using ":"
as a delimiter which means that the tokens will include spaces. For example, the first identifier would be "p "
instead of "p"
. You can use the .trim()
method to remove any trailing spaces and then use .equals()
for comparison like this:
if (identifier.trim().equals("p")) {
You also need to put your code in a loop to read the entire file. You could enclose it in a while loop until your scanner
throws a NoSuchElementException
which will indicate that you've read the entire file.
Reference here.
Upvotes: 2
Reputation: 3685
You cannot use == to compare Strings in Java (not in the way you intended).
Upvotes: 1
Reputation: 491
Use "p".equals(identifier)
instead of identifier == "p"
.
http://www.java-samples.com/showtutorial.php?tutorialid=221
Upvotes: 2
Reputation: 11543
You should not compare strings using ==, but instead use .equals() such as
"wantedvalue".equals(variable)
or
variable1.equals(variable2)
The equals methods test if the are equal in the sense that they look the same while == checks if they are the actual same object. Compare with taking two equal pen. They look the same (equals) but they are not the same pen (==).
Upvotes: 2
Reputation: 24411
Identifier is a String. You cannot use == to check the value of a string. == will check to see if the two objects are the same, which they are not. One object is a string variable; the other object is a constant. Using the .equals()
method will check the value of the String.
if( identifier.equals("p"))
...
If you want to cover the circumstance in which whitespaces may be included, you can also do:
if( identifier.startsWith("p"))
Upvotes: 3