Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My input String is as below,
{"string name"=hi;"id"=44401234;"string name"=hey;"id"=87695432.....}
I want only the ids as output like, {44401234 87695432}
for(int i=0;i<input.length();i++)
{
int index=input.indexOf("id");
hh=input.substring(index+4,index+12);
System.out.println(hh);
}
The below code uses Regex to get the value associated with id
try
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String input = "{\"string name\"=hi;\"id\"=44401234;\"string name\"=hey;\"id\"=87695432.....}";
Pattern pattern = Pattern.compile("\"id\"=[0-9]{8}");
Matcher m = pattern.matcher(input);
while (m.find()) {
String str = m.group();
str = str.replace("\"id\"=", "");
System.out.println(str);
}
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have a problem concatenating two strings or more be one variable in a loop in java.
My expected result as below :
/AIP/FE/perindo/custrpt/res_cus_1744341512710002.xml,/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml
The result is saved in one variable and return the value like this:
/AIP/FE/perindo/custrpt/res_cus_1744341512710002.xml,/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml
The following that i implemented :
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class pathData {
public static void main(String[] args) throws Exception {
String pthData = "\\\\10.28.88.28\\document\\FE\\perindo\\custrpt\\res_cus_1744341512710002.xml,\\\\10.28.88.28\\document\\FE\\perindo\\custrpt\\res_cus_1744341512710003.xml";
String bk = splitPath(pthData);
System.out.println(bk);
}
private static String splitPath(String bk) throws Exception {
String[] Str = bk.split(",");
String result = "";
if(Str.length > 1) {
for(int i = 0; i < Str.length; i++) {
String[] ss = Str[i].split("\\\\");
int a = ss.length;
String as = "/AIP"+"/"+ss[a-4]+"/"+ss[a-3]+"/"+ss[a-2]+"/"+ss[a-1];
result = as.concat(",");;
}
return result;
}
return "";
}
}
Based on my code, the result is always returned a value like this
/AIP/FE/perindo/custrpt/res_cus_1744341512710003.xml,
please give instructions and correct where the error lies.
You need to concatenate not just a "," to "as" but also the value of "as" to result. Otherwise it will only contain the last assigned value.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm writing a simple quiz program. The code on line 19 is giving me a run time error. Could someone please advise why?
import java.util.Scanner;
public class javaQuiz {
public static void main(String[] args) {
String questionOne = "Who is the best band member of the beatles?";
String questionOneAns = "John";
String questionTwo = "what is 1 + 1?";
int questionTwoAns = 2;
String questionThree = "What continent is China a part of?";
String questionThreeAns = "Asia";
String questionFour = "Who is the Turing Test named after?";
String questionFourAns = "Alan Turing";
Scanner userI = new Scanner(System.in);
String userAns = scan.NextLine();
System.out.println(questionOne);
if(userAns == questionOneAns) {
System.out.println("Correct!");
} else {
System.out.println("Wrong answer!");
}
}
}
Isnt it because your scanner is called userI instead of scan?
Line 19 should be:
String userAns = userI.nextLine();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to print out only
Int from String and first 2 char from string as well so, how i can do that, please help me thank!!
suppose,
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class match1 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String address = "1234 main street";
}
}
Output should be like:
1234 ma
How can i do that please help me!! Thanks!!
String address = "1234 main street";
String[] tokens = address.split(" ");
System.out.println(tokens[0]+" "+tokens[1].substring(0,2));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a string and I want to have that string get split up so that each individual character is in its own string. The string will vary in length as it is user inputted. Thanks in advance
If you actually want an array of strings from a string you can try this
String[] chars = myString.split("");
String str = /*Your String here*/;
char[] charArray = str.toCharArray();
String[] strArray = new String[charArray.length];
String strChars = "";
for (Character c : charArray){
int i=0;
strChars = c.toString();
strArray[i] = strChars;
System.out.println(strChars);
i++;
}
System.out.println(strArray.length);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to extract integer values in string.
For example
this is the java 1234 and 7899/45767 program
in that I need to extract only integer like
1234
7899
45767
I think a regex could fit your need:
import java.util.regex.*;
String aParser="123 bla bla 1234 bla bla 0324";
Pattern p=Pattern.compile("(\d+)");
Matcher m=p.matcher(aParser);
while(m.find())
{
//your code here
}
try this
Matcher m = Pattern.compile("-?\\d+").matcher(str);
while(m.find()) {
System.out.println(m.group());
}
You can try Scanner.nextInt() method
Scanner sc = new Scanner("this is the java 1234 and 7899/45767 program, in that i need to extract only integer like 1234 7899 45767");
while(sc.hasNext()) {
boolean scanned = false;
if(sc.hasNextInt()) {
System.out.println(sc.nextInt());
scanned = true;
}
if(!scanned)
sc.next();
}