Reputation: 45
I'm doing a project with command line and array. A program that converts F -> C and C -> F. Here what I got so far:
public class Implementation
{
public static void main(String[] args)
{
double degree = 0;
String celsius = null;
String fahrenheit;
int n = 0;
String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};
if (degree < 0)
{
n = 0;
}
if (degree >= 0 && degree < 32)
{
n = 1;
}
if (degree >= 32 && degree < 50)
{
n = 2;
}
if (degree >= 50 && degree < 60)
{
n = 3;
}
if (degree >= 60 && degree < 70)
{
n = 4;
}
if (degree >= 70 && degree < 90)
{
n = 5;
}
if (degree >= 90)
{
n = 6;
}
if (args.length != 3)
{
System.out.println("Error! Please try again.");
System.exit(0);
}
else
{
degree = Double.parseDouble(args[0]);
celsius = args[1];
fahrenheit = args[2];
}
switch (celsius)
{
case "c":
System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[n]);
break;
case "f":
System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[n]);
break;
}
}
public static double celsius(double fahrenheitTemperature)
{
return ( 5.0 / 9.0 * ((double) fahrenheitTemperature - 32));
}
public static double fahrenheit(double celsiusTemperature)
{
return ( 9.0 / 5.0 * (double) celsiusTemperature + 32);
}
}
I have problem in Array part. I couldn't get the right array days[] with the specific degree. For example, If I do command line: 100 c f, it supposes to show up 100 Celsius is 212.0 Fahrenheit Cold "Hot". But my program only shows up "Cold", no matter what degree I input.
Upvotes: 0
Views: 67
Reputation: 3275
you set
double degree = 0;
then
String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};
so of course it will result in "Cold" all the time
...
Here is a fully working class:
As you can see in my solution the whole point is to create a function which returns the proper integer
, depending on the degree
you provide as input
.. this function
is called public static int checkDegree(double degree) {
. This way you put the functionality
in a function
and remove it from the main class (which is very problematic the way you had it). this way you call the function as an argument directly, and the function gives you proper days you need for the logic you request
, instead of calling the days[n] directly
, which was causing the troubles you were faced with in the first place...
public class Implementation
{
public static void main(String[] args)
{
double degree=0;
String celsius=null;
String fahrenheit;
int n;
String[] days = {"Very Cold", "Cold", "Mild", "Very Mild", "Warm", "Very Warm", "Hot"};
if (args.length != 3)
{
System.out.println("Error! Please try again.");
System.exit(0);
}
else
{
degree = Double.parseDouble(args[0]);
celsius = args[1];
fahrenheit = args[2];
}
switch (celsius)
{
case "c":
System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[checkDegree(degree)]);
break;
case "f":
System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[checkDegree(degree)]);
break;
}
}
public static double celsius(double fahrenheitTemperature)
{
return ( 5.0 / 9.0 * ((double) fahrenheitTemperature - 32));
}
public static double fahrenheit(double celsiusTemperature)
{
return ( 9.0 / 5.0 * (double) celsiusTemperature + 32);
}
public static int checkDegree(double degree) {
int myReturn = 0;
if (degree < 0)
{
myReturn = 0;
}
if (degree >= 0 && degree < 32)
{
myReturn = 1;
}
if (degree >= 32 && degree < 50)
{
myReturn = 2;
}
if (degree >= 50 && degree < 60)
{
myReturn = 3;
}
if (degree >= 60 && degree < 70)
{
myReturn = 4;
}
if (degree >= 70 && degree < 90)
{
myReturn = 5;
}
if (degree >= 90)
{
myReturn = 6;
}
return myReturn;
}
}
INPUT : java Implementation 100 c f
OUTPUT : 100 Celsius is 212.0 Fahrenheit Hot
Upvotes: 1
Reputation: 1939
Your
double degree = 0;
remains always 0. You change it after your if statements so it is always 0.
You can change this:
switch (celsius)
{
case "c":
System.out.printf("%n%s Celsius is %.5s Fahrenheit %s\n", args[0], fahrenheit(degree), days[n]);
break;
case "f":
System.out.printf("%n%s Fahrenheit is %.5s Celsius %s\n", args[0], celsius(degree), days[n]);
break;
}
to this:
switch (celsius)
{
case "c":
System.out.printf("%n%s Celsius is %.5s Fahrenheit ", args[0], fahrenheit(degree));
break;
case "f":
System.out.printf("%n%s Fahrenheit is %.5s Celsius ", args[0], celsius(degree));
break;
}
and move it just right above your degrees declaration. Move this part too:
if (args.length != 3)
{
System.out.println("Error! Please try again.");
System.exit(0);
}
else
{
degree = Double.parseDouble(args[0]);
celsius = args[1];
fahrenheit = args[2];
}
Then at the end of your main() add this:
System.out.printf("%s\n", days[n]);
That's the quickest fix I can imagine. Of course there are about a million more elegant ways to do what you want but it goes out of this answer's scope. I strongly recommend you to watch some programming tutorials or even better read a book.
Upvotes: 1