#method cannot be applied to types

1 messages · Page 1 of 1 (latest)

vital minnow
#

im very confused on whats wrong here please can someone help.

public String CardMovement(Card card) {
    
    if (canCardEnter(card)) {
        
        //Leave soruce enter destination.
        SourceArea.leave(card);             
        DestinationArea.enter(card);  
        //Adds and deducts credits and converts the points.
        card.AddCredits();
        card.DeductCredits();
        card.convertPoints();               
        return "Card has been moved to the new Area";
    } else {
        // Card does not meet conditions to enter the bridge
        return "The Card cannot be moved to the new area.";
    }
}```
astral gorgeBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

It seems like you have a typo in your code. The comment "//Leave soruce enter destination" should be "//Leave source enter destination". The word "source" is misspelled as "soruce".

vital minnow
#
public void AddCredits(int point){
    Points = Points + point;
}

public void DeductCredits(int cred){
    Credits = Credits - cred;
}```
astral gorgeBOT
vital minnow
silent sierra
# vital minnow

Your public void DeductCredits(int cred) method takes 1 parameter

#

which is how much credits to actually deduct

vital minnow
#

Yeah

#

So its causing the error in the CardMovement method?

#

Because its 1 parameter?

silent sierra
#

well you're calling it like card.DeductCredits()

#

so you're not giving it that one parameter

vital minnow
#

So I have to put it in the bracketss

#

?

silent sierra
#

yeah

vital minnow
#

Yea hbut I was getting errors still

#

Let me try again

silent sierra
#

The same is true for AddCredits as well

vital minnow
#

Yeah I know

#

Unless If Im doing it wrong but I thought it would be correct like this

silent sierra
#

why

#

what is cred

vital minnow
#

WHAT?

#

The int parameter in AddCredits

#

Thats point actually the deduct is cred

silent sierra
#

it wants a number

#

how much credits is it going to deduct?

vital minnow
#

Oh

silent sierra
#

it can't guess that

vital minnow
#

But the thing is

#

I want it to be an input later on so let me show you

#

i want to update credits later

#

So later when you actually run the UI it will say please enter how many creidts then it adds will putting a number cause a problem with this or no?

silent sierra
#

Why are you adding and deducting credits in the CardMovement method to begin with?

vital minnow
#

Actually it seems to be working

#

Well tbf even im not sure its what ive been asked to do so Ive done it

#

Im sure its fine though because when I go to the menu and add credits it adds credits fine now so yeah who knows hopeflly its all good lol

#

Thank you

obtuse epoch
#

I'm really not trying to be mean, but you really need to take a look at your lecture material to get a better grasp on how method invocations work. You've made a couple of topics the past few days with exactly the same issue.

astral gorgeBOT
#

method cannot be applied to types

#

Changed the title to method cannot be applied to types.

vital minnow
#

Yeah well Im trying just struggling lo

#

lol

#

How come when I TYPE in the ID then bridge code it becomes false

#
public boolean canMove(int trId, String brCode)
    {   
    Card card = getCard(trId);
    Bridge bridge = getBridge(brCode);
    if (card != null && bridge != null){
        return bridge.canCardEnter(card);
    }
    else{
        return false;

    }  ```
astral gorgeBOT
nova ingot
#

debug your program or add prints to see what's null

#

also where is the rest of your method ?

#

( u already got an if statement , when that not true u do not need an else but just a return false )

vital minnow
#

Yeah im doing the testing now to try find the issue

#

Its my last issue in my program is travel and can travel which is similar

vital minnow
#
public boolean canMove(int trId, String brCode)
    {   
    Card card = getCard(trId);
    Bridge bridge = getBridge(brCode);
        
    if (card == null || bridge == null)
    {
        return false ;
    }
    else
    {
        return bridge.canCardEnter(card);
    }
       
    }```
astral gorgeBOT
vital minnow
#
public String move(int pCardId, String brCode )
    {   //other checks optional
    
       
        
        Card card = getCard(pCardId);
        Bridge bridge = getBridge(brCode);
        
        if (bridge == null){
            return "You cant proceed";
        }
        else if (card == null){
            return "No card";
        }
        else
        {
            return bridge.CardMovement(card);
        }
        
    }```
astral gorgeBOT
vital minnow
#
private Bridge getBridge(String br)
    {
        for(Bridge bridge : bridgeList){
            if (bridge.equals(bridge.getCode()));
        }
        return null;
    }```
#
private Card getCard(int id)
    {
        for(Card card : newcardList){
            if (card.getID() == id){
                return card;
            }
        }
        return null;
    }```
#
public boolean canCardEnter(Card card){
    int CardID = card.getID();
    return SourceArea.isOn(CardID) && 
    (card.getLuxuryRating() >= DestinationArea.getRating()) && 
    !DestinationArea.isFull() && card.BridgeCrossing();
    
}
//card can enter a new area. 

public String CardMovement(Card card) {
    
    if (canCardEnter(card)) {
        
        //Leave soruce enter destination.
        SourceArea.leave(card);             
        DestinationArea.enter(card);  
        //Adds and deducts credits and converts the points.
        card.AddCredits(1);
        card.DeductCredits(4);
        card.convertPoints();               
        return "Card has been moved to the new Area";
    } else {
        // Card does not meet conditions to enter the bridge
        return "The Card cannot be moved to the new area.";
    }
}```
astral gorgeBOT
nova ingot
#

its the first method that's posted

#

that always returns null

#
private Bridge getBridge(String br)
    {
        for(Bridge bridge : bridgeList){
            if (bridge.equals(bridge.getCode()));
        }
        return null;
    }
astral gorgeBOT
nova ingot
#

even when the code would make sense ( which it doesnt), there is only 1 return and its always null

#

the part on not making sense is that your doing a for, then an if . And in the if your comparing to itself for some reason and ignoring the br bridge

vital minnow
#

private Bridge getBridge(String br)
{
for(Bridge bridge : bridgeList){
if (bridge.equals(bridge.getCode())){
return bridge;
}
}
return null;

}

#

First of all thiss one now has a retrun

#

return*

nova ingot
#

still is null

#
private Bridge getBridge(String br)
    {
        for(Bridge bridge : bridgeList){
            if (bridge.equals(bridge.getCode())){
                return bridge;
            }
        }
        return null;

reduces to :
private Bridge getBridge(String br){
 return null;
}
astral gorgeBOT
nova ingot
#

the logic u placed has no result what so ever, whenever

#

for every bridge u find, compare to itself with a empty getCode

#

even if u do manage to get a result, it will always return the 1st result

#

your plain and simply NOT using the getBridge(String br) part to compare to. I would really do as someone higher suggested and reread your basic java course. Your repeating the same mistake over and over and over

vital minnow
#
private Bridge getBridge(String br)
    {
        
        for(Bridge bridge : bridgeList){
            String code = bridge.getCode();
            if (code.equals(br)){
                return bridge;
            }
        }
        return null;
    
    
}```
astral gorgeBOT
nova ingot
#

now reduce that even more and it might be correct

#

there is no need for an inbetween step

vital minnow
#

What inbetween step?

nova ingot
#

String code = bridge.getCode();

#

that one

vital minnow
#

the if statment

#

oh

#

yeah then there would be an error with the line below?

nova ingot
#

your not doing anything with it basicly

vital minnow
#

private Bridge getBridge(String br)
{

    for(Bridge bridge : bridgeList){

        if (code.equals(br)){
            return bridge;
        }
    }
    return null;

}

nova ingot
#

code.equals code from air ?

vital minnow
#

bridge.equals

nova ingot
#

bridge.getCode().equals ......

#

u need to info code from the bridge your iterating, so u need to use the variable to get the info

#

your code doesn't do that, hence it faults

vital minnow
#

Yeah now its working with canmove its now working thank you

nova ingot
#

now do yourself a huge favour

#

read up 😉

vital minnow
#

Yeah I know

#

I am like ive been trying but im legit like done with this work its the last 2 methods and I jsut wanna finish ik i should i will even tho i already have this whole cdoing stuff seems to confuse me so much but yeah

nova ingot
#

your not really getting it

vital minnow
#

Im also confused why its asking me to enter bridge code but it doesnt let me once i do id it instantly goes

nova ingot
#

your trying to do it, yet your remaking the same mistake for about 3-4 days now

#

at that point, it is wiser to do some reading to why instead of trial and error

vital minnow
#

I know it is true

#

It is very true lol but im legit done pretty much lol

nova ingot
#

are u using scanner to get that input ?

vital minnow
#

I mean before yeah

#

Yeah scanner

nova ingot
#

right without even seeing the code -> here is why

astral gorgeBOT
#

Mixing any nextXXX method with nextLine from the Scanner class for user input, will not ask you for input again but instead result in an empty line read by nextLine.

To prevent this, when reading user input, always only use nextLine. If you need an int, do

int value = Integer.parseInt(scanner.nextLine());

instead of using nextInt.

Assume the following:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
int age = scanner.nextInt();
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

When executing this code, you will be asked to enter an age, suppose you enter 20.
However, the code will not ask you to actually input a name and the output will be:

Hello , you are 20 years old.

The reason why is that when you hit the enter button, your actual input is

20\n

and not just 20. A call to nextInt will now consume the 20 and leave the newline symbol \n in the internal input buffer of System.in. The call to nextLine will now not lead to a new input, since there is still unread input left in System.in. So it will read the \n, leading to an empty input.

So every user input is not only a number, but a full line. As such, it makes much more sense to also use nextLine(), even if reading just an age. The corrected code which works as intended is:

Scanner scanner = new Scanner(System.in);

System.out.println("Enter your age:");
// Now nextLine, not nextInt anymore
int age = Integer.parseInt(scanner.nextLine());
System.out.println("Enter your name:");
String name = scanner.nextLine();

System.out.println("Hello " + name + ", you are " + age + " years old");

The nextXXX methods, such as nextInt can be useful when reading multi-input from a single line. For example when you enter 20 John in a single line.

nova ingot
#

its a very, very common problem

vital minnow
#
private void moveNow()
    {
        System.out.println("Enter your ID");
        int location = reader.nextInt();
        System.out.println("Enter the bridge code");
        String bridge = reader.nextLine();
        System.out.println(fantasia.move(location,bridge));
        
    }```
astral gorgeBOT
nova ingot
#

see

#

no code, and they preprgramd a bot reply

#

and its spot on :p

#

nextInt / nextLine

#

the result is not what a human would assume it is

vital minnow
#

So I should just do both nextline?

nova ingot
#

yep and convert where needed

vital minnow
#

Yeah so i need to convert the int im guessing

#

or i get error

nova ingot
#

or handle the problem on another way ( but that's really going to give unknown problems due to user input being unknown )

vital minnow
nova ingot
#

( also your saying your almost done, if this is not an assigment or u got time, read up. Will save u HOURS of time )

vital minnow
#

nvm

vital minnow
#

I am defintly gonna read up mroe tho

nova ingot
#

your trying to parse an int to an itn

vital minnow
#

I actually thing im incapable lol

#

No I know I changed it

#

I noticed it after I sent it

nova ingot
#

your capable

#

your just trying to much without knowledge and thinking

vital minnow
#

Yeah

#

Well thank you for the help since yeah it works better now Ill try search up

#

Do you have any recomendations on videos and stuff

astral gorgeBOT
#

MOOC is a completely free introductory Java course created by the University of Helsinki, it is a great way to learn Java from the ground up.

It consists of two parts, one at beginner, and another at intermediate level. The end of the course is marked by creating your own Asteroids game clone!

Even though the instructions show how to configure and use NetBeans for the course, you can use IntelliJ. To use IntelliJ, simply install the TMC plugin by opening IntelliJ -> File -> Settings -> Plugins and searching for TMC. You will then be able to use IntelliJ to complete MOOC.

Visit MOOC here: https://java-programming.mooc.fi/
(the course is available in both English and Finnish)

About the course - Java Programming

vital minnow
#

Im just gonna go back to the early practicals and stuff

nova ingot
#

video's are a bad source. They are 99% outdated and 99.5% waste of actual time lost versus the stuff u only need

#

5 min video. Only need 2-3 seconds

vital minnow
#

Tbf Im like over 3 hours thru this 12 hour youbtube videos

#

its called BroCode Java course

#

Ive went through over 3 hours recently

#

From the start just tryna get information but tbf I guess its not been so helpful

nova ingot
#

might be. But i would assume after that time, u would atleast know the fundamentals

vital minnow
#

Yeah

#

Well it goes over all the stuff I mean I know certian things

nova ingot
#

what's a variable, what's a return type , access types etc

nova ingot
vital minnow
#

class level since its public?

nova ingot
#

can u show it ?

vital minnow
#
public void UpdatePoints(int Change_Credit, int Change_Points){
    Credits += Change_Credit;
    Points += Change_Points;
    
    System.out.println("Your credits are: " + Credits + "Your points are: " + Points);
    
    
}```
nova ingot
#

that's not it

#

that one

vital minnow
#
// provide the code here
    private void updateCredits()
    {
        System.out.println("Enter your ID");
        int id = reader.nextInt();
        System.out.println("Enter your amount of Credits");
        int credit = reader.nextInt();
        fantasia.topUpCredits(id, credit);
        
    }```
#

Oh so its only on a method level

nova ingot
#

yep as your not even storing it 😉

#

unless your doing that in the topUpCredits, but that's one of the things u need to think on when and how things need to get accessed

vital minnow
#

But the method was like that already

#

Like it was made I jsut added the middle part

#

But when I update credits it works

nova ingot
#

and when u add more then once ?

vital minnow
#

I guess thats points

nova ingot
#

see how fast it breaks when u dont go on 'normal' flow ?

vital minnow
#

what do you mean?

nova ingot
#

your making your program step 1 - 2 - 3 - 4 -5

#

reading int/text etc according to those steps

vital minnow
#

What id name rating credits points?

nova ingot
#

when u go out of order , it breaks easely

vital minnow
#

So your refering to the toString printed?#

nova ingot
#

always use nextLine and convert when needed

vital minnow
#

Next line after every line?

#

Like the input next line?

nova ingot
#

read a string for every input needed and convert that to the type u need

vital minnow
#

Ok thank you