Finding data inside a file

  • 20
  • Locked
Read a file name from the console. Search the file for information related to the specified id. Display it in the format used in the file. The program is started with one argument: id (an int). Close the streams. The file data is separated by spaces and stored in the following order: id productName
You can't complete this task, because you're not signed in.
Comments (18)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Олег Байбула
Level 32 , Ukraine
Expert
20 February, 08:51
It doesn't accept args[0] as a search. It accepts args[0] + " ". The space identifies when the id ends. But the best way is to use splt() method, to get an array. The condition would be like:
String[] entities = line.split(" ");
                    if(Objects.equals(entities[0], args[0])){
                        System.out.println(line);
                        break;
                    }
Shamil
Level 19 , Kyiv, Ukraine
27 August 2022, 12:29
At first I didn't get what should I print. They want you to print whole line, including id, wich starts with "id" passed as first argument in main.
Mike S
Level 26 , Saint Louis, United States
10 February, 16:37
I don't know if I'm falling for the ol' "trailing spaces" trick or what, but I played with a test file that looked like this and it printed this which is what I feel like is the output they're looking for. Can anyone verify? id productName price quantity 1 doritos 3.99 10 1 fritos 4.99 5 1 nachos 1.99 11
Mike S
Level 26 , Saint Louis, United States
10 February, 16:38
Okay, based upon comments does it go something like this... File contains this: id productName price quantity 1 doritos 3.99 10 3 fritos 4.99 5 2 nachos 1.99 11 args[0] can be parsed as "1". So, we print the doritos line here?
Mike S
Level 26 , Saint Louis, United States
10 February, 16:55
For some other poor soul, I'll provide final clarity. If you have this kind of data in some file: id productName price quantity 1 doritos 3.99 10 3 fritos 4.99 5 2 nachos 1.99 11 And your args[0] returns 3, then your displayed output should be: 3 fritos 4.99 5
Enrique Del Valle Backend Developer
17 August 2022, 23:20
Hello community. What I am doing is the following : 1. I read a whole line from the file with BufferedReader(new InputStreamReader(fileInput)) 2. I Iterated the lines 3. For each line iterated, I ask the following if (line.contains(args[0])) 4. if it is true, I print the whole line. It fails the requirement, but I don't understand how the file is organized or the format to print the information. Could you give me a hint?
Justin Smith
Level 38 , Greenfield, USA, United States
5 October 2021, 01:06
Possible point of confusion. The directions say that the program has a single argument "an int". However, the args array in main is a String array. I think what they mean is that it's a string that can be parsed as an int?
Gellert Varga
Level 23 , Szekesfehervar, Hungary
5 October 2021, 17:54
Yes, it's a string that can be parsed as an int.
Jurij Thmsn
Level 29 , Flensburg, Germany
13 April 2021, 13:25
hint: the output should include the id.
Adrian
Level 27 , Spanish Town, Jamaica
22 August 2020, 04:33
I had a good laugh at myself! what i did for this was absolutely insane, i thought i had to get and parse the values (int, string double ) this led me down quite the road! i learnt a ton! and then, it suddenly dawned on me that all this task required was checking the id value and printing the line. enormously more simple than i ended up doing!
MoJo
Level 22 , Cairns, Australia
11 December 2020, 12:29
Same here lol. I think the task conditions are poorly worded. Maybe an intput and an output example would help.
Liliane Top Backend Developer at Procura
8 January 2021, 12:11
"The program should search the file and display information related to the specified id passed as the first argument." This is very hard to understand if English is not your native tongue. I also went way down the rabbit hole ... haha
Petros
Level 23 , Columbia, United States
22 August 2020, 04:01
I brushed over where it said "productName is a String — it can contain spaces". That messed me up for a while. A good reminder to read the requirements carefully.
Artur Verdyan
Level 24 , Hoboken, United States
8 June 2020, 16:28
Sometimes I feel that maybe I am missing something. Is there somewhere a file which for each task which I need to use ? So far I just created it myself
Sven
Level 19 , Hanover, Germany
4 October 2019, 20:33
It does work with the testfile, but the verification fails anyways. Any suggestions as to what the problem is?
Roman
Level 41
7 October 2019, 06:10
If you need help, something isn't right in your code, the server won't accept your solution (even if you are 100% sure that it is correct). Describe your question/issue in the HELP section at codegym.cc/help.
Julián Orellana
Level 20 , Guayaquil, Ecuador
15 October 2018, 17:50
wich is the byte that says a separate line 10 or 13?
Roman
Level 41
16 October 2018, 06:07
Try to use the universal solution :)