0

String to array

How to String =“1230.5789”; To String[] list={“1”,”2”,”3”,”0.5”,”7”,”8”,”9”}

1st Apr 2020, 4:55 PM
hamid
hamid - avatar
3 Answers
0
hamid try this: import java.util.*; public class test { public static void main(String[] args) { String str ="1230.5789"; //String[] list={“1”,”2”,”3”,”0.5”,”7”,”8”,”9”}; List<String> list = new ArrayList<String>(); for(int i =0; i<str.length();i++){ if (i<str.length()-1){ if (str.charAt(i+1)!= '.') list.add(str.substring(i,i+1)); else { list.add(str.substring(i,i+3)); i=i+2; } } else list.add(str.substring(i,i+1)); } System.out.println(list); } }
1st Apr 2020, 6:33 PM
John Robotane
John Robotane - avatar
0
see output in my question
1st Apr 2020, 5:17 PM
hamid
hamid - avatar
- 1
Use loop to convert into character array by traversing each charecter of string. Or else use predefined function like String str="12340.567"; char array[] =str.toCharArray();
1st Apr 2020, 6:23 PM
Jayakrishna 🇮🇳