vendredi 27 avril 2018

Finding two specific numbers in a list using string tokenizer

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");}


}
}

Aucun commentaire:

Enregistrer un commentaire