• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

Reading a file and creating a string

 
Greenhorn
Posts: 13
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day,

I am trying to read a file but when an open parenthesis is encountered, I need it to keep reading and add all of the numbers into an array until the closing parenthesis. I am a little stuck here and could really use some help. Right now the file would read the input "Numbers (100, 100)" as

Numbers
(100,
100)

but when my scanner encounters the '(', I need it to create a string from '(' to ')' keep going read each number into my array.
Here is what I have so far:
 
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more.
What are you going to do with the text before the (?
How are the numbers separated?
What are you going to do with the text after the )?
Have you gone through the documentation for Scanner recently? I think it will give you hints about how you can get all the text between the () as one token.

I am afraid you aren't opening the Scanner nor closing it as I would expect in post‑2009 programming. You should use try with resources, which ensures the file is closed (I can see no sign of your closing it), and I would prefer to use a Path, not a File:-Don't use while (scan.hasNextLine())....   Use while (scan.hasNext())..., which won't cause problems if there is an empty line at the end of the file.
 
Pat Watson
Greenhorn
Posts: 13
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please explain more.
What are you going to do with the text before the (?
How are the numbers separated?
What are you going to do with the text after the )?
Have you gone through the documentation for Scanner recently? I think it will give you hints about how you can get all the text between the () as one token.

I am afraid you aren't opening the Scanner nor closing it as I would expect in post‑2009 programming. You should use try with resources, which ensures the file is closed (I can see no sign of your closing it), and I would prefer to use a Path, not a File:-Don't use while (scan.hasNextLine())....   Use while (scan.hasNext())..., which won't cause problems if there is an empty line at the end of the file.



Hello Campbell,
I read the scanner document again but I am still not understanding how to capture what I am looking for
The first word from the line will determine what happens next. My issue is that I am using the split(" ") but when the reader encounters '(', I need it to ignore the space after the comma and save each number into my array of numbers so for example: with the input file numbers (1, 2, 3, 4, 5), with the delimiter being a whitespace " ", the output will be
number
(1,
2,
3,
4,
5)

and this is my problem. I need the reader to keep reading from ( and if it encounters a digit, place that into an array. I wanted to use a regular expression but even that is not helping me either.
Thank you
 
Saloon Keeper
Posts: 4168
160
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not replace all ", " by a "," in a line, and then do a split on a space? Then one of the strings will be "(n1,n2,n3,...)" from which it is easy to derive the individual numbers. Be careful about other commas that may be present in a line.
 
Pat Watson
Greenhorn
Posts: 13
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Why not replace all ", " by a "," in a line, and then do a split on a space? Then one of the strings will be "(n1,n2,n3,...)" from which it is easy to derive the individual numbers. Be careful about other commas that may be present in a line.



Thank you very much Piet. I don't know why I did not think about that before. I really appreciate it.  
 
Campbell Ritchie
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why, Piet? You can easily split on ",\\s*"
You can change the delimiter for the Scanner and use "(" (or more likely "\\(") to split off everything before the first (.
 
Pat Watson
Greenhorn
Posts: 13
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why, Piet? You can easily split on ",\\s*"
You can change the delimiter for the Scanner and use "(" (or more likely "\\(") to split off everything before the first (.



Hello Campbell,
Can you explain that regular expression? I think I am lost
 
Campbell Ritchie
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find yourself a regex tutorial; there is a nice little section in the Java™ Tutorials.
The first one means comma followed by any number (maybe zero) of whitespace characters. If you go through that tutorial you will find that \s is a predefined token for whitespace, but you have to double up the escape when you write is as a String literal.
The second means L parenthesis; I think that is a metacharacter (see tutorial: a comma isn't a metacharacter), in which case it will probably need to be escaped.
Do you have more than one pair of (...) in your file?
 
Campbell Ritchie
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an alternative to \s, find out what the default delimiter for a Scanner is (it has a method to get that). Change the + at its end to * and you can use that as the remainder of the regex after the comma instead of \\s* You probably have to double up escape characters (\→\\).
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic