4 Replies - 186 Views - Last Post: 12 February 2019 - 06:00 PM Rate Topic: -----

#1 acenkhabib   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-February 19

method in class cannot be applied to given types; Please Help!

Posted 12 February 2019 - 10:40 AM

I have 2 classes, one is the test class to run the code and the other is a constructor.

This class compiles successfully!
import java.util.*;

public class PapaPizzeria
{
	private String Size;
	private String Toppings;
	private int Price;
	int size;

	public String Size(int size)
	{

		Scanner siize = new Scanner(System.in);

		//public static setSize(int Sizes)
				//{
		System.out.println("Enter 1 for small, yada yada");
		size = siize.nextInt();
		if(size == 1)
		{
			Size = "Small";
			System.out.println("You have chosen Small");
			return Size;
		}
		else if(size == 2)
		{
			Size = "Medium";
			System.out.println("You have chosen Medium");
			return Size;
		}
		else if(size == 3)
		{
			Size = "Large";
			System.out.println("You have chosen Large");
			
		}
		else if(size == 4)
		{
			Size = "X-Large";
			System.out.println("You have chosen X-Large");
			
		}
			return Size;
				//}

	}
}

:code:

import java.util.*;

public class TestPapaPizzeria
{
	public static void main(String[] args)
	{
		PapaPizzeria P1 = new PapaPizzeria();

		//P1.setSize();

		System.out.println("The size of your pizza is " + P1.Size());
	}
}


:code:

Errors:
TestPapaPizzeria.java:11: error: method Size in class PapaPizzeria cannot be applied to given types;
		System.out.println("The size of your pizza is " + P1.Size());
		                                                    ^
  required: int
  found: no arguments
  reason: actual and formal argument lists differ in length
1 error


Tool completed with exit code 1

This post has been edited by modi123_1: 12 February 2019 - 10:51 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: method in class cannot be applied to given types; Please Help!

#2 wseng92   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 581
  • Joined: 23-September 15

Re: method in class cannot be applied to given types; Please Help!

Posted 12 February 2019 - 10:46 AM

Size function hold a parameter but there are no argument passed from TestPapaPizzeria.

For me, I will get user input from TestPapaPizzeria and pass to PapaPizzeria, instead of asking user input in class PapaPizzeria.
import java.util.*;

public class TestPapaPizzeria {
 public static void main(String[] args) {
  PapaPizzeria P1 = new PapaPizzeria();
  Scanner siize = new Scanner(System.in);
  System.out.println("Enter 1 for small, yada yada");
  int size = siize.nextInt();
  System.out.println("The size of your pizza is " + P1.Size(size));
 }
}

This post has been edited by wseng92: 12 February 2019 - 11:05 AM

Was This Post Helpful? 0
  • +
  • -

#3 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3617
  • View blog
  • Posts: 16,596
  • Joined: 20-September 08

Re: method in class cannot be applied to given types; Please Help!

Posted 12 February 2019 - 11:24 AM

Quote

For me, I will get user input from TestPapaPizzeria and pass to PapaPizzeria, instead of asking user input in class PapaPizzeria.


Absolutely.
Also, in Java, method names begin lower case, as do variable names
http://technojeeves....yle-conventions

    public String getSizeName(int size) {
        final String[] SIZES = { "", "Small", "Medium", "Large", "X-Large" }; // Better as a class variable
        return SIZES(size);
    }


is something to consider too. Bounds-checking needed
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2766
  • View blog
  • Posts: 10,958
  • Joined: 03-December 12

Re: method in class cannot be applied to given types; Please Help!

Posted 12 February 2019 - 02:49 PM

More critique,

Methods do things. Variables name things. Don't do
int size
public String Size


Use getters and setters

public class PapaPizzeria
{
	private String _size;
	private String Toppings; // <--- should be a list at minimum. 
	private int Price;


	public void setSize(int s)
	{
           if(s < 0)
	      System.out.print("Error in selection");
	   final String[] SIZES = { "", "Small", "Medium", "Large", "X-Large" };
	   if(s >= SIZES.length) 
              this._size = SIZES[s];

                // pass this in, don't make the class responsible for getting it
		// Scanner siize = new Scanner(System.in); 
        }

	public getSize()
        {
	    return this._size;
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 acenkhabib   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-February 19

Re: method in class cannot be applied to given types; Please Help!

Posted 12 February 2019 - 06:00 PM

Thank you guys so much! I'm still new to Java so the little critiques here and there help a lot. Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1