editing a file. Am I doing this right?
I'm currently helping make a game with my friends and we have character sheets.
each .txt files contains information about the player.
right now i'm trying to get it to search the inventory.
I'm trying to read the scanner has next. If the scanner == a String ("InventoryStart") It should start at the inventory slots. for whatever reason it is not bothering to look at the IF statment. Code:
import java.io.*;
import java.util.*;
public class bucky {
BufferedReader in;
boolean testings = false;
String read;
// Number of spaces till you reach inventory
// int inv=2;
String reads="inventoryStart";
//the specific line i want to read
int counter =0;
public bucky(){
try {
in = new BufferedReader(new FileReader("zren.txt"));
//ignore thsi
// PrintWriter in= new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
while(testings=false||counter<10){
//this is important
//read the next line until the specific line is found
read = in.readLine();
//this is what is read
System.out.println(read);
if(read=="inventoryStart"){
System.out.println("Success!!! "+read);
testings=true;}
else{
counter ++;
System.out.println("numbers of failed tries: " +counter);
}
}
counter=0;
//ignore this
//while(counter <= 3){
// read = in.readLine();
// System.out.println("file output: " + read);
// counter++;
// }
//
// in.close();
//
}catch(IOException e){
System.out.println("There was a problem:" + e);
}
File test = new File();
//test.happy();
}
//yes i know this is on the bottom. this is for testing reasons
public static void main(String[] args){
//ignore this as well
bucky File = new bucky();
// testing hi = new testing();
// hi.readFile();
// hi.writeFile();
}
}
zren.txt doc
Name: Zren K Tas
Race: Human
inventoryStart
1 Empty
2 Empty
3 Empty
4 Empty
5 Empty
6 Empty
7 Empty
8 Empty
9 Empty
10 Empty
results (what the line reads then if it passed or failed)
Name: Zren K Tas
numbers of failed tries: 1
Race: Human
numbers of failed tries: 2
inventoryStart
numbers of failed tries: 3
1 Empty
numbers of failed tries: 4
2 Empty
numbers of failed tries: 5
3 Empty
numbers of failed tries: 6
4 Empty
numbers of failed tries: 7
5 Empty
numbers of failed tries: 8
6 Empty
numbers of failed tries: 9
7 Empty
numbers of failed tries: 10
when it hits inventoryStart the IF statement should conclude as true. but this is not the case.
Re: editing a file. Am I doing this right?
For starters, never use == to test Strings for equality. The equality operator, ==, tests if two String variables refer to the same String object, and this is not what's important to you, and in fact is misleading. Instead you want to know if the two Strings contain the same characters in the same order, and for that use the String method equals(...) or equalsIgnoreCase(...).
So this code:
Code:
if (myStringVar == "foo") {
// do something
}
becomes:
Code:
if (myStringVar.equals("foo")) {
// do something
}
or:
Code:
if (myStringVar.equalsIgnoreCase("foo")) {
// do something
}
Re: editing a file. Am I doing this right?
Yep .equals(".foo") works wonders
now on to checking and editing the files. wish me luck!
(.equalsIgnoreCase is to ingore lower and upper case. not going to use it here but Really good to know for future searching reasons thank you for going the extra mile)