-2

I tried many ways. this is the one I gave up on

    double prevClose = 0;

    String test = "this is a test test test stop 123 3.5";
    Scanner parse = new Scanner(test);
    while(parse.hasNext()) {
        if(parse.next("stop") == "stop")
            prevClose = parse.nextDouble();
    }

    return prevClose;
minnymauer
  • 374
  • 1
  • 4
  • 17
  • 6
    Please.. not again.. compare Strings with `.equals` – Maroun Jul 29 '14 at 06:46
  • 2
    Well, to start with `parse.next("stop") == "stop"` isn't how `String` comparison works in Java. You could use `String#subString` and `String#indexOf`, you could use regular expression... – MadProgrammer Jul 29 '14 at 06:46
  • 5
    @MarounMaroun My day on Stackoverflow starts with a String compare question. – Suresh Atta Jul 29 '14 at 06:47
  • 5
    @sᴜʀᴇsʜᴀᴛᴛᴀ My day ends with it. – Maroun Jul 29 '14 at 06:48
  • 5
    The rest of the day is filled with it... – MadProgrammer Jul 29 '14 at 06:49
  • 3
    @MadProgrammer Have to propose stringcompare.stackoverlow.com on [area51](http://area51.stackexchange.com/) :P – Suresh Atta Jul 29 '14 at 06:51
  • 1
    You need to take a look at [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) and forgive us, but we see this type of problem all the time. It's an easy mistake for the inexperienced to make... – MadProgrammer Jul 29 '14 at 06:56
  • @MadProgrammer Thanks for the link. I'm running into another problem using the skip() method in the Scanner class. In a string I have a lone colon and want the double after it (in this test case its a string.) Using the skip() method from the Scanner class; why doesn't this work public String test() { String test = "why is this not : working"; Scanner parse = new Scanner(test); while(parse.hasNext()) { if(parse.next().equals("not")) { parse.skip(":"); test = parse.next(); } } return test; } – minnymauer Jul 30 '14 at 00:09
  • @user3884880 I don't know why, but `parse.skip(" : ");` works – MadProgrammer Jul 30 '14 at 00:41

7 Answers7

1
if(parse.next("stop") == "stop")

Above will never give you true. So prevClose will always be 0. Always do String comparisons with .equals() or equalsIgnoreCase() and not == opearator.

You can do the following

 while(parse.hasNext()) {
      if(parse.next().equalsIgnoreCase("stop"))
      prevClose = parse.nextDouble();
}
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1
  1. Compare Strings with equals.

  2. Remove this next("stop") an call next() without parameters.

    double prevClose = 0;
    
    String test = "this is a test test test stop 123 3.5";
    Scanner parse = new Scanner(test);
    while(parse.hasNext()) {
        if(parse.next().equals("stop"))
            prevClose = parse.nextDouble();
    }
    
    System.out.println(prevClose);
    

    output: 123.0

kai
  • 6,702
  • 22
  • 38
1

Let's ignore the String == String problem in your code to start with and go a different direction...

Basically, all you want is all the text AFTER stop, for this we can use a combination of String#indexOf and String#substring

String test = "this is a test test test stop 123 3.5";
test = test.substring(test.indexOf("stop") + "stop".length()).trim();

Once you have the text you want, you can simply split the remaining text on the " " delimiter and you should have all the numbers in an array...

String parts[] = test.split(" ");
for (String part : parts) {
    System.out.println(part);
}

Which outputs...

123
3.5

For the String compare problem, you should take a look at How do I compare strings in Java? for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • OP is actually facing this issue other than that compare issue +1. – Suresh Atta Jul 29 '14 at 06:57
  • @sᴜʀᴇsʜᴀᴛᴛᴀ This is just intended as an example of another way to attack the problem, not saying it's the best, just different (and I have a personal dislike of `Scanner` ;)) – MadProgrammer Jul 29 '14 at 06:58
1

Try this

double prevClose = 0;

String test = "this is a test test test stop 123 3.5";
String[] split = test.split("stop");
String rightArray = split[1];
String[] numbers = rightArray.split(" ");
for (int i = 1; i < numbers.length; i++) {
    prevClose = Double.parseDouble(numbers[i]);
    System.out.println(prevClose);
}

Output :

123.0
3.5
OO7
  • 2,785
  • 1
  • 21
  • 33
0

Not only in String comparison but also another issues there.

    double prevClose = 0;
    String test = "this is a test test test stop 123 3.5";
    Scanner parse = new Scanner(test);
    while (parse.hasNext()) {
        if ("stop".equals(parse.next())) { // String comparison and next()
            prevClose = parse.nextDouble();
        }
    }
    System.out.println(prevClose);
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Change to this

while(parse.hasNext()) {
    if(parse.next().equals("stop")) {
        prevClose = parse.nextDouble();
    }
}

Difference between == and equals - Since the String class override equals method, It return true if two String object contains same content but == will only return true if two references are pointing to same object.

Moreover Scanner#next()-Finds and returns the next complete token from this scanner.It does not accepts argument

SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • 1
    Okay, but why and what additional value does this add that the other answers haven't already done...? – MadProgrammer Jul 29 '14 at 06:56
  • My bad but no one tried to explain the difference between `==` and `equals` and why he should use `equals` in his case – SparkOn Jul 29 '14 at 07:01
  • The first two answers did, explain the problem and the solution, at least as far as I can see, I even mentioned it my own answer, which avoided the problem entirely... – MadProgrammer Jul 29 '14 at 07:04
0
    double prevClose = 0;

    String test = "this is a test test test stop 123 3.5";
   String[] test2 = test.split(" ");
   int i = 0;
    while(test2.length>i) {
        String string = test2[i];
        try {
            prevClose = Double.parseDouble(string);
            System.out.println(prevClose);
        } catch (NumberFormatException e) {
            //System.out.println("this is not number");
            //do nothing
        }

        i++;
    }

Ouptput :

123.0
3.5

This will work for any type of string independent of "stop"

Pravin
  • 1,137
  • 12
  • 20