0

I keep getting errors in my very short, simple code. I'm not sure what is wrong, can you please help me out?

Code:

import java.util.Scanner;


public class Driver {
private int age, carValue, tickets;



public getInfo(){
    Scanner in = new Scanner(System.in);
    System.out.println("Enter Age: ");
    age = in.nextInt();
    System.out.println("Enter Car Value: ");
    carValue = in.nextInt();
    System.out.println("Enter the number of tickets you have: ");
    tickets = in.nextInt();
}





}

This is my main:

class Main
{   public static void main(String args[])
{   
    Driver john;
    john = new Driver();
    john.getInfo();

}
}

My errors are:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method getInfo() is undefined for the type Driver

at Main.main(Main.java:6)

Ok, I have updated it now, and I still get errors.

1
  • all methods have to have a return type, even if they don't return anything ie: void Commented Feb 7, 2012 at 2:37

5 Answers 5

10

you must place the code:

Scanner in = new Scanner(System.in);

System.out.println("Enter Age: ");
age = in.nextInt();

inside some kind of method or constructor.

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

Comments

2

Try:

public class Driver {
private int age, carValue, tickets;
Scanner in;

public Driver()
{
this.in = new Scanner(System.in);

System.out.println("Enter Age: ");
this.age = in.nextInt();
}

}

7 Comments

No problem, also remember to "accept answer" if it helped you solve the problem else your accept-rate will go down witch will make it harder for you to get help in the future.
Indeed, it said I had to wait a few minutes before accepting.
I'm trying to use that new info to create an if else statement and its throwing me back some errors, can you take a look? gist.github.com/1756809
What errors? problems I can see from start is that you wont be able to access "a" outside the method.
If you want to use the method to declare a variable (for example float age = john.age();) you need to have a return statement. So what you want to do is changing "void" to the returntype(in this case float) and then return the value(in this case a).
|
2

your getInfo method declaration is incomplete - it needs a return type. Try public void getInfo() instead.

Comments

1
class Main
{   
    public static void main(String args[])
    {   
        Driver john;
        john = new Driver();
    }
}

should look like this, you don't need two classes it just complicates things un-necessarily and violates the principal of high cohesion and loose coupling.

class Driver
{   
    private int age;
    private int carValue;
    private int tickets;

    public void setAge(final int age)
    {
       this.age = age;
    }

    public static void main(final String args[])
    {   
        final Driver john = new Driver();

        final Scanner in = new Scanner(System.in);

        System.out.println("Enter Age: ");
        john.setAge(in.nextInt());

    }
}

1 Comment

it is un-neccessary the way they are doing it, this is more cohesive.
0

Take a closer look at Reteif's answer. Your original code works great!! You just have to add the return type 'void' to getinfo().

In the code you posted you have getInfo declared like this.

public getInfo(){
//code stuff
}

When it should be like this:

public void getInfo(){
//code stuff
}

I've compiled the code you posted with this small fix and it works fine. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.