# Method Problems

Page 1 of 1

## 9 Replies - 1304 Views - Last Post: 02 May 2007 - 07:42 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=27365&amp;s=a7210e155922e978b2903b2a88ba8b31&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dogz

Reputation: 0
• Posts: 13
• Joined: 15-April 07

# Method Problems

Posted 28 April 2007 - 02:03 AM

The code below is supposed to convert metres to inches, feet or kilometres (whatever the user wishes), by using other methods as well as main. but at the new method headers it wont compile and states "illegal start of expression" I hav tried changing around the header and using different method names but cant seem to figure it out. please help.

```import javax.swing.JOptionPane;
import java.util.Scanner;
import javax.swing.*;

/**
* Using other methods
*
* @author (Simon Orazi)
* @version (26/03/07)
*/
public class Workshop6
{
public static void main(String[] args)
{
int number;		 // number entered by user.
String input;	   // To hold user's input
char repeat;	   // whether user wishs to continue

// create a scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get user to enter a distance in metres.
input = JOptionPane.showInputDialog("Enter a distance in metres");
number = Double.parseDouble(input);
repeat = input.charAt(0);
while (repeat != '0')
{
// prompts user to change value of distance.
input = JOptionPane.showInputDialog("Metres to Convert: " + number
+ " /n 1. Convert to kilometres." +
" /n 2. Convert to inches."
+ " /n 3. Convert to feet." +
" /n Quit Conversion." + "/n" +
{
if (input == '1');
kilometres = showKilometres();
}

if (input == '2');
inches = showInches();
{

if (input == '3');
feet = showFeet();
}
{
if (input == '4');
System.exit(0);
}
}

/**
* The Kilometres method transforms the users input (metres) into kilometres.
*/

public static double showKilometres(kilometre)
{
int kilometre;

// transform number to kilometres.
kilometre = number * 0.001

JOptionPane.showMessageDialog(null, number + " metres = "
+ kilometres + " Kilometres");
}

/**
* The inches method transforms the users input (metres) into inches.
*/

public static void showInches(int inches)
{
int inches;

// transform number to inches.
inches = number * 39.37

JOptionPane.showMessageDialog(null, number + " metres = "
+ inches + " Inches");
}

/**
* The showFeet method transforms the users input (metres) into Feet.
*/

public static void ShowFeet(int feet)
{
int feet;

// transform number to inches.
inches = number * 3.281

JOptionPane.showMessageDialog(null, number + " metres = "
+ feet + " Feet");
}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Method Problems

### #2 vasdueva

Reputation: 15
• Posts: 141
• Joined: 03-April 07

## Re: Method Problems

Posted 28 April 2007 - 06:24 AM

A few things are happening.
One you have semicolons after your if statements, there shouldn't me there. Your parenthesis seem to line up very wrong.

if(condition)
{
code
}

When you pass a variable to a method. You need not declare the same variable name again. Such as inches in th your show inches method.

Was that the only error you recieved? I'm seeing quite a few.

### #3 vasdueva

Reputation: 15
• Posts: 141
• Joined: 03-April 07

## Re: Method Problems

Posted 28 April 2007 - 06:30 AM

A method header/call should look like this
```double methodVariableOne;
int methodVariableTwo;
variableName = methodName(methodVariableOne, methodVariableTwo);

methodName(double var1, int var2)
{
code
}

```

Get the idea, method calls/headers variables must correspond through type.

### #4 dbrine

Reputation: 0
• Posts: 52
• Joined: 25-April 07

## Re: Method Problems

Posted 28 April 2007 - 10:31 PM

I am a newbie but wouldn't a switch statement be earlier?

vasdueva, on 28 Apr, 2007 - 06:30 AM, said:

A method header/call should look like this
```double methodVariableOne;
int methodVariableTwo;
variableName = methodName(methodVariableOne, methodVariableTwo);

methodName(double var1, int var2)
{
code
}

```

Get the idea, method calls/headers variables must correspond through type.

### #5 trickydicky

Reputation: 0
• Posts: 2
• Joined: 01-May 07

## Re: Method Problems

Posted 01 May 2007 - 10:41 PM

I'm doing the exact same program and I have two questions
1) When I get to here...
if (select = 2);
}
showInches(metres);
{
The compiler says "invalid method declaration, return type required"
I can understand this except it works fine for the first if statement..
if (select = 1);
{
showKilometres(metres);
}
If I leave out the inches and feet methods the program works fine.

2) how do I get it to loop back to the question with 4 options if I'm forced to use "void" method types....is it just a matter of adding the input box again at the end of each method.

Let me know if you want to have a look at the whole code I've written.

dbrine, on 28 Apr, 2007 - 10:31 PM, said:

I am a newbie but wouldn't a switch statement be earlier?

vasdueva, on 28 Apr, 2007 - 06:30 AM, said:

A method header/call should look like this
```double methodVariableOne;
int methodVariableTwo;
variableName = methodName(methodVariableOne, methodVariableTwo);

methodName(double var1, int var2)
{
code
}

```

Get the idea, method calls/headers variables must correspond through type.

### #6 nbarten

Reputation: 5
• Posts: 162
• Joined: 30-April 07

## Re: Method Problems

Posted 02 May 2007 - 02:33 AM

Well, i was bored and took also a look at the code. Lots of ';' missed, the IF statements where wrong, methods also wrong... but well, i thought after a while - all clear! But, i still got this error:

illegal start of expression
public void showKilometres(int number)

And this is the method showKilometres:

```
public void showKilometres(int number)
{
int kilometres;
// transform number to kilometres.
kilometres = number * 0.001;

JOptionPane.showMessageDialog(null, number + " metres = "
+ kilometres + " Kilometres");
}

