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
Finding data inside a file
- 20
Locked
Comments (18)
- Popular
- New
- Old
You must be signed in to leave a comment
Олег Байбула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:
0
Shamil
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.
0
Mike S
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
0
Mike S
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?
0
Mike S
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
0
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?
0
Justin Smith
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?
0
Gellert Varga
5 October 2021, 17:54
Yes, it's a string that can be parsed as an int.
0
Jurij Thmsn
13 April 2021, 13:25
hint: the output should include the id.
0
Adrian
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!
+2
MoJo
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.
+2
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
0
Petros
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.
0
Artur Verdyan
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
0
Sven
4 October 2019, 20:33
It does work with the testfile, but the verification fails anyways. Any suggestions as to what the problem is?
0
Roman
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.
0
Julián Orellana
15 October 2018, 17:50
wich is the byte that says a separate line
10 or 13?
0
Roman
16 October 2018, 06:07
Try to use the universal solution :)
0