Member Avatar for san_fran_crisko

Hi, I cannot for the life of me figure out why I keep getting the error message, "cannot find symbol method methodname()" when I try to call methods from another method. I have tried changing the main method so its not static but it makes no difference.

Anyone who could shed light on the issue, it would be greatly appreciated.

Thanks.

import java.io.*; //import API to allow input of characters
import java.util.Vector; //import API to allow Vector functionality

class Details {
    
        String name = null;
        String artist = null;
        String year = null;
        String genre = null;
        String rating = null;

    Vector v = new Vector();        
    
        public void newcd() {
        
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.println("CD Name: ");
        
        try { //Reads keyboard input and acts accordingly
            name = stdin.readLine();
        }catch (IOException ex) {
            System.out.println("Problem reading from the keyboard"); }
            
        System.out.println("Artist: ");
        
        try { //Reads keyboard input and acts accordingly
            artist = stdin.readLine();
        }catch (IOException ex) {
            System.out.println("Problem reading from the keyboard"); }
            
        System.out.println("Year of album: ");
        
        try { //Reads keyboard input and acts accordingly
            year = stdin.readLine();
        }catch (IOException ex) {
            System.out.println("Problem reading from the keyboard"); }
            
        System.out.println("Genre: ");
        
        try { //Reads keyboard input and acts accordingly
            genre = stdin.readLine();
        }catch (IOException ex) {
            System.out.println("Problem reading from the keyboard"); }
            
        System.out.println("Rating: ");
        
        try { //Reads keyboard input and acts accordingly
            rating = stdin.readLine();
        }catch (IOException ex) {
            System.out.println("Problem reading from the keyboard"); }
        
        Details disc = new Details ();
        disc.name = name;
        disc.artist = artist;
        disc.year = year;
        disc.genre = genre;
        disc.rating = rating;
        
        v.add(disc);
    }
    
}
public class CDSystem { //main class

    public static void main (String [] args) { //main method containing menu system
    
        String input = null; //initialising input string
        int iInput;
        int ixInput;
    
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    
        System.out.println("Welcome to your CD Collection"); //commands to display menu on screen
        System.out.println("Select an option from the menu:");
        System.out.println("1: Add a new CD");
        System.out.println("2: Display CDs by artist");
        System.out.println("3: Display CDs by genre");
        System.out.println("4: Display all albums ascending by year");
        System.out.println("5: Display all albums descending by rating");
        System.out.println("Press any other key to exit");
        
        try { //Reads keyboard input and acts accordingly
            input = stdin.readLine();
        }catch (IOException ex) {
            System.out.println("Problem reading from the keyboard");
        
        iInput = Integer.valueOf(input); // convert String to Integer
        }
        if (iInput == 1) {
        newcd();
        }    
        if (iInput == 2) {
        artistdisplay();
        }
        if (iInput == 3) {
        genredisplay();
        }
        if (iInput == 4) {
        yeardisplay();
        }
        if (iInput == 5) {
        ratingdisplay();
        }
        else {
        System.out.println("Thank you for using CDSystem");
    }
    }
}
if (iInput == 1) {
        newcd();
        }    
        if (iInput == 2) {
        artistdisplay();
        }
        if (iInput == 3) {
        genredisplay();
        }
        if (iInput == 4) {
        yeardisplay();
        }
        if (iInput == 5) {
        ratingdisplay();
        }
        else {
        System.out.println("Thank you for using CDSystem");
    }
    }
}

If I understand correctly you try call this methods to do something?
I don't see any place in your code where you declare it, so I asume there in other class. To call this method from second class you do following

SecondClass secondClass = new SecondClass();
if (iInput == 1) {
        secondClass.newcd();
        }

Hope this helped, if not please provide detailed problem description

1. Give complete information. E.g. error msg (as printed on your console) so we know which line are we talking abt... ??
2. Is the error runtime or compile time??
3. Without that info all I can say/guess is there might be some unsatisfied linking kinda error. Assuming it's a runtime error of course. Hope you got it.. :)

I meant something like this.

Member Avatar for san_fran_crisko

Thanks for replying peter.

Firstly, apologies for not pointing out that those other classes and methods do not exist yet. Work in progress.

Secondly, thanks for the advice. At first it didn't work but it was because I had a random word "Details" in my code which Java was thinking was a variable and was mixing it up with the class called Details too. Once I isolated that, your code solved the problem.

Many thanks.

Crisko.

Member Avatar for san_fran_crisko

Thanks man, eventually got it solved (see above/below) but I'll bear that in mind in future.

Member Avatar for san_fran_crisko

Got another problem :sad:

Can anyone tell me why my menu system doesn't work? Whenever I enter a value when its running it exits every time. E.G. It will not run the newcd() method when I enter one.

