• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Update Text file by ID.

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need help reading a text file and updating data in it. I'm creating an Inventory program and the user has to be able to search product by ID and update the stock level. The text file looks like this :
702111 plant 2020-09-08 10 2022 5.0 kg 50.0 100.0 5
261623 der 1989-09-08 45 45.0 kg 45.0 46.0 1
248559 der 2003-04-02 20 34.0 kg 34.0 67.0 1

The last digits are the stock level (5,1,1). As said I need to ask user for the product ID he wants to edit and update the stock level. Thanks. Here is the code I'm trying to work with :


but I'm getting an error after entering my ID.
 
Marshal
Posts: 72423
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us the full details; we can only help if we know the information.
I can, I am afraid, see all sorts of problems with your code.
The most serious error I can see is closing a Scanner reading System.in. That closes System.in and you can never reopen it.
Move all your code out of the main() method. Reading and writing should be separate activities and should therefore have methods of their own.
I am not happy about the notion of searching a text file. Remember you have to do a linear search through the text file takes time, and you cannot “edit” a text file as you could a random access file. I would read the entire file into a class representing the record (but don't call it Record), and use a Map with the ID as its “K”. Then you only need to search the Map, which is much faster than searching the file, and you  only need to write the file once. You can't write part of a text file; you can write the whole of it, or you can append lines to it, so that is going to be a slow operation.
If the file is too large to handle in RAM memory, consider a database instead and let the database do the searching. Databases can do such searches very quickly.
Use the new (new? It is twelve years old) way to close your readers and writers: as shown in the Java™ Tutorials.
Why are you using hasNextLine() and nextLine() in lines 21, 23, and 27? You are creating an infinite loop. And you are better off using next() rather than nextLine().
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. To be honest I really never used Hash map as I’m really new to this. This is just a project so of won’t be handling large files. If anyone can help me a bit with the coding and explain me through it I really appreciate . Thanks
 
Master Rancher
Posts: 4251
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm getting an error


Please copy the full text of the error message and paste it here. It has important info about the error.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enter id of the record you want to edit?
248559
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.valueOf(Integer.java:983)
at StockUpdate.main(StockUpdate.java:25)
But if the coding is bad too I'd appreciate some help in the right direction.
 
Saloon Keeper
Posts: 8005
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put a print in to see what "currentLine" actually holds.

I suspect the line begins with a space.
 
Norm Radder
Master Rancher
Posts: 4251
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the Arrays class's toString method to show what is in an array:
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm lost here I just can't retrieve the row by ID and replace the last character with a new updated one
 
Carey Brown
Saloon Keeper
Posts: 8005
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To update any part of the file you have to re-write the entire file.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this code which I'm using to delete a row using the ID but I don't know how to modify it and update the data to that particular row with the searched ID :


 
Carey Brown
Saloon Keeper
Posts: 8005
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest adding a class to hold  a Record and then a couple of methods using those Records.

public List<Record> readRecords( String fileName ) { ... }

public void writeRecords( String fileName, List<Record> records ) { ... }

In between those two you can search the List and edit the Records that you want.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you help me out a bit with the coding as I'm fairly new to this :S thanks
 
Carey Brown
Saloon Keeper
Posts: 8005
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 8005
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are reading a line of text and looking for an ID. The code you wrote will find an ID anywhere in the line, not necessarily at the correct token position. Make a class Record and use that, a bit of extra effort but it will keep you from tripping over yourself.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes but the ID is a unique generated number so there won't be any duplicatesin the file
 
Carey Brown
Saloon Keeper
Posts: 8005
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
702111 plant 2020-09-08 10 2022 5.0 kg 50.0 100.0 5
261623 der 1989-09-08 45 45.0 kg 45.0 46.0 1
248559 der 2003-04-02 20 34.0 kg 34.0 67.0 1

You mean that there can't be a quirk of fate looking for an ID of 2022 that returns the first record?
 
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Put a print in to see what "currentLine" actually holds.

I suspect the line begins with a space.


It is the old story about the Scanner class. See lines 20 and 23: currentline will be the empty string, and so the split will result in token[0] being the empty String, that will give the error if you try to Integer.parseInt it.

A remedy could be to have a boolean "updating" being true, and then

But all this is not very handy. A database would be ideal, but a HashMap<id, number> would also work. You can do all the updating in this Map, and once finished, write the Map to a file.
So have a look at the API of a HashMap and see if you can apply it.

PS: I did not see all the posts from the last minutes, so my reply may be superfluous.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to work with this : while( ( record = br.readLine() ) != null ) {
       
       
      if( record.contains(ID) )
      {
          // modify record
      }

      bw.write(record);
      bw.flush();
      bw.newLine();

  }

Till now I have printed the line contents and I'm trying to update the stock number
 
Piet Souris
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But what if you have more than one update?
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This project just requires us to do basic stuff. we're just going with one for now but I'm still stuck. If you can guide me with a code and explain I learn something new hopefully. I just managed to print the line with the searched ID
 
Piet Souris
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, your latest code-snippet looks okay, but there is still the problem that Carey described. By using 'line.contains' you are looking for that ID at any place. Instead, you should first determine the ID of the line. Now, have a look at the method 'indexOf' of a String. The line-ID is the part of the line from start to the index of the first space. And there is also a method 'lastIndexOf', that you need to replace the last part of a line by the new number. You can find more details in the API of the String class (String API)

If you get it running, let us know. We can then see if there are better solutions.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try {
   String lineText = null;

   while ((lineText = br.readLine()) != null) {
   
   
    if( lineText.contains(ID) ) {
   
    int index = lineText.lastIndexOf("2");
    System.out.println(index);
   
   
   
    }
   }
   
   bw.close();
   br.close();
} catch (IOException ex) {
   System.err.println(ex);
}
}
}

