1

i got this code below to do replacing of a field that can be either String or Int but it is not working as expected. What i would like to do is inside the file player.dat the fields are "Username|Password|No. of Chips" i would like to replace the 3rd field based on username and i used string replace to change the old string to the new string but it is not working it is just adding the number next to the old value and all my data in file is messed up from the original line by line. Need some help on this

Current Output:

Username1|HashedPassword|200200Username2|HashedPassword2|300200Username3|HashedPassword3|300200

Expected Output

Username1|HashedPassword|200
Username2|HashedPassword|300
Username3|HashedPassword|200 

Codes Shown Below.

 public class testcode {
   public static void main(String[] args) {
     new testcode().replace();
   }

   public void replace() {
      String oldFileName = "players.dat";
      String tmpFileName = "tempmod.dat";

      BufferedReader br = null;
      BufferedWriter bw = null;
      ArrayList<String> player = new ArrayList<String>();
      try {
         br = new BufferedReader(new FileReader(oldFileName));
         bw = new BufferedWriter(new FileWriter(tmpFileName));
         String line;
         Scanner read = new Scanner(System.in);
         System.out.println("Please Enter Username");
         String UserN = read.nextLine();
         System.out.println("Please Enter Chips to Add");
         String UserCadd = read.nextLine();

         while ((line = br.readLine()) != null) {
                String[] details = line.split("\\|");
                String Username = details[0];
                String Password = details[1];
                String Chips = details[2];
                int totalChips = Integer.parseInt(UserCadd) + Integer.parseInt(Chips);
            if (Username.equals(UserN))
               line = Username + "|" + Password + "|" +totalChips;
            bw.write("\r\n"+line);
         }
      } catch (Exception e) {
         return;
      } finally {
         try {
            if(br != null)
               br.close();
         } catch (IOException e) {
            //
         }
         try {
            if(bw != null)
               bw.close();
         } catch (IOException e) {
            //
         }
      }

   }
}
Linux Newbie
  • 252
  • 2
  • 7
  • So your actual problem is that the adding of the integer value is not working? Have a look at this answer to learn how to convert a string to a integer: http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java You need to do that before the add operation. – Nitram Apr 10 '15 at 09:16
  • Open the .dat file in other software like `notepad++`.. in some editors `\n` does not work for .dat file – AJ. Apr 10 '15 at 09:27
  • http://stackoverflow.com/questions/8148004/ignoring-end-of-line-character-in-a-dat-file-in-db2-import-command-while-loading – AJ. Apr 10 '15 at 09:29

2 Answers2

1

You can create it manually:

Integer totalChips = (Integer.parseInt(UserCadd) + Integer.parseInt(Chips));
line = Username+"|"+Password+"|"+totalChips;
FuRioN
  • 623
  • 5
  • 12
0

String totalChips = UserCadd + Chips;

This is a String concatenation, rather than a numerical addition, so you end up with 200200.

I think you want to add the actual numbers instead before your replace the old value. Have a look at Integer.parseInt()

user1717259
  • 2,717
  • 6
  • 30
  • 44