Boby Gandhi Boby Gandhi - 1 month ago 12
Java Question

Very simple java program, keep receiving errors.

Ive written a short program that asks the user for a underground station and responds with what zone said station is in. I haven't got all the stations but that doesn't matter, its just to try and learn java.
I'm receiving 4 errors and i'm sure I've maid some obvious mistakes but i cant find them. Any help appreciated.

import java.util.Scanner;

class underground {

public static void main(String[] args) {
String s1 = intro();
int z1 = stations();
zones(s1, z1);

System.exit(0);
} //end main method

public static String intro() {

System.out.println("This program tells you which zone a London Underground station is in.");
Scanner station = new Scanner(System.in);

System.out.println("Which station would you like to know the zone of");

String ustation = station.nextLine();


return ustation;
} // end intro

public static int stations(String ustation) {

int zone;

switch (ustation) {

case "woodford":
zone = 4;
break;

case "barking":
zone = 4;
break;

case "wimbledon":
zone = 3;
break;

case "tooting broadway":
zone = 3;
break;

case "oval":
zone = 2;
break;

case "mile end":
zone = 2;
break;

case "bank":
zone = 1;
break;

case "waterloo":
zone = 1;
break;

default:
zone = 0;
System.out.println("Is " + ustation + " a London Underground station? Maybe check your spelling");
break;

} // end switch statement

return zone;

} // end stations method

public static String zones(String ustation, int zones) {

if (zone == 0) {

} else {
System.out.println(ustation + " is in zone " + zone);
}

} // end zones method

} // end class underground


The errors are:

underground.java:8: error: method stations in class underground cannot be applied to given types;
int z1 = stations();
^
required: String
found: no arguments
reason: actual and formal argument lists differ in length
underground.java:72: error: incompatible types: int cannot be converted to String
return zone;
^
underground.java:78: error: cannot find symbol
if (zone == 0) {
^
symbol: variable zone
location: class underground
underground.java:81: error: cannot find symbol
System.out.println(ustation + " is in zone " + zone);
^
symbol: variable zone
location: class underground

Answer

You're attempting to call:

int z1 = stations();

stations as it is defined requires a string passed in as a parameter, as the error message indicates.

if (zone == 0) {

and

System.out.println(ustation + " is in zone " + zone);

Your method signature:

public static String zones(String ustation, int zones) {...}

Your method has a parameter zones, not zone. If you're using an IDE, all of this information should be pointed out to you.

Eclipse is a free java IDE you could use, I'm sure there are others.