I managed to find the index of the last number at least
Now I'll try to update it. Any help? thanks
 
Carey Brown
Saloon Keeper
Posts: 8005
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your original approach of splitting the string on a space is a good one, then you should only need to search one of the elements for the ID and only need to modify one element. I think there's some sort of String join() method that you can use to put the array back together before writing it back out.
 
Piet Souris
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by  // change the last part of lineText to the new number?
 
Piet Souris
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
lineText contains the old number, and before you write lineText, you need to replace that old number with the new number
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if( lineText.startsWith(ID) ) {
   
    int index = lineText.indexOf("2");
   
    lineText.contains(index);
    line
   
   
    bw.write(lineText);
   
    }
   }
   
I'm trying but I'm very bad at this :S @Bartender
 
Piet Souris
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just thought of a possible problem:

If a line has ID = 12345 and you look for ID = 123, then both 'contains' and 'startsWith' will select that wrong line.
So it is better to find the index of the first space to determine the ID of the line.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show me how please and I'll try to understand it please.
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the .contains ID I understood why. I just can't figure out how to replace the last value. The IDs will al be the same length as they are auto generated.
 
Piet Souris
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the number comes directly after the last space. Let lastIndexOfSpace = lineText.lastIndexOf(" ");
Then you get the updated lineText as follows:
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Now it's returning Product Id to update stock:
361335
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null
at java.base/java.io.Writer.write(Writer.java:249)
at StockUpdate.main(StockUpdate.java:48)
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I managed to solve that but now my inventory txt file everything is being deleted and one;y left with  2 3 only
 
Piet Souris
Saloon Keeper
Posts: 4355
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you have

Should be
 
Warren Griscti
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks it worked perfectly 🌝 I’ll play around with it tomorrow and understand it a bit better
 
Carey Brown
Saloon Keeper
Posts: 8005
70
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your original code was more on track, it just needed some work.
You needed to open the files in a try-with-resources block so that the resources will get closed automatically when the block is done.
Your splitting of lines into fields was the right thing to do IMO, you just needed to update the quantity and put the fields back together.
Then you ALWAYS had to write a line out, whether it was an unchanged one or a modified one.

 
Good night. Drive safely. Here's a tiny ad for the road:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic