# How to use several methods with arguments/condition statements

Page 1 of 1

## 5 Replies - 1953 Views - Last Post: 14 October 2010 - 11:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=194547&amp;s=b3d18c45cb7e0e973e34fb6dca1ea1c2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DirtyMartyr

Reputation: -3
• Posts: 12
• Joined: 30-September 10

# How to use several methods with arguments/condition statements

Posted 11 October 2010 - 07:51 PM

I'm working on my latest assignment, & I'm having a lot of trouble arranging my script in the right format. The original assigment is as follows:

Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selections:

1. Convert to kilometers
2. Convert to inches
3. Convert to feet
4. Quit the program

The program will convert the distance to kilometers, inches, or feet, depending on the users selection. Here are the specific requirements:

 Write a method named calcKilometers, which accepts the number of meters as an argument and returns the number of kilometers using the following formula: kilometers = meters * .001
 Write a method named calcInches, which accepts the number of meters as an argument and returns the number of inches using the following formula: inches = meters * 39.37
 Write a method named calcFeet, which accepts the number of meters as an argument and returns the number of feet using the following formula: feet = meters * 3.281
 Write a void method named menu that displays the menu of selections. This method should not accept any arguments.
 The program should continue to display the menu until the user enters 4 to quit the program.
 The program should not accept negative numbers for the distance in meters.
 If the user selects an invalid choice from the menu, the program should display an error message.
 Here is an example session with the program, using console input. The users input is shown in bold:

Enter a distance in meters: 500 [Enter]
1. Convert to kilometers.
2. Convert to inches.
3. Convert to feet.
4. Quit the program.

500 Meters is 0.5 kilometers.

1. Convert to kilometers.
2. Convert to inches.
3. Convert to feet.
4. Quit the program.

Bye!

The purpose of this assignment is to get familiar with writing and calling methods. I have most of my script already written out (except I need to add error statements when user enters negative number, or invalid menu choice), but I'm having a lot of trouble organizing & correctly laying out my methods/variables/arguments/conditions. I'm getting about 20+ errors, & I know this is due to incorrect format. Otherwise, I'm usually pretty decent at identifying & correcting errors. I was wondering if someone could please offer some advice, an example script, or any other tidbits to get me going on the right track?

Here is my script thus far:

```/***********************************************************************
@Title:	BSarahConversion.java
@Purpose:	To get familiar with writing and calling methods
@Author:    (BSarah)
@Date:   	(October 09, 2010)
@Version:	1.0
************************************************************************/
import java.util.Scanner;
public class BSarahConversion
{
public static void main(String[] args)
{

int quit = 4;

int usersChoice;

Scanner sc = new Scanner(System.in);
System.out.print("Please enter distance in meters: ");
inputMeters = sc.nextDouble();
System.out.println("Please make a selection between 1-4: ");
usersChoice = sc.nextInt();

do
while (usersChoice = sc.nextInt());
if (usersChoice==1)
{
calcKilometers();
}
if (usersChoice==2)
{
calcInches();
}
if (usersChoice==3)
{
calcFeet();
}
while (usersChoice==quit);
{
System.out.println("Good bye!");
}

{
System.out.println("1. Convert to kilometers");
System.out.println("2. Convert to inches");
System.out.println("3. Convert to feet");
System.out.println("4. Quit the program");

}

public static calcKilometers()
{
double convertedInput = (inputMeters * .001);
System.out.println(convertedInput);

}

public static void calcInches()
{
double convertedInput = (inputMeters * 39.37);
System.out.println(convertedInput);

}

public static void calcFeet()
{
double convertedInput = (inputMeters * 3.281);
System.out.println(convertedInput);

}
}
}

```

**I also suspect my loop is wrong. Especially since I only need to loop the menu & user inputMeters. I think once I place my condition statments in the methods as arugments, then that will correct most of that problem. Still not sure how to do this correctly tho.

This post has been edited by DirtyMartyr: 11 October 2010 - 08:00 PM

Is This A Good Question/Topic? 0

## Replies To: How to use several methods with arguments/condition statements

### #2 SegFaulty

Reputation: 13
• Posts: 31
• Joined: 11-October 10

## Re: How to use several methods with arguments/condition statements

Posted 11 October 2010 - 10:10 PM

First you don't want to put other methods inside the main method; they go outside it. Here's how I would have set up the program:

```import java.util.Scanner;

public class test{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Please enter distance in meters: ");
double inputMeters = sc.nextDouble();

while(true){  //This loop will run until we issue the break command
System.out.print("Please make a selection 1-4: ");
int usersChoice = sc.nextInt();
if(usersChoice==1)  //No need for brackets if the there's only one action following the if statement
calcKilometers(inputMeters);
if(usersChoice==2)
calcInches(inputMeters);
if(usersChoice==3)
calcFeet(inputMeters);
if(usersChoice==4)
break; //here we break the loop
}

}

System.out.println("1. Convert to kilometers");
System.out.println("2. Convert to inches");
System.out.println("3. Convert to feet");
System.out.println("4. Quit the program");
}

public static void calcKilometers(double inputMeters){
double convertedInput = (inputMeters * .001);
System.out.println(convertedInput);
}

public static void calcInches(double inputMeters){
double convertedInput = (inputMeters * 39.37);
System.out.println(convertedInput);
}

public static void calcFeet(double inputMeters){
double convertedInput = (inputMeters * 3.281);
System.out.println(convertedInput);
}
}

```

Hope this helps!

### #3 DirtyMartyr

Reputation: -3
• Posts: 12
• Joined: 30-September 10

