Home > Enterprise >  not able to print the value one by one when iterating through for loop
not able to print the value one by one when iterating through for loop

Time:10-26

public void verifyPattenData() {            
    beans = new HealthDataBeans();

    for (int i = 0; i < 3; i  ) {                      
        System.out.println("aaaaaa"  
            ConfigurationManager.getBundle().getProperty("value").toString()
        );
    }
}

getting Output like- [350,218,344]
data type is - string

But I need to print the value in the below format -

350
218
344

please help me on this if anyone can provide the solution

CodePudding user response:

  1. Remove brackets and split the string by comma and/or spaces getting the array of String, print each element as needed:
String output = ConfigurationManager.getBundle().getProperty("value").toString();
System.out.println("aaaaaa: "   String.join("\n", 
    output.replaceAll("^\\[|\\]$", "") // remove brackets
          .split(",\\s*")              // get array of strings
));
  1. Use replaceAll to replace commas followed by spaces with "\n":
System.out.println("aaaaaa: "   output
    .replaceAll("^\\[|\\]$", "")
    .replaceAll(",\\s*", "\n")
);

Output in both cases:

aaaaaa: 350
218
344

CodePudding user response:

Try the following:

public void verifyPattenData() {            
    beans = new HealthDataBeans();

    List<String> values = ConfigurationManager.getBundle().getProperty("value");
    for (int i = 0; i < values.size(); i  ) {                      
        System.out.println(values.get(i));
    }
}
  • Related