I was reading Programming with java for Dummies 2nd Edition and came across this piece of code. I tried to read a chrachter using scannerobj.findInLine(".").charAt(0) method.
But I get an exception saying theres no such method. And even nextLine, nextWord isnt working.

Please help.

I have pasted the exact code from that book.
import java.util.Scanner;

import java.util.Scanner;
class ReverseWord {
    public static void main(String args[]) {
        Scanner myScanner = new Scanner(System. in );
        char c1, c2, c3, c4;
        c1 = myScanner.findInLine(".").charAt(0);
        c2 = myScanner.findInLine(".").charAt(0);
        c3 = myScanner.findInLine(".").charAt(0);
        c4 = myScanner.findInLine(".").charAt(0);
        System.out.print(c4);
        System.out.print(c3);
        System.out.print(c2);
        System.out.print(c1);
        System.out.println();
    }
}

This is the exception that I got.

E:\JAVA Programs\chartest>java MyClass
Exception in thread "main" java.lang.NoSuchMethodError: main

Recommended Answers

All 15 Replies

What IDE are you using?
I ran it in NetBeans and it worked. I only changed the code a little.
The code I used left out the 'class ReverseWord' and just used the file
name class (I called it two.java):

import java.util.Scanner;

public class two {
        public static void main(String args[]) {
            Scanner myScanner = new Scanner(System.in);
            char c1, c2, c3, c4;
            c1 = myScanner.findInLine(".").charAt(0);
            c2 = myScanner.findInLine(".").charAt(0);
            c3 = myScanner.findInLine(".").charAt(0);
            c4 = myScanner.findInLine(".").charAt(0);
            System.out.print(c4);
            System.out.print(c3);
            System.out.print(c2);
            System.out.print(c1);
            System.out.println();
        }
}

It works like a charm.

- WolfShield

I use JDK 1.6 and JRE 1.5 without an IDE. I do have eclipse. But wanted to try out in Command prompt. So Im compiling it in Command prompt.

Im new to Java. Thats why I thought of trying stuff out in the command prompt.
It doesn't matter right?

What is the name of the class you are trying to execute?

Your posted code has: class ReverseWord {

Your java command: java MyClass

Where is the MyClass class file? Does it have the correct main() method?

Oooohhhh I see lol!

Thank you very much! And BTW, isnt nextWord a method of Scanner? But it isnt working.

it isnt working.

Please explain. Post full text if errors

This is the program:

import java.util.Scanner;

class MyClass 
{

	public static void main(String args[]) 
	{
		String steve;
		Scanner in = new Scanner(System.in);
		steve=in.nextLine();
		System.out.println(steve);
		
		
	}
}

Error:

E:\JAVA Programs\chartest>javac chartest.java
chartest.java:9: cannot find symbol
symbol : method nextWord()
location: class java.util.Scanner
steve=in.nextWord();
^
1 error

Strange. Your posted code does NOT have the same source line in it as the error message.

Double check that the files are where you think they are.
You must have two copies of the source. The one you posted and the one you are trying to compile.

The source file you compiled is named: chartest.java
The code you posted shows another class.

No its correct. The filename is chartest.java and class name is MyClass.

Did you read my post about there being two source files?
One has nextWord() in it
the other (the one you posted) does not have nextWord(). It has has nextLine()

Figure out which one you want to work with and delete the other one.

BTW, isnt nextWord a method of Scanner?

Have you read the API doc for the Scanner class?

In Java, you have to have a class that is the same name as the file name, or it gives
an error.
So, if your file is called 'chartest.java' you need to have a class named 'chartest'.
Try that and let us know what happens.

- WolfShield

No Im just playing around with the same file. I edited it and compiled it. Thats all.

In Java, you have to have a class that is the same name as the file name, or it gives
an error.

Have you tried it? I don't get an error with a non public class and no public class.

// Test if non public class gives error 
// This program saved in TestNonPublic.java

class NonPublic {
  int x = 1;
}/*  Console:
Running: D:\Java\jdk1.6.0_25\bin\javac.exe  -Xlint -g -deprecation -classpath D:\JavaDevelopment\;. TestNonPublic.java


0 error(s)
*/

I have used different file names multiple times. But i got no error while compilation.

BUt I use class name to execute the files. So that cant be the problem surely.

Can anyone please refer the javadocs and tell whether nextWord() is part of the Scanner class or not? Im a beginner and I i couldnt read the docs properly.

Thanks.

The code+messages you posted earlier point definitively to multiple versions of your files, regardless of what you may believe.
Java requires that public classes are defined in a .java file with the same name as the class, but there is no such requirement for non-public classes as in your code. (It's not required for your main method to be in a public class, but it is normal practice.) However, the compiled .class files always have the same name as the (single) class they contain.
By naming your .java file differently from the class in it you are creating an opportunity for confusion, ie you compile chartest.java but you get myClass.class as output, this making it hard to match source and compiled files.
It would be a good idea
(1) to make the source code file name match the class name
(2) delete all the other .java files
(3) delete every .class file
(4) compile & run again.

Asking someone else to read the JavaDoc for you is a really bad idea (plus nobody's going to do that anyway). Searching the API doc is a fundamental essential skill for Java developers. Go to the JavaDoc for the Scanner class. You will find
A description of the class with examples
A list of all the constructors for the class
A list of all the methods for the class
A list of all the other methods that this class inherits.
- click any method name for full details

It's not hard, just try it.

OK. I understand thanks for that. Thank you very much!

I used the docs and found that scannerObj.next() has a delimiter as " "
So it essentially parses a word
Thanks for motivating me james, to look into the java docs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.