if (iInput == 1) {
        newcd();
        }    
        if (iInput == 2) {
        artistdisplay();
        }
        if (iInput == 3) {
        genredisplay();
        }
        if (iInput == 4) {
        yeardisplay();
        }
        if (iInput == 5) {
        ratingdisplay();
        }
        else {

System.out.println("Thank you for using CDSystem");

input = stdin.readLine();

does return string and you are looking for integer. Does it help :cheesy: ?

Member Avatar for san_fran_crisko

Sorry, I should have put the code above in. Its converted to an integer from the inputted string. Still a mystery!

Look at this from your code:

try { //Reads keyboard input and acts accordingly
            input = stdin.readLine();
        }catch (IOException ex) {
            System.out.println("Problem reading from the keyboard");
        
        iInput = Integer.valueOf(input); // convert String to Integer
        }

You convert your string input to an integer only when an exception happens. You need to move the closing brace from the catch block to a point before the iInput = line.

And, as a matter of fact, your indenting implies this to the reader, but the braces define something else, and it's the braces that count.

Member Avatar for san_fran_crisko

Thanks masijade. I'll try that out when I get home from work.

Very much appreciated.

Member Avatar for san_fran_crisko

Yet another problem if anyone could help with this. Sorry for all the bother. Finding Java rather difficult. This time my problem is trying to call the main method from another method in another Class. Here is the Class and method:

class ADisplay {
    
    int i = 0;
    
    static String artistsearch;

    public static void artistdisplay(){
        
        Details disc; 
        
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        
            System.out.println("What artist would you like to search for?: ");
            
            try { //Reads keyboard input and acts accordingly
                artistsearch = stdin.readLine();
            }catch (IOException ex) {
                System.out.println("Problem reading from the keyboard");
            }
            
            for (int i=0; i<CDSystem.v.size(); i++) {
                disc = (Details)CDSystem.v.get(i);
            if ((artistsearch).equals(disc.artist)){
                
        System.out.println("Found detail(s):");
        System.out.println("CD Name: " + disc.name);
        System.out.println("CD Artist: " + disc.artist);
        System.out.println("CD Year: " + disc.year);
        System.out.println("CD Genre: " + disc.genre);
        System.out.println("CD Rating: " + disc.rating);
            
            }
        else{
        
        System.out.println("No CD info found. Returning to main menu");
}
}
CDSystem.main();            }

}

I keep getting the error "main(java.lang.String[]) in CDSystem cannot be applied to ()"

As far as I know it has definetely found the method but I don't know why it will not run it.

Thanks.

There is only one main!
So there is your problem ...

Member Avatar for san_fran_crisko

Does this mean I will have to call another method in another class?

Thanks

take it like this, you have three classes
One.java(have the main)
Two.java
Three.java

One can call any method of two and three. Two and three can call method of each other but can only do computing for one or they will return some values once they finish they job.

Member Avatar for san_fran_crisko

Ah, gotcha now.

Thanks.

Member Avatar for san_fran_crisko

This is fast becoming "spot the useless programmer". Unfortunately I've been stuck on this one all afternoon so I'm back to be saved by DaniWeb.

I'm getting the error message "unreported exception java.io.IOException; must be caught or declared to be thrown". The error is flagged when I try to call that method

Menu.menusystem()

from another method (in this case, three other methods). As far as I know theres no problems with the try and catch block I've implemeted and I've tried moving the code

throws java.io.IOException

to where the method is being called, including putting it in the brackets, however this doesn't work.

Instead of copying the entire code I'll put in one of the methods where I try to call the menudisplay method and the menudisplay method itself. If you want me to post the entire program I can do that too.

Thanks.

Method thats calling menudisplay:

class YDisplay {
    
    int i = 0;
    
    static String yearsearch;
    static Integer iyearsearch;

    public static void yeardisplay(){
        
        Details disc; 
        
            BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        
            System.out.println("What year would you like to search from? Results will be displayed in ascending order from searched year: ");
            
            try { //Reads keyboard input and acts accordingly
                yearsearch = stdin.readLine();
            }catch (IOException ex) {
                System.out.println("Problem reading from the keyboard");
            }
            
            iyearsearch = Integer.valueOf(yearsearch);
            
            for (int i=0; i<CDSystem.v.size(); i++) {
                disc = (Details)CDSystem.v.get(i);
            if ((iyearsearch).equals(disc.year)){
                
        System.out.println("Found detail(s):");
        System.out.println("CD Name: " + disc.name);
        System.out.println("CD Artist: " + disc.artist);
        System.out.println("CD Year: " + disc.year);
        System.out.println("CD Genre: " + disc.genre);
        System.out.println("CD Rating: " + disc.rating);
            
            }
        else{
        
        System.out.println("No CD info found. Returning to main menu");
}
}
Menu.menudisplay();        
 }
}

The menudisplay method:

class Menu {

    public static void menudisplay() 
        
        throws java.io.IOException
        { //main method containing menu system
    
        String input = null; //initialising input string
        int iInput = 0;
    
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    
        System.out.println("Welcome to your CD Collection"); //commands to display menu on screen
        System.out.println("Select an option from the menu:");
        System.out.println("1: Add a new CD");
        System.out.println("2: Display CDs by artist");
        System.out.println("3: Display CDs by genre");
        System.out.println("4: Display all albums ascending by year");
        System.out.println("5: Display all albums descending by rating");
        System.out.println("6: Exit");
        
        try { //Reads keyboard input and acts accordingly
            input = stdin.readLine();
        }catch (IOException ex) {
            System.out.println("Problem reading from the keyboard");
        }
        iInput = Integer.valueOf(input); // convert String to Integer
         
        
        switch (iInput) {
        case 1 : Details tempdetails = new Details();
                     tempdetails.newcd(); break;
        case 2 : ADisplay tempadisplay = new ADisplay();
                     tempadisplay.artistdisplay(); break;
        case 3 : GDisplay tempgdisplay = new GDisplay();
                     tempgdisplay.genredisplay(); break;
        }
    }
}
Member Avatar for san_fran_crisko

Never mind about that problem. Got it solved.

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.