-1

I'm trying to find two specific numbers (25,55) in a input list by converting them to tokens. e.g. below - string list = (52 98 55 86 42 25 87 566 56 843). Just for context, the numbers are prices for books bought in a week for a library.

If they are both in a line only then I want to know (print "both"). If only one of them is in the line or something like 5562 or 3259 (part of another number), i want a return of "no". I guess that's why I'm converting them to tokens.

This code below is not working unless i remove the else statement and even when i do remove it, it prints out "both" no matter what I numbers i put in, even if there's no 25 or 55. Sorry if this seems like a dumb question, pretty new to coding.

package part;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
public class Part {
   public static void main(String[] args) throws Exception {
      String list = "52 98 55 86 42 25 87 566 56 843";

      StringTokenizer tokenizer = new StringTokenizer(list);

      String rp = tokenizer.nextToken();
      if (rp.equals("25") && rp.equals ("55")){
          System.out.println("both");   
      } else { 
          System.out.println("no");
      } 
}
Eduardo Meneses
  • 504
  • 3
  • 18
hay2009
  • 11
  • 3

2 Answers2

1

StringTokenizer works like ResultSet when fetching queries in DB side. Considering it, you should do something like this:

public static void main(String[] args) throws Exception {
    String list = "52 98 55 86 42 25 87 566 56 843";
    List<String> tokenList = new ArrayList<>();

    StringTokenizer tokenizer = new StringTokenizer(list);
    while(tokenizer.hasMoreTokens()){
        tokenList.add(tokenizer.nextToken());
    }

    if(tokenList.contains("25") && tokenList.contains("55")){
        System.out.println("both");
    } else {
        System.out.println("no");
    }
}
Eduardo Meneses
  • 504
  • 3
  • 18
  • List tokenList = new ArrayList<>(); gives an error. I'm also trying a new code below but that also returns both if just one number is on there. – hay2009 Apr 27 '18 at 19:54
  • @hay2009 did you imported from java.util? which java version are you using? It worked here, so, try using `List tokenList = new ArrayList();` – Eduardo Meneses Apr 27 '18 at 19:57
  • IT worked after the import. I was working on another method but this will serve amazingly! Thanks so much. I'll definitely delve further into what you did with the arraylist. – hay2009 Apr 27 '18 at 20:01
0

StringTokenizer class is deprecated now. It is recommended to use split() method of String class or regex (Regular Expression).

Try using below code if you want to check that the contains both of them.

String list = "52 98 55 86 42 25 87 566 56 843";
String[] strarr = list.split("\\s+");
boolean first;
boolean second;
for(String str:strarr){
if(str.equals("25")) first=true;
if(str.equals("55")) second=true;
if(first && second) break;
}
if(first && second) System.out.println("both");
else System.out.println("no");
Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32
  • Its just that im too used to tokenizer. But your code will definitely help me learn more about strings. Thanks! – hay2009 Apr 27 '18 at 20:10