java - Exporting variables with multiple values? -
import java.util.*; import java.text.*; import java.io.*; public class avgtime { static double hrs1; static double min1; public static void main(string[] args) throws interruptedexception, filenotfoundexception { scanner in = new scanner(system.in); system.out.print("how many times? "); int numoftimes = in.nextint(); double hrtotal = 0; double mintotal = 0; (int = 1; <= numoftimes; i++){ system.out.println("\nenter time in military time notation: "); system.out.print("hour "); double hrs1 = in.nextdouble(); system.out.print("minute "); double min1 = in.nextdouble(); hrtotal += hrs1; mintotal += min1; } //calculate average double avghr1 = hrtotal/numoftimes; double timemin1 = math.round(mintotal/numoftimes); decimalformat df = new decimalformat("###"); string hours = df.format(avghr1); string minutes = df.format(timemin1); string time = hours+":"+minutes+":"+00; string mt = hours+minutes; simpledateformat fmtmil = new simpledateformat("hh:mm:ss"); date indate = fmtmil.parse(time, new parseposition(0)); simpledateformat fmtampm = new simpledateformat("hh:mm:ss aa"); stringbuffer outdate = fmtampm.format(indate, new stringbuffer(), new fieldposition(0)); system.out.println("\nthe average time " + outdate+"\n"+mt+" in military time.\n\n"); string copy = "copying..."; thread.sleep( 550 ); for( int = 0; < copy.length(); i++ ) { system.out.print(copy.charat( ) ); try{ thread.sleep( 105 ); }catch( exception e ){} } printwriter p = new printwriter("times.txt"); p.println(hrs1+min1); p.close(); system.out.println("\ndone\n\n\n"); thread.sleep( 550 ); } }
i want export file times entered user. example if user sets numoftimes 2, have 2 different values hrs1 , min1.
say enter: 1629 , 2018.
i want text file display hrs1 (16) , min1 (29) 1629. instead of 'hrs1+min1' when put hrs1+min1 exports '0.0'. want export "1629" , "2018" .
why , how fix it?
output
how many times? 2 enter time in military time notation: hour 16 minute 29 enter time in military time notation: hour 20 minute 18
what should exported:
1629 2018
there 2 major modifications make code work. first printwriter
must declared , instantiated outside of loop. , calls println
must occur inside of loop. second, double
s must converted string
prior printing , substring appropriately.
scanner in = new scanner(system.in); system.out.print("how many times? "); int numoftimes = in.nextint(); double hrtotal = 0; double mintotal = 0; printwriter p = new printwriter("times.txt"); (int = 1; <= numoftimes; i++) { system.out.println("\nenter time in military time notation: "); system.out.print("hour "); double hrs1 = in.nextdouble(); system.out.print("minute "); double min1 = in.nextdouble(); hrtotal += hrs1; mintotal += min1; string hrs = string.valueof(hrs1); string min = string.valueof(min1); p.println(hrs.substring(0, hrs.indexof(".")) + min.substring(0,min.indexof("."))); } p.close();
before change code adding 2 doubles hrs1
, mins1
using actual arithmetic , using static variables had declared @ top of class.
static double hrs1; static double min1;
you should remove these static doubles since never used. because call println
outside of loop, causing static doubles referenced instead of hrs1
, mins1
declared , assigned inside loop. thought using locals variables declared in loop never in scope.
Comments
Post a Comment