0

I have a line of data that contains various non-delimited field values.

X55655PlateX58745CupX52689SaucerX52459SpoonH59876KnifeX59866Fork

I am trying to use substring and indexOf to pull the data, using a series of statements similar to the one below.

dataField1 = substring.inputLine((indexOf("Marker1: ")+9),(inputLine.indexOf("Marker2:")-1));

I keep getting error:cannot find symbol pointing to substring. Typically, this is when I've not called the proper java packages in, but I'm using the following:

import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.lang.*;

and java.lang.* is where the string methods live, right? I'm also initializing String datafield1 = "";.

dataField1 = inputLine.Substring((indexOf("Marker1: ")+9),(indexOf("Marker2:")-1));

results in indexOf receiveing the cannot find symbnol error. But if indexOf is ALSO a method of String, where would that need to go?

My program appears to be using the proper syntax substring(x,y) as noted in the java documentation, however, there are no real-world examples applicable to my troubleshooting.

I've reviewed this article but it doesn't quite get me to wrap my head around the concepts any better.

Why dioes this keep erroring out, and how can I prevent it?

Community
  • 1
  • 1
dwwilson66
  • 6,806
  • 27
  • 72
  • 117
  • 1
    `inputLine.Substring` should have a lower case **s** for `substring`. i.e. `inputLine.substring(...` http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#substring%28int,%20int%29 – Jon Taylor Jul 17 '12 at 15:35
  • Generally, you don't need to import `java.lang`, as it's imported 'by default' (given it contains `Object`). But, isn't this a good case for actually using `Pattern`/`Matcher` (regex) combination? – Clockwork-Muse Jul 17 '12 at 15:39

3 Answers3

7

substring should be called on a String object. Otherwise it is looking for a method called substring in your current class.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
4

substring() is a method of String, should be:

inputLine.substring(...

assuming inputLine is a String instance.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Thanks! When I do that, then indexOf becomes the unknown symbol. If indexOf is also a method of String...where would that need to go? – dwwilson66 Jul 17 '12 at 15:30
  • @dwwilson66, `inputLine.indexOf()`, as usual already have following the first call. – hmjd Jul 17 '12 at 15:31
  • 1
    You had it correct in your second use of indexOf just missed the `inputLine.` before the first call. – Jon Taylor Jul 17 '12 at 15:31
0

Try this.

String dataField1 = inputLine.substring((inputLine.indexOf("Marker1: ")+9),
                                        (inputLine.indexOf("Marker2:")-1));
Anthony
  • 12,177
  • 9
  • 69
  • 105
nook
  • 2,378
  • 5
  • 34
  • 54
  • 1
    you ought to explain why what you are doing makes the code valid rather than just rewriting code. This helps the OP to understand what he/she was doing wrong. – Jon Taylor Jul 17 '12 at 15:29
  • @publ1c_stat1c Actually...I think I see it...because substring and indexOf are both methods of String, I need to call them from my string in both instances: inputline.substring AND inputline.indexOf. Is that a correct statement? – dwwilson66 Jul 17 '12 at 15:35
  • 1
    Yes that is correct. Methods must be called on the object otherwise it looks for the method defined in your class. Since substring is not a method in your class it `cannot find the symbol` – Jon Taylor Jul 17 '12 at 15:37