aspose file tools*
The moose likes Beginning Java and the fly likes passing to method and returning value from method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

passing to method and returning value from method

Carie Herrick
Greenhorn

Joined: Sep 26, 2008
Posts: 16
Hi -

I'm in a quandry on how to make this work. In my directive, in my main() method the user is to enter a number using a Scanner Object. I believe that I'm supposed to accept this as a String as the next step in the assignment is I'm supposed to call a different method in the class (getIntFromKeyBoard()) in which this method looks at the String and converts it to an integer. It then returns the integer value back to the main() method.

I have no clue as to where to start. I know I could make this work in the main() method but the directive says it has to be done in a different method. This is what I've started with but I have no clue as to how to pass & call the values between the methods.

main()
//prompt user to enter movid ID

//create keyboard object from the Scanner Class for user input
Scanner keyboard = new Scanner(System.in);

//ask user to enter movie ID in String and process in getIntFromKeyboard to change to Integer
System.out.print("Enter movie ID (enter 0 to terminate program): ");

movieIdEntered = keyboard.next();

movieInt = movieIdValidated.getIntFromKeyboard(); //- I know this isn't right...


//The method I'm trying to pass & call from
public int getIntFromKeyboard()

int movieIdValidated;
int customerIdValidated;

if (String movieIdEntered <0)


This is a sample of what I think, but I'm really at a loss on how to pass between the methods.

Any help is appreciated. I don't want this done for me, I just step in the right direction on how I can accomplish this.

Thanks!
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
Originally posted by Carie Herrick:
Hi -


movieIdEntered = keyboard.next();

movieInt = movieIdValidated.getIntFromKeyboard(); //- I know this isn't right...


Take a look at the Sanner API. next() returns a String. You can use nextInt() instead which returns an integer type and then pass the result to your other method.

Carie Herrick
Greenhorn

Joined: Sep 26, 2008
Posts: 16
That's my frustration - I know I can convert the keyboard entry to int using nextInt() in the main() method.

Problem is, the directive explicitly says to send a string to the getIntFromKeyboard() method and convert it to a integer there. I don't get how this can be done. If I convert it to integer before sending to getIntFromKeyboard, than I don't see it as much as an issue and what you gave me as a solution will work.

Any ideas on how to pass a string and return an integer?
Satish Chilukuri
Ranch Hand

Joined: Jun 23, 2005
Posts: 266
Then pass the String to getIntFromKeyboard and use Integer.parseInt() to convert it to an int. Or do you need to write a string->int conversion algorithm?
[ September 26, 2008: Message edited by: Satish Chilukuri ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38859
    
  23
Please use the code button around all quoted code, and use ctrl-c ctrl-v to post code; your quoted code is difficult to read and looks as though it won't compile.

Have you really been told to use a Scanner and get its input and parse it to an int? The usual way to parse to an int is to use the parseInt() method of the Integer class, but a Scanner will read an int directly; if you tell it to look for nextInt() it will take "123" and produce the int 123. Note the warnings about the Scanner class I gave in this thread about � an hour ago.
Do you really have to pass the String to getNextIntFromKeyboard?
What details have you been given of how implement that method? Or has it already been written?

The name of a method ought to tell you what it does; a getNextIntFromKeyboard() method ought to get an int from the keyboard, which suggests it ought to have access to the Scanner object, and it calls for an int from the keyboard and returns that. If you are calling from the main method your Scanner could be set up as a static field.

Get the getNextIntFromKeyboard method working so it returns the number, print out the number, and leave off any validation until you have got that bit working.
Carie Herrick
Greenhorn

Joined: Sep 26, 2008
Posts: 16
My directive is this:

In main() Prompt the user to enter number - accept it. Call method from getIntKeyboard() with a Scanner parameter. The method uses a Scanner object that you created in main() and passes to getIntKeyboard() as an argument and returns a positive integer value.

Inside the getIntKeyboard()method, use an infinite do-while loop to enter next string token from the keyboard and convert the string to int. If the value is negative, print a rejection message, otherwise the method returns the value (as an integer) to main().

I know I can convert the string to integer in the code and pass. This is what I have doing it this way - I need help on how to change it to work to send a string argument


[ September 26, 2008: Message edited by: Carie Herrick ]
Carie Herrick
Greenhorn

Joined: Sep 26, 2008
Posts: 16
Please disregard this post - I've started a new one...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing to method and returning value from method