-4

I am trying to achieve below output

A1 = 123 then in next line A2= 456 and A3 = 678

But when I run this, it's going in a loop and getting 10 times. I just need these 3 lines like A1 ,A2 and A3. Length can change based on demand. Below is my code. Please suggest a solution.

public class SusbTesting {


    public static void main (String args[]){

          String text = "123456789"; //The text

          int len = text.length(); //Get the length
         // text = text.substring(0,4)+ "_"+ text.substring(4,6) + "-" + text.substring(6);
        System.out.println("testing test >>>" +text);
         for (int k = 0; k < text.length(); k++) 
         {
             String A1  =   text.substring(0,2);
             String A2  =   text.substring(3,6);
             String A3  =   text.substring(7,9);
             text = A1+ "_"+ A2 + "-" + A3;
             System.out.println("testing  >>>" +text);
         }


    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Abby
  • 1
  • 3
  • 2
    please add the code to the question, and format it properly. As you can see it's very difficult to read it from the comments section. – Nir Alfasi Oct 25 '17 at 19:45
  • it was hard to put code in main area so added in comment . i am trying to acheive output something like this A1 = 123 then in next line A2 = 456 and next line should be 678 but its not coming pls help – Abby Oct 25 '17 at 19:45
  • 2
    Welcome to Stackoverflow, please read [How To Ask](https://stackoverflow.com/help/how-to-ask). Pay special attention to [How To Create MCVE](https://stackoverflow.com/help/mcve). The more effort you'll put into posting a good question: one which is easy to read, understand and which is [on topic](https://stackoverflow.com/help/on-topic) - the chances are higher that it will attract the relevant people and you'll get help even faster. Good luck! – Nir Alfasi Oct 25 '17 at 19:45
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Oct 25 '17 at 19:49
  • 3
    If you don't want a loop, then don't use a loop. Your code simply repeats `text` length times the same code. If that is not what you want, then don't program that. – Mark Rotteveel Oct 25 '17 at 19:50
  • its a java standalone program .i am using eclipse – Abby Oct 25 '17 at 19:51
  • i want to save the values something like this H1 text.substring(0,2) H2 text.substring(3,6) H3 text.substring(7,12) for that big string and then process it like H1 , H2 and H3 – Abby Oct 25 '17 at 19:58
  • But why do you need loop for that? What do you think that loop should do? What happens instead? – Pshemo Oct 25 '17 at 20:24
  • i am still using loop , i need loop for one more logic but i found the issue . thank you so much everyone for your help and time – Abby Oct 25 '17 at 20:29

2 Answers2

0

Try not using a loop:

public class SusbTesting {
   public static void main(String [] args) {
      String text = "123456789";
      String a1 = text.substring(0, 3), a2 = text.substring(3, 6), a3 = 
      text.substring(6, 9);
      System.out.println(a1);
      System.out.println(a2);
      System.out.println(a3);
   }
}
Bimde
  • 722
  • 8
  • 20
  • String ph = StringFunctions.NVL(AttributeHelper.getAttributeValue(thisItem,"ATTRIBUTE VALUE Data"),"",true); String P1 = ph .substring(0, 2); String P2 = ph .substring(3, 6); – Abby Oct 25 '17 at 20:12
  • Accept the answer as accepted if it helped you in any way :) – Bimde Oct 26 '17 at 01:51
0
String text = "123456789"; //The text

        int len = text.length(); //Get the length

        String A1 = text.substring(0,3);
        String A2 = text.substring(3,6);
        String A3 = text.substring(6,9);
        System.out.println(A1);
        System.out.println(A2);
        System.out.println(A3);

Try this. I don't think you need a loop for that.

Reynold Bhatia
  • 139
  • 1
  • 7