Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: What am i doing wrong?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default What am i doing wrong?

    So, i want the programm to get 2 inputs from the user( car1money car1gas) and outprint them after it gets it. NOTE: i want this to be done with object and method procedures in order to understand the concepts of them( ignore the variables car2money and car2gas cause basically its the same thing). i know this is basic and im not sure if i should post it, but ill make this attemt to get help.

     
    import java.io.*;
    class testCars {
     
    	public static void main(String args[]) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
    	//fields
    	double car1money;
    	double car2money;
    	double car1gas;
    	double car2gas;
     
     
    	cars car1 = new Car1object();
    	car1.Car1Method();
     
    }
     
    class cars{
     
     
    	//constructor
    	 Car1object(double car1money,double car1gas){
    		this.car1money= car1money;
    		this.car1gas = car1gas;
    	}
     
    	//method
    	Car1Method(){
    		System.out.println(" Money for the 1st car: ");
    		car1money= br.read();
    		System.out.println(" gas for the 1st car: ");
    		car1gas= br.read();
    		System.out.println("You typed:" + car1money + car1gas);
    		}
     
    	}

    P.S. you can ignore the "throws IOexception" thank you in advance


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: What am i doing wrong?

    Is there some reason you're not just using a Scanner( System.in ) to get user input from the keyboard?

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: What am i doing wrong?

    Why do you have a constructor for a Carlobject in the class cars?

    Your BufferReader object appears to be only local to the main method. I don't think it can find it inside that other class.

    I see no carlmoney class variable declared inside the cars class.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What am i doing wrong?

    Is there some reason you're not just using a Scanner( System.in ) to get user input from the keyboard?
    yes, the excercise should be with no Scanner, even though im not sure what that means



    Why do you have a constructor for a Carlobject in the class cars?

    Your BufferReader object appears to be only local to the main method. I don't think it can find it inside that other class.

    I see no carlmoney class variable declared inside the cars class.
    where the constructor should be?

    so the br object has to be public?

    i dont think i understood compeletely what youre saying, but if im getting this, i have all my variables declared in the main class. shouldn't it be that way?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What am i doing wrong?

    have all my variables declared in the main class.
    Where is the class named: main? Did you mean the main() method?
    Variables defined in a method are NOT known outside of that method.
    If you want all the methods in a class to be able to access a variable it should be defined at the class level, outside of any method.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: What am i doing wrong?

    Where is the class named: main? Did you mean the main() method?
    Variables defined in a method are NOT known outside of that method.
    If you want all the methods in a class to be able to access a variable it should be defined at the class level, outside of any method.
    i took all the fields and placed them in the clas cars and now i get 2 errors

    Car1.java:25: error: invalid method declaration; return type required
    Car1object(double car1money,double car1gas){
    ^
    Car1.java:31: error: invalid method declaration; return type required
    Car1Method(){
    ^
    2 errors

    how should i fix this?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What am i doing wrong?

    Are those attempts to define a constructor for the classes? The name of the constructor must match EXACTLY the name of the class.

    If those are attempts to define a method in a class, then they must include a return type.

    See the tutorial:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Where am I going wrong
    By keepStriving in forum Java Theory & Questions
    Replies: 0
    Last Post: May 6th, 2013, 09:56 AM
  2. Re: What wrong?
    By rajesh_rasu26 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 21st, 2012, 11:03 AM
  3. What did I do wrong here?
    By Paetilium in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 19th, 2012, 11:13 PM
  4. [SOLVED] where I'm wrong?
    By arvindbis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 7th, 2012, 09:12 AM
  5. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM