-4

I read this Answers from Stack overflow

Tried it and work fine for the first occurrence of two characters.

Here i have an input String like

S1:99,78,67,60,75;S2:66,76,56,70,80;S3:89,76,81,70,90;

Answer should be displayed depending on the identifier S%. For example if the user writes S1, the programm should return the values till it reaches the semicolon. (in this example it should be: 99,78,67,60,75).

Could you help me achieve this result.

UserA1195
  • 103
  • 9
  • 3
    How-how, sit and write program – Andremoniy Dec 21 '16 at 09:27
  • (1) Look up the index of `S2`, (2) look up index of `;` in substring starting from (2), (3) result is substring from (1) to (2). – qqilihq Dec 21 '16 at 09:28
  • @Andremoniy I do tried to write it , i came up with wrong solutions, so i raised this question. And i don't know why people do down vote suddenly, – UserA1195 Dec 21 '16 at 09:41
  • 2
    @UserA1195 if you tried something include it inside the question no matter how wrong or right it is. Without it it seems to be a `please write code for me question`, instead of the usually accepted `hey i want that output, tried it this way but what i got was this` question. This way you usually don´t get downvotes. For more information refer to the [how-to-ask](http://stackoverflow.com/help/how-to-ask) section – SomeJavaGuy Dec 21 '16 at 09:42
  • @Andremoniy Will keep this suggestion and use it for further questions too. Thank you. – UserA1195 Dec 21 '16 at 09:44

6 Answers6

1

Answer using StringTokenizer

String value= "'S1':'99','78','67','60','75';'S2':'66','76','56','70','80';'S3':'89','76','81','70','90';";
char ch =  (char) System.in.read();
String NewValue=  value.replaceAll("'", "");
StringTokenizer str=new StringTokenizer(NewValue, ";");
    while(str.hasMoreTokens()){

          String checkstring=str.nextToken();
          char check=checkstring.charAt(0);

          if(ch==check){
                System.out.println(checkstring.substring(checkstring.indexOf(":")+1));
          }
    }
}

Input in console : S1

Output : 99,78,67,60,75

Thanks for all your answers

UserA1195
  • 103
  • 9
0
String value = "S1:99,78,67,60,75;S2:66,76,56,70,80;S3:89,76,81,70,90;";
String resultS1 = value.subString(value.indexOf("S1:"), value.indexOf(";");
String resultS2 = value.subString(value.indexOf("S2:"), value.indexOf(";");

You can write a function:

public String parseMyString(String toParse, int sStart){
    return toParse.subString(value.indexOf("S"+ sStart + ":"), value.indexOf(";");
}
Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • What about using a StringTokenizer? – Nico Dec 21 '16 at 09:31
  • 1
    because [here](https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html) they say: "The StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings" – Luca Nicoletti Dec 21 '16 at 09:34
  • Oh that's good to know :) Thanks. Because I used the StringTokenizer to split a String that looks like this `en_EN` at the underscore the get both parts as single string. Thought that could be done here to for `;` – Nico Dec 21 '16 at 09:37
  • 1
    Thanks @Nico to give idea about StringTokenizer and it works fine – UserA1195 Dec 21 '16 at 11:32
  • Does that work with Tokenizer? Never used them before! – Luca Nicoletti Dec 21 '16 at 11:33
  • Glad the StringTokenizer works for you :) It is pretty convenient and simplifies the code in my opinion. @UserA1195 – Nico Dec 21 '16 at 11:37
0

try to split the string first with ';' as separation character and then apply the "Answers from Stack overflow" on every substring.

Michriko
  • 337
  • 1
  • 11
0
String s = "S1:99,78,67,60,75;S2:66,76,56,70,80;S3:89,76,81,70,90;";
String[] h = s.split(";");
for (int i = 0; i < h.length; i++) {
    String result = h[i].split(":")[1];
    int number=i+1;
    System.out.println("S"+number+": "+result);
}

this will give the output:

S1: 99,78,67,60,75
S2: 66,76,56,70,80
S3: 89,76,81,70,90

there might be better ways to do it but this is a possibility

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
0

First of all, you should convert your data into a more appropriate structure, such as a Map, instead of searching for the values in a long string over and over again.

final static String DATA = "S1:99,78,67,60,75;S2:66,76,56,70,80;S3:89,76,81,70,90;";    
public static void main(String[] args) {
    Map<String,List<Integer>> data = Arrays.stream(DATA.split(";"))
            .map(s -> s.split(":",2))
            .collect(Collectors.toMap(
                    parts -> parts[0],
                    parts -> Arrays.stream(parts[1].split(","))
                            .map(Integer::valueOf)
                            .collect(Collectors.toList())));
    System.out.println("S2 = " + data.get("S2"));
}
Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
0

You can use following code to fulfill your requirement. I have used regex expression to identify the recursive parts of the string. Then on the while loop where matching parts used split to show the result you requested

    String s="S1:99,78,67,60,75;S2:66,76,56,70,80;S3:89,76,81,70,90;";

    // here regex used to  identify recursive strings
    Pattern pat = Pattern.compile("S\\d+:[\\d+|,]+;"); 
    Matcher mat = pat.matcher(s);

    while (mat.find()) {

        String match=mat.group();
        System.out.println(match.substring(0,match.indexOf(":"))+" : "+match.substring(match.indexOf(":")+1,match.indexOf(";")));

    }

You will get following output for the above

S1 : 99,78,67,60,75
S2 : 66,76,56,70,80
S3 : 89,76,81,70,90
Coder
  • 1,917
  • 3
  • 17
  • 33