• Post Reply Bookmark Topic Watch Topic
  • New Topic

Methods Need help  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone know what my problem in this code is? I am inputting it as all the guidelines say but can't get it rite.
Staff note (Knute Snortum):

Please don't start multiple threads on the same subject. I will merge this topic with the first one you started.

Staff note (Knute Snortum):

Merge didn't work for some reason. Let's continue the discussion here. I'll lock the older thread with a link here.

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling the menu method like this which means the method should return an int and have no parameters.  But your two method declarations look like this: This declares a method that returns an int (good) and takes one parameter (huh?).  This declaration is a syntax error, since method parameters need to be typed.  It also returns void, which is not what you need.

So, how would you declare a method that returns an int and has no parameters?
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still have no idea how it is supposed to be done. This appears to be the exact way all the textbooks say to do it. How do you input qt1
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
**how do you input ints then**
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I delete the variable n1, all it does is give me an error that I need a value of n1.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on how you are calling it, the getMenuOption() method signature should look like this: Because you're declaring that the method returns an int, you need a return statement at the end of your method: You don't need the two declarations, just one.  But there is another problem with your code.  On line 15 you close the class, then you try to write a method outside of the class.  Bring your method inside the brace or just move the brace to the bottom of the code.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I posted this question already, but do not think I asked it properly. I do not know why this code is failing, it says it is a double declared variable but that is not the case.
\
Staff note (Knute Snortum):

Please don't start a new thread when you have a question about the same program in another thread.

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the error I get:
It's important to copy the exact error message and the line number and program name.

This is telling you what I told you above, that you are declaring a method that returns an int, but you have no return statement.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the updated code, now receiving the error that int1 cannot be resolved to a variable.



 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember to include the line number with the error message.

Look carefully at the variable name you declare and the variable you return.  They are not the same, although they look similar.  This is why I never use "1" in my variable names.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 27 i declare int1
line 29 I return int1 ... WHich is what I am wanting to do, I want to return the menu selection
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 27 i declare int1
line 29 I return int1 ... WHich is what I am wanting to do, I want to return the menu selection
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohhh it's intl instead of int1, that explains it
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at the last letter of the two variables.  One is a number one, and the other is the letter el.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:ohhh it's intl instead of int1, that explains it

Ding, ding, ding!
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i receive the error
The method getMenuOption() in the type aCalculateP is not applicable for the arguments (int)



 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you calling getMenuOption() with a parameter?
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was putting i inside the parameter so that I can define what i is(which is the menu selection) and return that to the main method where I can then go on to perform my mathematic calculation.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also tried it as such
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was believing that having the i in the parameter, would allow me to run the method, and have the int1 value that is returned from the method stored in the value i.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, thanks, takes a while to sink in but then it suddenly clicks. I have literally been studying methods and arrays for 15 hours the past 2 days, and it just clicked. What a great feeling! I will move onto the rest of the code that I must do, which involves arrays, if I have any other questions would it be best to start a new thread or continue on with this one?
[code]

import java.util.Scanner;

public  class aCalculateP {

public static void main(String[] args) {
//menu choice variable

getMenuOption();

}


//getmenu input method
public static int getMenuOption() {
System.out.println("\tArray Calculations calculator \n");
System.out.println("1. Add\n");
System.out.println("2. Subtract\n");
System.out.println("3. Multiple\n");
System.out.println("4. Divide\n");
System.out.println("5. Dot Product\n");
System.out.println("6. Generate Random Number\n");
System.out.println("7. Quit\n");
System.out.println("Make your selection\n");
//activate scanner
Scanner input = new Scanner(System.in);
int int1 = input.nextInt();

return int1;

}

}


[code]
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You send arguments to a method with parameters.  Think of arguments as what you input into your method and the method parameters "receive" the input.

You don't need input into your getMenuOption() method, so it has no parameters, therefore it can't have any arguments when it's called.  You don't need them.  You call it like this:
But then you're not capturing the return value.  Your method declares that it has a return value of type int.  It also has a return statement, returning an int.  Now you need to capture that return value like this: Make sense?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:Got it, thanks, takes a while to sink in but then it suddenly clicks. I have literally been studying methods and arrays for 15 hours the past 2 days, and it just clicked. What a great feeling!

Isn't it though?
I will move onto the rest of the code that I must do, which involves arrays, if I have any other questions would it be best to start a new thread or continue on with this one?

Let's stay on this thread for now.
Unfortunately, I don't see you capturing the return value from getMenuOption().
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have moved onto where I am trying to get 2 double values, which I will use in my calculations. I call upon my method getOperand, and run the method, however I am not sure how to go about this, here is the specific part where I am stuck.




 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I try it like this and it also does not work
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread still active?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you use a method to input a value into an array, and return that array to your main method? This is what I have thus far

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a tutorial on arrays.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been reading tutorial after tutorial, not one mentions how you are to use a method collecting user input to return an array to another array in the main method
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is "value"? What are you doing with it?
You've declared "num" local to the for() loop. It will not be visible outside of the loop.
You are not assigning  your double to a particular slot in the array.
You are not declaring the length of the arry.
Your return syntax is wrong.
-- read some of the tutorials again, you are missing basic syntax issues.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to?
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The tutorial mentions nothing in regards to this. I do not know how to lay it out, I simply want to use a method to send 2 values back to the main method in an array, where I can then perform calculations with those 2 values
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No tutorial I have seen has mentioned anything in regards to this
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is from the link I just sent you.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my updated code with my array according to that, I still don't understand how I am to output the numArray in the  getOperand method to my main method operandsArray

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!