Results 1 to 3 of 3
  1. #1
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default 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.
    Java 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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:

    Java Code:
     
    if (myStringVar == "foo") {
       // do something
    }
    becomes:

    Java Code:
     
    if (myStringVar.equals("foo")) {
       // do something
    }
    or:
    Java Code:
     
    if (myStringVar.equalsIgnoreCase("foo")) {
       // do something
    }

  3. #3
    Mokomi is offline Member
    Join Date
    May 2011
    Posts
    32
    Rep Power
    0

    Default 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)

Similar Threads

  1. sound editing
    By nikkka in forum Advanced Java
    Replies: 3
    Last Post: 04-14-2011, 02:34 PM
  2. Problem with editing jar
    By DanielR in forum New To Java
    Replies: 16
    Last Post: 02-11-2011, 02:15 AM
  3. FileWriter editing hosts file Mac
    By danborgir in forum New To Java
    Replies: 2
    Last Post: 01-12-2011, 11:23 AM
  4. Photo Editing
    By stekun in forum Advanced Java
    Replies: 3
    Last Post: 02-23-2010, 05:11 AM
  5. Text editing
    By right2001 in forum New To Java
    Replies: 1
    Last Post: 04-10-2009, 07:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •