• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

substring dynamic string

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have an output string in console in which this code appears several times with a different value. I would like to get dinamically the value without the n_id.


Any idea, please?
 
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that one String object or two?
Several ways to do it:
String#split()
new Scanner(myText).useDelimiter(...).next()
new Scanner(myText).next(regexForColonNumbers)

All those suggestions would use a regex for what you want to get rid of, or for what you want to keep. If you are certain that the length of the prefix you want to get rid of is always the same, try String#substring().
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact what I have is;



I need to get the "n_id"  value. I would like to use substrisng but it looks like brute force...
 
Campbell Ritchie
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How often does "n_id" appear in your text? Is that one String with embedded quotes or multiple Strings?
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that as it is a json result fro a rest call, I should convert it into a array...
 
Campbell Ritchie
Marshal
Posts: 80288
433
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can only help if we have all the necessary information.
 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks like JSON, so the obvious approach would be to use one of the many available JSON libraries. https://github.com/stleary/JSON-java would be a good choice.

Edit: I added linebreaks to the JSON so that the page is formatted properly, and also took the liberty of removing much of the duplicated data.
 
Marshal
Posts: 4700
588
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:I think that as it is a json result fro a rest call, I should convert it into a array...


What you have posted appears to be part of a larger JSON document, which looks mal-formed.  Right at the beginning, the [ character indicates an array is following, but then there rather than array values, there are key-value pairs, which would normally be part of an JSON object.  

Can you post a smaller and complete one to look at?

Changing the beginning array to an object, your data would look like this:
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the next code.



I get the error when using reader.beginArray();



I get the error when using reader.beginObject();




Any idea, please?
 
Ron McLeod
Marshal
Posts: 4700
588
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:I get the error when using reader.beginArray();


You didn't include the complete stack trace, but I bet that this exception was at line 11:
    String value = reader.nextString();
not line 8:
    reader.beginArray();


Isaac Ferguson wrote:Any idea, please?


After entering the array, you are trying to read a string, but the next value found was an object.

Looking at your document, it has a [ token which indicates the start of an array, followed by a { token which indicates the start of an object.  There is no string.

 
Ron McLeod
Marshal
Posts: 4700
588
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Isaac Ferguson wrote:I am using the next code.


What are you expecting that the code will do?
 
Ron McLeod
Marshal
Posts: 4700
588
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also - this object inside your document has a duplicate key - p1 is used twice.


If that was by-design, you might want to just have one object named p1 containing key-values pairs from both.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String value = reader.nextString();



Yes, after debugging the error is on this line. Could I read Objects instead to avoid the exception?

I am trying to read a .json file which contains the response of Rest calls in order to localize a literal in the retrieved text.
 
Did you just should on me? You should read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic