7 Replies - 2473 Views - Last Post: 08 September 2012 - 03:37 PM Rate Topic: -----

#1 Albert Dong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-September 12

no suitable method found for error

Posted 06 September 2012 - 03:42 PM

I've been trying to write an applet that randomly picks 10 cards out of a deck of 52 and displays them on the screen. I have an helper method that picks 10 cards and put them in an array for another method to display. When I try passing the array to the other method, " No suitable method found for" and " actual and formal arguments differ in length" errors displays. I have no clue of what either of each mean and most explanations on the internet are code specific and hard to understand. I attached a picture of the error I've received.



import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.applet.Applet;
import java.lang.Math;
import java.util.Random;

public class randomDisplay extends Applet
{
	Image card1, card2, card3, card4, card5, card6, card7, card8, card9, card10, card11, card12, card13;
	Image card14, card15, card16, card17, card18, card19, card20, card21, card22, card23, card24, card25, card26;
	Image card27, card28, card29, card30, card31, card32, card33, card34, card35, card36, card37, card38, card39;
	Image card40, card41, card42, card43, card44, card45, card46, card47, card48, card49, card50, card51, card52;

	public void init()
	{
		card1 = getImage( getDocumentBase(), "c1.gif" );
		card2 = getImage( getDocumentBase(), "c2.gif" );
		card3 = getImage( getDocumentBase(), "c3.gif" );
		card4 = getImage( getDocumentBase(), "c4.gif" );
		card5 = getImage( getDocumentBase(), "c5.gif" );
		card6 = getImage( getDocumentBase(), "c6.gif" );
		card7 = getImage( getDocumentBase(), "c7.gif" );
		card8 = getImage( getDocumentBase(), "c8.gif" );
		card9 = getImage( getDocumentBase(), "c9.gif" );
		card10 = getImage( getDocumentBase(), "c10.gif" );
		card11 = getImage( getDocumentBase(), "cj.gif" );
		card12 = getImage( getDocumentBase(), "cq.gif" );
		card13 = getImage( getDocumentBase(), "ck.gif" );
		card14 = getImage( getDocumentBase(), "d1.gif" );
		card15 = getImage( getDocumentBase(), "d2.gif" );
		card16 = getImage( getDocumentBase(), "d3.gif" );
		card17 = getImage( getDocumentBase(), "d4.gif" );
		card18 = getImage( getDocumentBase(), "d5.gif" );
		card19 = getImage( getDocumentBase(), "d6.gif" );
		card20 = getImage( getDocumentBase(), "d7.gif" );
		card21 = getImage( getDocumentBase(), "d8.gif" );
		card22 = getImage( getDocumentBase(), "d9.gif" );
		card23 = getImage( getDocumentBase(), "d10.gif" );
		card24 = getImage( getDocumentBase(), "dj.gif" );
		card25 = getImage( getDocumentBase(), "dq.gif" );
		card26 = getImage( getDocumentBase(), "dk.gif" );
		card27 = getImage( getDocumentBase(), "h1.gif" );
		card28 = getImage( getDocumentBase(), "h2.gif" );
		card29 = getImage( getDocumentBase(), "h3.gif" );
		card30 = getImage( getDocumentBase(), "h4.gif" );
		card31 = getImage( getDocumentBase(), "h5.gif" );
		card32 = getImage( getDocumentBase(), "h6.gif" );
		card33 = getImage( getDocumentBase(), "h7.gif" );
		card34 = getImage( getDocumentBase(), "h8.gif" );
		card35 = getImage( getDocumentBase(), "h9.gif" );
		card36 = getImage( getDocumentBase(), "h10.gif" );
		card37 = getImage( getDocumentBase(), "hj.gif" );
		card38 = getImage( getDocumentBase(), "hq.gif" );
		card39 = getImage( getDocumentBase(), "hk.gif" );
		card40 = getImage( getDocumentBase(), "s1.gif" );
		card41 = getImage( getDocumentBase(), "s2.gif" );
		card42 = getImage( getDocumentBase(), "s3.gif" );
		card43 = getImage( getDocumentBase(), "s4.gif" );
		card44 = getImage( getDocumentBase(), "s5.gif" );
		card45 = getImage( getDocumentBase(), "s6.gif" );
		card46 = getImage( getDocumentBase(), "s7.gif" );
		card47 = getImage( getDocumentBase(), "s8.gif" );
		card48 = getImage( getDocumentBase(), "s9.gif" );
		card49 = getImage( getDocumentBase(), "s10.gif" );
		card50 = getImage( getDocumentBase(), "sj.gif" );
		card51 = getImage( getDocumentBase(), "sq.gif" );
		card52 = getImage( getDocumentBase(), "sk.gif" );
	}

	public void getCards()
	{
		String cardNumber; 
		double cardRandom;
		int cardRandomNumber;
		String[] cardSelection = new String[10];
		Random ran = new Random();
		Graphics g;

		for (int number = 0; number <=  9; )
		{
			cardRandom = ran.nextInt(52) + 1;
			cardRandomNumber = (int) Math.round( cardRandom );

			if ( cardRandomNumber > 0 && cardRandomNumber <= 52 )
			{ 
				cardNumber =  "card" + cardRandomNumber;
				number++;
			}
		}	
		paint( g, cardSelection );
	}

	public void paint(Graphics g, String[] card)
	{
		setBackground( Color.green );
		g.drawImage( card[0], 10, 10, this);
	}
}


Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: no suitable method found for error

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: no suitable method found for error

Posted 06 September 2012 - 04:01 PM

cardNumber = "card" + cardRandomNumber;

you cannot dynamically create a variable name like in this case

cardNumber = "card" + "10" to have card10

use an artray, it will shorten your code a lot and you can

cardNumber = card[cardRandomNumber];
Was This Post Helpful? 1
  • +
  • -

#3 SPorter  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 31
  • Joined: 29-June 12

Re: no suitable method found for error

Posted 06 September 2012 - 04:03 PM

Instead of making all those card objects and initializing each one by hand, why not make an array of them and initialize them via a loop?

			cardRandom = ran.nextInt(52) + 1;
			cardRandomNumber = (int) Math.round( cardRandom );


So, you have a Random object (ran) and are calling the nextInt method to return an int to assign to a double then rounding the int after you typecast the int to an int? This is not good... anyhow... the nextInt method for the random class returns an int. So you can get rid of cardRandom and typecasting and rounding and just use cardRandomNumber directly.

Also, nextInt(52) will return a number between 0 and 52, so by adding the + 1 to it, you are making it able to return 53, which is also not good.

	public void paint(Graphics g, String[] card)

	{
		setBackground( Color.green );
		g.drawImage( card[0], 10, 10, this);
	}



Now you are calling a method which does not exist...

This post has been edited by SPorter: 07 September 2012 - 07:46 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: no suitable method found for error

Posted 06 September 2012 - 04:05 PM

The first error messages mean that you're referring to a method that doesn't exist, doesn't have the same signature. Line 97,

g.drawImage( card[0], 10, 10, this);

calls a Graphics method with the signature:

drawImage( String, int, int, randomDisplay )

which doesn't exist. You can see the available Graphics.drawImage() methods at the Graphics API page.
Was This Post Helpful? 0
  • +
  • -

#5 Albert Dong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-September 12

Re: no suitable method found for error

Posted 07 September 2012 - 02:11 PM

View Postpbl, on 06 September 2012 - 04:01 PM, said:

cardNumber = "card" + cardRandomNumber;

you cannot dynamically create a variable name like in this case

cardNumber = "card" + "10" to have card10

use an artray, it will shorten your code a lot and you can

cardNumber = card[cardRandomNumber];


Ah. Agreed. I must have changed it when I was moving things around trying to fix various bugs. Ill go edit my post now.
Was This Post Helpful? 0
  • +
  • -

#6 Albert Dong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-September 12

Re: no suitable method found for error

Posted 07 September 2012 - 02:17 PM

View PostGregBrannon, on 06 September 2012 - 04:05 PM, said:

The first error messages mean that you're referring to a method that doesn't exist, doesn't have the same signature. Line 97,

g.drawImage( card[0], 10, 10, this);

calls a Graphics method with the signature:

drawImage( String, int, int, randomDisplay )

which doesn't exist. You can see the available Graphics.drawImage() methods at the Graphics API page.


That string was an array that was suppose to be linked to an image. I thought that if card[0] has card5 in it, it would link it to its image but I guess not. Anything I can do so that, it could work like that?
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: no suitable method found for error

Posted 07 September 2012 - 02:27 PM

Kinda hard to follow your thinking, but that parameter needs to be an Image object. So, if you were to use card5 instead of the String you used, then that should work. And if you wanted all of the card1 - card52 images in an array (as I believe was already suggested by someone else), then you should store them in an array and forget the clumsy numbers in the names. Instead of card1 holding the card1 Image, the image would be the first in an array of card Images:

Image[] cardImages = new Image[52];

and

cardImages[0] = card1; // instead of card1

Then you could modify the drawImage() method to print the desired card image by specifying the corresponding index in the cardImages[] array. Yes, you could determine which index that is somehow using the Strings you've already created, but I'm not sure that's the best way. Seems like that would add extra levels of abstraction that aren't helpful.

Think about it, and maybe you'll think of the most efficient way.

This post has been edited by GregBrannon: 07 September 2012 - 02:29 PM

Was This Post Helpful? 1
  • +
  • -

#8 Albert Dong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 06-September 12

Re: no suitable method found for error

Posted 08 September 2012 - 03:37 PM

Appreciate the help you guys gave me! Kudos!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1