1,105,340 Community Members

cannot replace data on repeat

Member Avatar
LdaXy
Light Poster
32 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

i'm extremely new to java, so i'm probably making a mistake somewhere.

anywyas, my program works, however whn the user repeats the data entry, it does not get replaced with new data.

package convhms;

/*HOW THIS WORKS:
 * 
 * the main function captures the user input as a float
 * while checking to make sure that the data stays numeric
 * 
 * then it calls the conversion function,
 * where it takes the data inputed data
 * and converts it down to minutes,hours etc.
 * 
 * finally it outputs the data to the console.
 * 
 * this was designed to show how exception handling works.
 * there is no repeater, so you must recompile it to run it.
 * however i will implement one later.
 * 
 * designed by: A.A.J.Frankland
 * 
 */

import java.text.DecimalFormat; //SO THAT THE PROGRAM DOES NOT TRUNCATE TO E
import java.util.InputMismatchException; //FOR EXCEPTION 
import java.util.Scanner; //FOR SCANNER

public class conv {
	
	
	public static float sec; //VARUABLE TO HOLD DATA TO CONVERT
	public static Scanner Scan = new Scanner(System.in);
	
	public static void main(String [] args)
	{		
		DecimalFormat df = new DecimalFormat("#.####################");//DECIMAL FORMAT
		String yn;
	
		do//DO WHILE LOOP
		{
			try //CODE BLOCK TO TEST
		
			{
				sec = 0;
				System.out.println("enter a value for seconds: ");
				sec = Scan.nextFloat();
			}
		
			catch(InputMismatchException ex)//CATCHES EXCEPTION
			{
				System.out.println("Error: Invalid data! cannot covert or cast a  String to Float!\n");
				System.out.println(ex.getStackTrace() + "\n");
				System.exit(0);
			}
		
			convert.convertfunc();
		
			System.out.println("Seconds: " + convert.seconds + "\n");//OUTPUTS THE DATA
			System.out.println("Minutes: " + df.format(convert.minutes) + "\n");
			System.out.println("Hours: " + df.format(convert.hours) + "\n");
			System.out.println("Days: " + df.format(convert.days) + "\n");
			System.out.println("Weeks: " + df.format(convert.weeks) + "\n");
			System.out.println("Years: " + df.format(convert.years) + "\n");
			System.out.println("Decades: " + df.format(convert.decades) + "\n");
			System.out.println("Milenia: " + df.format(convert.milenia) + "\n\n");
			
			System.out.println("want to convert again? (Yes / No)");
			yn = new Scanner(System.in).nextLine();
			System.out.println("\n");
			
		}
		
		while(yn.indexOf("yes") > -1 || yn.indexOf("y") > -1);
		System.exit(0);
	}
	

}

class convert
{
	public static float seconds = conv.sec;//THIS COLLECTS THE DATA FROM SEC
	public static float minutes;
	public static float hours;
	public static float days;
	public static float weeks;
	public static float years;
	public static float decades;
	public static float milenia;
	
	public static void convertfunc()//THE FUNCTION TO CONVERT THE DATA
	{
		minutes = (float) (seconds / 60);//CASTS THE DOUBLE TO FLOAT.
		hours = minutes / 60;
		days = hours / 24;
		weeks = days / 7;
		years = weeks / 52;
		decades = years / 10;
		milenia = decades / 100;
	}
}

sample output:

enter a value for seconds:
62762
Seconds: 62762.0

Minutes: 1046.0333251953125

Hours: 17.433889389038086

Days: 0.7264120578765869

Weeks: 0.10377315431833267

Years: 0.00199563754722476

Decades: 0.00019956375763285905

Milenia: 0.00000199563760361343


want to convert again? (Yes / No)
y


enter a value for seconds:
28383
Seconds: 62762.0

Minutes: 1046.0333251953125

Hours: 17.433889389038086

Days: 0.7264120578765869

Weeks: 0.10377315431833267

Years: 0.00199563754722476

Decades: 0.00019956375763285905

Milenia: 0.00000199563760361343


want to convert again? (Yes / No)
y


ideas?

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

The problem is when you run convert for the first time it initialises its variables, including public static float seconds = conv.sec;//THIS COLLECTS THE DATA FROM SEC but because that var is static the initialisation is done once only, and any subsequent changes to conv.sec are not copied over. In fact it's pure luck that it wasn't done much earlier, before the user has input anything at all.

Instead of making a copy of conv.sec it would be better just to use conv.sec directly in your method.

ps: I guess you are using shared static variables like that because that's all you've been taught so far - it's not the best way to share data, but I'm sure you'll be taught better ways later.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: