• 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

substring dynamic string  RSS feed

 
Ranch Hand
Posts: 1188
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • 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: 61690
192
  • Mark post as helpful
  • send pies
  • 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().
 
Isaac Ferguson
Ranch Hand
Posts: 1188
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • 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: 61690
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How often does "n_id" appear in your text? Is that one String with embedded quotes or multiple Strings?
 
Isaac Ferguson
Ranch Hand
Posts: 1188
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • 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: 61690
192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can only help if we have all the necessary information.
 
Saloon Keeper
Posts: 5031
134
  • Mark post as helpful
  • send pies
  • 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.
 
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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:
 
Isaac Ferguson
Ranch Hand
Posts: 1188
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 2150
278
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • 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.
 
Isaac Ferguson
Ranch Hand
Posts: 1188
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!