Reputation: 87
I am making a program that implements a class file. What I want to do is use an if statement to output a string or just a specific println.
//Daily travel increments.
System.out.println("After one day ");
crossCountry.changeDay(1);
crossCountry.changeDistance(1);
crossCountry.changeLocation(1);
crossCountry.printStates();
What I want to do is print a string based on the "location" number. For example, if the location is 1, then the output for printing the "location" line should be "Dallas Texas" or something to that extent.
A full source can be found at this link.
I'm not sure if this can be done or not but I am looking for some information on how to implement it as is. If this is not possible please let me know my options.
I was thinking to make it simple, considering that there is no user input, I could eliminate the Location declaration from both my main java file and my class file and just add a print statement immediately after the printStates line. Although I would still like to know if there is a way to implement something else based on the location number.
Upvotes: 0
Views: 166
Reputation: 3898
changeDay()
changeDistance()
and changeLocation()
methodsIf
for each location instead use an array
(or any Data structure) to index the current location-
public class JavaApplication33 {
public static void main(String[] args) {
// Create object.
Travel crossCountry = new Travel();
// Initial output.
System.out.println("You decide to travel across the country.");
System.out.println("You start your trip in Augusta, Ga.");
System.out.println("You average 75mph and 12 hours driving per day");
System.out.println("After " + 1 + " day ");
crossCountry.changeDay(1);
crossCountry.changeDistance(1);
crossCountry.changeLocation(1);
crossCountry.printStates();
}
}
class Travel {
int day = 0;
int distance = 0;
int location = 0;
int increment = 900;
String[] locations = {"Augusta", "Portland", "Kentucky", "Chicago", "NY", "Indiana"};
void changeDay(int newValue) {
day += newValue;
}
public void changeLocation(int newValue) {
location += newValue;
}
public void changeDistance(int newValue) {
distance += 75 * 12 * newValue;
}
void printStates() {
System.out.println("Day: " + day + " Distance Driven: " + distance + " Current Location: "
+ locations[location]);
}
}
Upvotes: 1
Reputation: 2835
If they are sequential integers and you want to store them in memory, you can use an array of String to do it, and you can either fill it in declaration or runtime (see how to handle arrays here)
String[] locations = {"Dallas Texas", "Somewhere over the rainbow", "Your momma"};
You can read them by their array position (index starts at zero). This way you'll avoid all of those if then else or switch. Always validate if the array position exists. Assuming that locationId it's the location integer:
System.out.println(locations[locationId]); // print "Dallas Texas"
If they're not contiguous you can use structures that index your elements by their key (in this case it's the location ID), like a HashTable.
Upvotes: 1
Reputation: 850
You can use a switch statement, but if you must use the if statement, then the printStates method could be something like
void printStates()
{
String locationName = "Unknown"; // or some other default location name
if (location == 1)
{
locationName = "somelocation, SL";
}
else if (location == 2)
{
locationName = "thelocation, TL";
}
else if (location == 3)
{
locationName = "otherlocation, OL";
}
System.out.println("Day: " + day + "Distance Driven: " + distance +
"Current Location: " + locationName);
}
Upvotes: 1
Reputation: 1018
switch statement
switch (integerValue)
case 1:
System.out.println("output 1");
break;
case 2:
System.out.println("output 2");
break;
default:
System.out.println("wat");
}
Upvotes: 0