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?

2
  • 1
    inputLine.Substring should have a lower case s for substring. i.e. inputLine.substring(... docs.oracle.com/javase/1.4.2/docs/api/java/lang/… Commented Jul 17, 2012 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? Commented Jul 17, 2012 at 15:39

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

inputLine.substring(...

assuming inputLine is a String instance.

3 Comments

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, inputLine.indexOf(), as usual already have following the first call.
You had it correct in your second use of indexOf just missed the inputLine. before the first call.
0

Try this.

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

3 Comments

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.
@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?
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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.