Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8568125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:02:19+00:00 2026-06-11T18:02:19+00:00

I want to parse a given string into vectors. A vector starts with [

  • 0

I want to “parse” a given string into vectors. A vector starts with “[” and ends with “]”. The values of the vector as well as the vectors themself are seperated by “,”. If I use integers as values my code works fine “[1,2,3],[5,2,3],[1,6,3]”. But when I mix integer values with double values “[1,2.5,3],[5,2,3],[1,6,3]” stringtokenizer returns wrong values (in this case “1” “2.5” but then “3]” ……)

String s = "[1,2.5,3],[5,2,3],[1,6,3]";

Vector<Vector<Double>> matrix = new Vector<Vector<Double>>();        
for(int j=0;j<s.length();j++) {
   if (s.charAt(j)=='[') {
      int k=s.indexOf("]");      
      StringTokenizer st = new StringTokenizer(s.substring(j+1, j+k));// j+k-1 does not work either
      Vector<Double> vector = new Vector<Double>();
      while (st.hasMoreTokens()) {
          vector.add(Double.parseDouble(st.nextToken(",")));//Exception in thread     "main" java.lang.NumberFormatException: For input string: "3]"
      }
      matrix.add(vector);
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:02:20+00:00Added an answer on June 11, 2026 at 6:02 pm
    if (s.charAt(j)=='[') {
        int k=s.indexOf("]"); 
    

    Finds the index of the first occurrence of ] in s, starting from the beginning of the string. What you really want is to find the first occurrence after the start of current vector:

    if (s.charAt(j)=='[') {
        int k = s.indexOf("]", j);
    

    The reason it works when you just have 2 instead of 2.5 is that the number of characters in each vector just happened to be the same, so taking the fist occurrence of ] to calculate the length of the vector worked by luck.

    Note, you will also have to change the end index of your substring call to k:

    StringTokenizer st = new StringTokenizer(s.substring(j+1, k));
    

    As a side note, use of StringTokenizer is not recommended. In this case you should be using split() instead:

    String[] elements = s.substring(j+1, k).split(",");
    Vector<Double> vector = new Vector<Double>();
    for (String element : elements) {
        vector.add(Double.parseDouble(element));
    }
    matrix.add(vector);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to tokenize a given mathematical expression into a parse tree like this:
I want to convert a string of playing cards given as in A into
I want to parse a SQL query into an array. I could not figure
I want to convert a list object into a string and insert this string
I have parsed a given CSS file/string into a JSON object like so: {
Suppose I have String input = 1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,2,3,0,4,0,0,0,4,0,3; I want to encode it into a
I want to parse a JSON string: MyJsonString: { status: ok, count: 2, count_total:
I've got some strings that I want to parse into a list of chunks.
I've made a little forum and I want parse the date on newest posts
I want to parse something like this: Hi [{tagname:content}] [{tag1:xnkudfdhkfujhkdjki diidfo now nested tag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.