## Re: How to use several methods with arguments/condition statements

Posted 12 October 2010 - 10:08 AM

SegFaulty, on 11 October 2010 - 10:10 PM, said:

First you don't want to put other methods inside the main method; they go outside it. Here's how I would have set up the program:

```import java.util.Scanner;

public class test{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Please enter distance in meters: ");
double inputMeters = sc.nextDouble();

while(true){  //This loop will run until we issue the break command
System.out.print("Please make a selection 1-4: ");
int usersChoice = sc.nextInt();
if(usersChoice==1)  //No need for brackets if the there's only one action following the if statement
calcKilometers(inputMeters);
if(usersChoice==2)
calcInches(inputMeters);
if(usersChoice==3)
calcFeet(inputMeters);
if(usersChoice==4)
break; //here we break the loop
}

}

System.out.println("1. Convert to kilometers");
System.out.println("2. Convert to inches");
System.out.println("3. Convert to feet");
System.out.println("4. Quit the program");
}

public static void calcKilometers(double inputMeters){
double convertedInput = (inputMeters * .001);
System.out.println(convertedInput);
}

public static void calcInches(double inputMeters){
double convertedInput = (inputMeters * 39.37);
System.out.println(convertedInput);
}

public static void calcFeet(double inputMeters){
double convertedInput = (inputMeters * 3.281);
System.out.println(convertedInput);
}
}

```

Hope this helps!

Thanks for working out the kinks!! I'm reading thru your comments, & comparing my original script now to study what I did wrong. I've been staring at my book for days, but the examples in the book seem to show different ways to structure methods depending on what the script is supposed to do (pass an argument, layer, etc.) & I got really confused. Originally, I declared an extra variable in each method. I see now how & where they are supposed to used. Also, I knew my loop was wrong, but now I understand how to use true/false conditions accompanied by the nifty old break statement-- so thanks for that especially! I wasnt sure I used proper indentation either, so I'm glad to see a more correct, cleaner example. Again, many thanks!!!

This post has been edited by DirtyMartyr: 12 October 2010 - 10:11 AM

### #4 macosxnerd101

• Self-Trained Economist

Reputation: 9025
• Posts: 33,465
• Joined: 27-December 08

## Re: How to use several methods with arguments/condition statements

Posted 12 October 2010 - 10:18 AM

Better practice would be to have the methods return values rather than printing the values. This makes the methods more modular and reusable, as the internal program can utilize the methods to obtain the results rather than just outputting them to the console, where the program can't further use the data.

```public static double milesToFeet(double miles){
return miles*5860;
}

```

### #5 DirtyMartyr

Reputation: -3
• Posts: 12
• Joined: 30-September 10

## Re: How to use several methods with arguments/condition statements

Posted 12 October 2010 - 10:47 AM

macosxnerd101, on 12 October 2010 - 10:18 AM, said:

Better practice would be to have the methods return values rather than printing the values. This makes the methods more modular and reusable, as the internal program can utilize the methods to obtain the results rather than just outputting them to the console, where the program can't further use the data.

```public static double milesToFeet(double miles){
return miles*5860;
}

```

Technically, the assignment asks me to return the values. So, your right, I really need to incorporate this into my script, and correctly-- thanks!

### #6 DirtyMartyr

Reputation: -3
• Posts: 12
• Joined: 30-September 10

## Re: How to use several methods with arguments/condition statements

Posted 14 October 2010 - 11:03 AM

```import java.util.Scanner;

public class BSarahConversion{
public static void main(String[] args){

Scanner sc = new Scanner(System.in);
System.out.print("Please enter distance in meters: ");
double inputMeters = sc.nextDouble();

while(inputMeters<0){
System.out.println("Error: Cannot accept negative value.");
System.out.print("\nPlease enter distance in meters: ");
inputMeters = sc.nextDouble();
if(inputMeters>0)
continue;}

int usersChoice = sc.nextInt();

if(usersChoice<1 || usersChoice>4)

do{
usersChoice = sc.nextInt();

}while(usersChoice<1 || usersChoice>4);

if(usersChoice==1)
calcKilometers(inputMeters);
if(usersChoice==2)
calcInches(inputMeters);
if(usersChoice==3)
calcFeet(inputMeters);
if(usersChoice==4)
System.out.println("Good bye!");
System.exit(4);

do{
usersChoice = sc.nextInt();
}while(usersChoice!=4);

}

System.out.println("\n1. Convert to kilometers");
System.out.println("2. Convert to inches");
System.out.println("3. Convert to feet");
System.out.println("4. Quit the program");
System.out.print("Please make a selection 1-4: ");
}

public static void calcKilometers(double inputMeters){
double convertedInput = (inputMeters * .001);
System.out.println(inputMeters + " meters is " + convertedInput + " kilometers.");
}

public static void calcInches(double inputMeters){
double convertedInput = (inputMeters * 39.37);
System.out.println(inputMeters + " meters is " + convertedInput + " inches.");
}

public static void calcFeet(double inputMeters){
double convertedInput = (inputMeters * 3.281);
System.out.println(inputMeters + " meters is " + convertedInput + " feet.");
}
}

```

I'm still having trouble with my script. After the user selects a menu choice, & after the results are outputted, I need to keep looping back to the menu so that the user can pick another menu selection. I dont need to prompt the user to continue, I just need to automatically loop until the user selects menu option 4, which is to exit. Would it be easier to contain the users menu choice in the menu method? Since I'm supposed to write a menu method that doesnt accept any arguments, I figure that option is out?? My code runs correctly thus far, but my third loop doesnt run as it should. Any help would be much appreciated.