```

I wonder what's still wrong?

This post has been edited by nbarten: 02 May 2007 - 02:33 AM

### #7 dogz

Reputation: 0
• Posts: 13
• Joined: 15-April 07

## Re: Method Problems

Posted 02 May 2007 - 04:50 AM

so far I have been able to overcome the illegal start of expression (check the below code) but I seem to be getting cannot find symbol - Variable metres in the showKilometres method (and prob the rest).

import javax.swing.JOptionPane;
import java.util.Scanner;
import javax.swing.*;

/**
* Using other methods
*
* @author (Simon Orazi)
* @version (26/03/07)
*/
public class Workshop6
{
public static void main(String[] args)
{
int metres; // number entered by user.
String input; // To hold user's input.
char repeat; // whether user wishs to continue.
int convert; // what the user wishs to transfer metres to.

// create a scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// Get user to enter a distance in metres.
input = JOptionPane.showInputDialog("Enter a distance in metres");

metres = Integer.parseInt(input);

repeat = input.charAt(0);
while (repeat != '0')
{
// prompts user to change value of distance.
input = JOptionPane.showInputDialog("Metres to Convert: " + metres
+ " /n 1. Convert to kilometres." +
" /n 2. Convert to inches."
+ " /n 3. Convert to feet." +
" /n Quit Conversion." + "/n" +
convert = Integer.parseInt(input);

if (convert == 1);
showKilometres(metres);

if (convert == 2);
showInches(metres);

if (convert == 3);
showFeet(metres);

if (convert == 4);
System.exit(0);

}
}

/**
* The Kilometres method transforms the users input (metres) into kilometres.
*/

public static void showKilometres(int kilometres)
{

// transform number to kilometres.
kilometres = metres * 0.001;

JOptionPane.showMessageDialog(null, metres + " metres = "
+ kilometres + " Kilometres");
}

/**
* The inches method transforms the users input (metres) into inches.
*/

public static void showInches(int inches)
{
// transform number to inches.
inches = metres * 39.37;

JOptionPane.showMessageDialog(null, metres + " metres = "
+ inches + " Inches");
}

/**
* The showFeet method transforms the users input (metres) into Feet.
*/

public static void showFeet(int feet)
{
// transform number to inches.
inches = metres * 3.281;

JOptionPane.showMessageDialog(null, metres + " metres = "
+ feet + " Feet");

}
}

### #8 nbarten

Reputation: 5
• Posts: 162
• Joined: 30-April 07

## Re: Method Problems

Posted 02 May 2007 - 05:24 AM

dogz, i wonder what java book you are reading, but the IF statements and the functions are still wrong.

* IF-statements have no ';' after it.

The functions:

You call the function with: showKilometres(metres);
That's good. But if we look at the function itself:

public static void showKilometres(int kilometres)
{

// transform number to kilometres.
kilometres = metres * 0.001;

JOptionPane.showMessageDialog(null, metres + " metres = "
+ kilometres + " Kilometres");
}

This is obviously wrong. The integer you give when you call the function will be stored in the integer kilometres, not in metres!

So the 'public static void showKilometres(int kilometres)' must be 'public static void showKilometres(int metres)'.

### #9 dogz

Reputation: 0
• Posts: 13
• Joined: 15-April 07

## Re: Method Problems

Posted 02 May 2007 - 05:32 AM

I have tried that but then it shows the same error but for kilometres

### #10 trickydicky

Reputation: 0
• Posts: 2
• Joined: 01-May 07

## Re: Method Problems

Posted 02 May 2007 - 07:42 PM

Try this Dogz

import javax.swing.JOptionPane;
public class Metres1
{
public static void main(String[] args)

{
String inputOne;
String inputTwo;
double metres;
int select;

do
{
inputOne = JOptionPane.showInputDialog("Enter Distance in Metres");
metres = Double.parseDouble(inputOne);
}
while (metres <= 0);

do
{
inputTwo = JOptionPane.showInputDialog("Metres to convert " + metres +
" \n1.\tConvert to Kilometres" +
" \n2.\tConvert to Inches" +
" \n3.\tConvert to Feet" +
" \n4.\tQuit the Conversion");
select = Integer.parseInt(inputTwo);

if (select == 1)
{
showKilometres(metres);
}
if (select == 2)
{
showInches(metres);
}
if (select == 3)
{
showFeet(metres);
}
if (select == 4)
{
System.exit(0);
}
else if (select >= 5)
{
JOptionPane.showMessageDialog(null,"Invalid Entry");
}
}
while (select != 4);
}

//The kilometres method converts metres into kilometres.

public static void showKilometres(double metres)

{
double kilometre;
kilometre = metres * 0.001;
JOptionPane.showMessageDialog(null, metres + " metres = " + kilometre + " kilometres");
}

public static void showInches(double metres)

{
double inches;
inches = metres * 39.37;
JOptionPane.showMessageDialog(null, metres + " metres = " + inches + " inches");
}

public static void showFeet(double metres)

{
double feet;
feet = metres * 3.281;
JOptionPane.showMessageDialog(null, metres + " metres = " + feet + " feet");

}
}

All that worked for me.

Now I have to get an Array into it with some "for" loops..........with exception checking, this is proving more difficult.