Question:
Java Compile Error: Cannot find symbol?
AMALIAT21
2011-06-19 21:51:03 UTC
This is my program:
import java.util.*;
import java.util.InputMismatchException;

public class A08
{
public static void main(String[]args)
{
//get filename from user
Scanner keybd = new Scanner(System.in);
System.out.println("Enter a filename to convert: ");
String input = keybd.nextLine();

//trim filename to disregard whitespace characters
String file;
file = input.trim();
System.out.println("Filename: " + file);

//get
int period;
period = file.lastIndexOf();
System.out.println("Period is position: " + period);
}
}

When i try to compile, I get the following error:

1 error
atjs-MacBook-Pro:ICS111 atj$ javac A08.java
A08.java:20: cannot find symbol
symbol : method lastIndexOf()
location: class java.lang.String
period = file.lastIndexOf();
^

I am new to Java. I dont understand what I have to change so that it will compile. Any advice is appreciated. Thank you!
Eight answers:
R.F.
2011-06-19 22:11:24 UTC
The "Cannot find symbol" usually means it can't find the variable or name that you're trying to use.

The "A08.java:20: cannot find symbol" tells you the error is in line 20.



And it actually gives you the line with the error:

period = file.lastIndexOf();



You're trying to use the lastIndexOf() method from the String class.



Get use to using the Java API. It gives lists all the Java SE classes, methods, and properties.

Here's the String class API:

http://download.oracle.com/javase/6/docs/api/java/lang/String.html



Look at the lastIndexOf() methods available in this class.

You will see that there are 4 different signatures to this same named class, all with different arguments.

You are trying to use a version of this method with no arguments, which does not exist in the String class. That's why the compiler is giving the error that it cannot find the method you are trying to call.



What you are trying to do in: period = file.lastIndexOf();

is: call the lastIndexOf() method evaluating the String object called "file".

This method will look for the last occurrence of a certain character or string.

But as all the other lastIndexOf() methods show in the API, you need some kind of argument tell it what to look for, which you didn't.



From the last statement in your program:

System.out.println("Period is position: " + period);



I will guess you want to look for a period in the "file" String. So you have to tell the method that is what you want to look for. You do that by passing that information to the method as an argument like this:



period = file.lastIndexOf(".");



Now it knows what to look for, a String with a "."



Fix that and it should work now.
2016-12-14 09:44:24 UTC
Java Error Cannot Find Symbol
mcelwaine
2016-09-30 14:20:22 UTC
Java Cannot Find Symbol
2015-08-16 15:42:16 UTC
This Site Might Help You.



RE:

Java Compile Error: Cannot find symbol?

This is my program:

import java.util.*;

import java.util.InputMismatchException;



public class A08

{

public static void main(String[]args)

{

//get filename from user

Scanner keybd = new Scanner(System.in);

System.out.println("Enter a filename to...
Rita
2016-01-31 01:46:23 UTC
java compile error find symbol
auddrey_nw
2011-06-19 21:59:15 UTC
file.lastIndexOf()



the .lastIndexOf() method must have argument . But you kept it blank. Please take a look at the following example and understand the use of .lastIndexOf()





// Demonstrate indexOf() and lastIndexOf().

class indexOfDemo {

public static void main(String args[]) {



String s = "Now is the time for all good men " +

"to come to the aid of their country.";

System.out.println(s);

System.out.println("indexOf(t) = " +

s.indexOf('t'));

System.out.println("lastIndexOf(t) = " +

s.lastIndexOf('t'));

System.out.println("indexOf(the) = " +

s.indexOf("the"));

System.out.println("lastIndexOf(the) = " +

s.lastIndexOf("the"));

System.out.println("indexOf(t, 10) = " +

s.indexOf('t', 10));

System.out.println("lastIndexOf(t, 60) = " +

s.lastIndexOf('t', 60));

System.out.println("indexOf(the, 10) = " +

s.indexOf("the", 10));

System.out.println("lastIndexOf(the, 60) = " +

s.lastIndexOf("the", 60));



}

}



Here is the output of this program:





indexOf(t) = 7

lastIndexOf(t) = 65

indexOf(the) = 7

lastIndexOf(the) = 55

indexOf(t, 10) = 11

lastIndexOf(t, 60) = 55

indexOf(the, 10) = 44

lastIndexOf(the, 60) = 55
Linda
2016-03-13 12:11:00 UTC
Hmm... considering that "MYSQL" is the name of the class, it can't be the name of a variable. You need to declare an instance of the MYSQL class and call that.
2011-06-19 22:04:52 UTC
change

period = file.lastIndexOf();

to

period = file.lastIndexOf(".");



seems like you are trying to find the index position of the last period?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...