Parsing an array to String Tokeniser - java

I have an object declared in this format
StringTokenizer star = new StringTokenizer(st , SEPARATOR); //separator is '/'
Session s1 = new Session(int SessionID, String [] cineName, String[] cineType, String movieName, String date, int time);
int SessionID = Integer.parseInt(star.nextToken().trim()); //for integer
String movieName = star.nextToken().trim(); //for string
String [] cineName = ? //how about string array?
Thus, I wish to ask how I can implement the parsing of a cineName array.

You can store the tokens of a String which delimits tokens with "/" in cineArray as follows:
int tokens = star.countTokens();
for (int i=0;i<tokens;i++)
{
cineName[i] = star.nextToken().trim();
}

String [] cineName = star.nextToken().trim().split(CINE_NAME_ARRAY_SEPARATOR);

Related

How to convert a string contains array format to java array or list?

I have a string like this:
String res = '["A","B","0","1"]';
How to convert it to an array or list in Java to become like this:
String[] r = {"A","B","0","1"};
Since your string is a correct Json string, you may use the gson library:
String s = "[\"A\",\"B\",\"0\",\"1\"]";
Gson gson = new GsonBuilder().create();
String[] arr = gson.fromJson(s, String[].class);
// {"A","B","0","1"}
Without using Gson, You can get the desired result by following below approach -
String inputStr = "[\"A\",\"B\",\"0\",\"1\"]";
String[] strArray = inputStr.split("[^\\w\\d]");
List<String> list = new ArrayList<>();
for (String str : strArray) {
if (str != null && !str.isEmpty()) {
list.add(str);
}
}
System.out.println(list);
Output will be: [A, B, 0, 1]
String res = "[A,B,0,1]";
res = res.replace("[", "");
res = res.replace("]", "");
res = res.replaceAll(",", "");
String [] arrayOutput = new String [res.length()];
char [] ar = res.toCharArray();
for(int i = 0; i< ar.length; i++)
{
char buffer = ar[i];
arrayOutput[i] = String.valueOf(buffer);
}

How to convert char to String in tokenizer in Java

Trying to read a file separated by commas into an array and not sure how to make party into a string to work with tokenizer
for(int i = 0; i < s.length; i++) {
String str = scan.nextLine();
StringTokenizer st = new StringTokenizer(str, ",");
//String [] tokens = str.split(",");
String name = st.nextToken();
String abbreviation = st.nextToken();
long population = Long.parseLong(st.nextToken());
String govName = st.nextToken();
char party = st.nextToken();
int ageWhenElected = Integer.parseInt(st.nextToken());
s[i] = new State(name, abbreviation, population, govName ,party, ageWhenElected);
Try
char party = st.nextToken().charAt(0);
You can join your array of string with comma as separator
StringUtils.join(strArray, ",");

Using split function to split the string but not getting desired result

want to split the string
String vv = "class="fcg">1.6M";
String[] breakvv = vv.split(">");
String breakvv0 = parts[0];
String breakvv1 = parts[1];
System.out.println(breakvv0);
System.out.println(breakvv1);
Output which i am getting is breakvv0 = an and breakvv1 = class="fcg">1.6M
String vv = "class=\"fcg\">1.6M";
String[] breakvv = vv.split(">");
String breakvv0 = breakvv[0];
String breakvv1 = breakvv[1];
System.out.println(breakvv0);
System.out.println(breakvv1);
public static void main(String args[]) {
String vv = "class=\"fcg\">1.6M";
String[] breakvv = vv.split(">");
String breakvv0 = breakvv[0];
String breakvv1 = breakvv[1];
System.out.println(breakvv0);
System.out.println(breakvv1);
}
}

How can i split String in java with custom pattern

I am trying to get the location data from this string using String.split("[,\\:]");
String location = "$,lat:27.980194,lng:46.090199,speed:0.48,fix:1,sats:6,";
String[] str = location.split("[,\\:]");
How can i get the data like this.
str[0] = 27.980194
str[1] = 46.090199
str[2] = 0.48
str[3] = 1
str[4] = 6
Thank you for any help!
If you just want to keep the numbers (including dot separator), you can use:
String[] str = location.split("[^\\d\\.]+");
You will need to ignore the first element in the array which is an empty string.
That will only work if the data names don't contain numbers or dots.
String location = "$,lat:27.980194,lng:46.090199,speed:0.48,fix:1,sats:6,";
Matcher m = Pattern.compile( "\\d+\\.*\\d*" ).matcher(location);
List<String> allMatches = new ArrayList<>();
while (m.find( )) {
allMatches.add(m.group());
}
System.out.println(allMatches);
Quick and Dirty:
String location = "$,lat:27.980194,lng:46.090199,speed:0.48,fix:1,sats:6,";
List<String> strList = (List) Arrays.asList( location.split("[,\\:]"));
String[] str = new String[5];
int count=0;
for(String s : strList){
try {
Double d =Double.parseDouble(s);
str[count] = d.toString();
System.out.println("In String Array:"+str[count]);
count++;
} catch (NumberFormatException e) {
System.out.println("s:"+s);
}
}

Why does this conversion give an error?

I have this code in eclipse:
String A = String.valueOf(a);
String B = String.valueOf(b);
String C = String.valueOf(c);
String D = String.valueOf(d);
String E = String.valueOf(e);
String F = String.valueOf(f);
String G = String.valueOf(g);
String H = String.valueOf(h);
String I = String.valueOf(i);
String J = String.valueOf(j);
String K = String.valueOf(k);
String rawpassword = A+B+C+D+E+F+G+H+I+J+K;
int password = Integer.parseInt(rawpassword);
System.out.println(password);
And it gives me this error
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.parseInt(Integer.java:527)
at com.jakibah.codegenerator.Main.Generate(Main.java:65)
at com.jakibah.codegenerator.Main.run(Main.java:24)
at java.lang.Thread.run(Thread.java:745)
But I do not understand why.
can someone help me?
String A = String.valueOf(10);
String B = String.valueOf(10);
String C = String.valueOf(10);
String D = String.valueOf(10);
String E = String.valueOf(10);
String F = String.valueOf(10);
String G = String.valueOf(10);
String H = String.valueOf(10);
String I = String.valueOf(10);
String J = String.valueOf(10);
String K = String.valueOf(10);
String codestring = A+B+C+D+E+F+G+H+I+J+K;
BigInteger bigInteger = new BigInteger(codestring);
System.out.println(bigInteger.max(bigInteger));
The parseInt(String s) method throws a NumberFormatException if the argument is not a parseable Integer.
Make sure the String you pass to the method is a Number and is between -2^31 and 2^31 - 1
Number Format exception arise when you trying to convert a string type into a integer but that does not fit as a integer.
From your code I am not able to understand what value is a,b,c,d,..
From my experience I have uploaded two image to show you may be this kind of error you done
here number format exception arise as codestring is 10.320 and it is a string type so when compiler try to convert it as a string it unable to convert it due to .
But in this scenario codestring is 1020 , so easily it convert to a int.

Categories