Author Topic: need help with program (java)  (Read 4309 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
need help with program (java)
« on: February 24, 2011, 08:30:57 pm »
Ok so for my advance computer science class I've been given a task to write a java program that will take a given amount of hours, minutes, seconds and convert those into seconds. That part was a breeze. Heres the part I cant figure out: I have to also do the reverse which is take a given number of seconds and convert them into hours, minutes, and seconds. I have the first part done but I got the hours and minutes part done on it, but the seconds is all out of whack  :( oh and I have to use dialogue boxes (no problem for me). I would rather have help telling me what I did wrong and give me an explination as to why it did this rather than write me up a new piece of code that I didnt do and turn in as mine. That would not be helpful, that would be cheeting.

I basically take the seconds, divide them by 60 (twice) to get the hours and store that into 'hourint'. Take the hours and re-convert them into seconds and store it into 'hoursecondint'. I take that and minus it from the seconds to get down to the minutes. I take the seconds left, divide them by 60 to get the minutes and store that into 'minuteint'. I then take that, multiply it by 60 then store that into 'minutesecondint'. I take that and minus it from the seconds and supposedly what I get left are the seconds so then I have the seconds, minutes, and hours. The problem is that I get a negative number of seconds :s. I have been testing this when you input 30000 seconds and it should come out to be 8 hours, 20 mins, and 0 seconds (I tested that by hand), but the program says 8 hrs, 20 mins, and -70000 something seconds  ???

here's my code:

Code: [Select]
import javax.swing.JOptionPane;

public class Time
{
public static void main (String[] args)
{
String hour;
String minute;
String second;

hour = JOptionPane.showInputDialog("Enter number of hours:");
minute = JOptionPane.showInputDialog("Enter number of minutes:");
second = JOptionPane.showInputDialog("Enter number of seconds:");
int hourint = Integer.parseInt(hour);
int minuteint = Integer.parseInt(minute);
int secondint = Integer.parseInt(second);
hourint = hourint * 60 * 60;
minuteint = minuteint * 60;
secondint = secondint + minuteint + hourint;
JOptionPane.showMessageDialog(null,"here is your number of seconds:");
JOptionPane.showMessageDialog(null,secondint);

//second half of code: take seconds convert into hours, mins, and seconds.

String secondint2 = JOptionPane.showInputDialog("Enter number of seconds to convert into hours, minutes, and seconds:");
secondint = Integer.parseInt(secondint2);
hourint = secondint / 60;
hourint = hourint / 60;
int hoursecondint = hourint * 60 * 60;
secondint = secondint - hoursecondint;
minuteint = secondint / 60;
int minutesecondint = secondint * 60;
secondint = secondint - minutesecondint;
JOptionPane.showMessageDialog(null,hourint);
JOptionPane.showMessageDialog(null,minuteint);
JOptionPane.showMessageDialog(null,secondint);

}
}
« Last Edit: February 24, 2011, 08:33:12 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: need help with program (java)
« Reply #1 on: February 24, 2011, 09:25:19 pm »
Just do % symbol (Mod).  As in: (Also, I would go with renaming to different variables, but that's just me)
Code: [Select]
int secondint = secondint2 % 60;
secondint2 = (secondint2-secondint)/60;
int minuteint = secondint2 % 60;
secondint2 = (secondint2-minuteint)/60;
int hourint = secondint2;
For 30000, that is:
30000 % 60 =0 seconds
(30000-0)/60=500
500 % 60 = 20 minutes
(500-20)/60 = 8 hours
« Last Edit: February 24, 2011, 09:25:52 pm by graphmastur »

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: need help with program (java)
« Reply #2 on: February 25, 2011, 07:14:56 am »
Alright ill try that then.. although i dont want to since its not my work and I would rather have an explanation :/

but thanks anyhow :)
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: need help with program (java)
« Reply #3 on: February 25, 2011, 06:29:00 pm »
Alright ill try that then.. although i dont want to since its not my work and I would rather have an explanation :/

but thanks anyhow :)
N mod X is the remainder of N divided by X.  N % X is the java way of saying N mod X.

Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: need help with program (java)
« Reply #4 on: February 25, 2011, 06:32:57 pm »
mods can be really helpful sometimes. ;)

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: need help with program (java)
« Reply #5 on: February 25, 2011, 10:35:58 pm »
mods can be really helpful sometimes. ;)
And a pain if you're trying to crack security systems... But I guess useful if you wanted them secure.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: need help with program (java)
« Reply #6 on: March 24, 2011, 05:24:12 pm »
errrk... Ok, reason i am replying to this thread, I have (somehow) managed to get the .java file to compiled code.. I dont know how neither does my teacher ??? Anyhow, I was going back to this, and I have 2 errors:

Code: [Select]
--------------------Configuration: <Default>--------------------
F:\adv. comp sci\New folder\Time.java:27: operator % cannot be applied to java.lang.String,int
secondint = secondint2 % 60;
                       ^
F:\adv. comp sci\New folder\Time.java:29: operator % cannot be applied to java.lang.String,int
minuteint = secondint2 % 60;
                       ^
2 errors

Process completed.

here is my code:
Code: [Select]
import javax.swing.JOptionPane;

public class Time
{
public static void main (String[] args)
{
String hour;
String minute;
String second;

hour = JOptionPane.showInputDialog("Enter number of hours:");
minute = JOptionPane.showInputDialog("Enter number of minutes:");
second = JOptionPane.showInputDialog("Enter number of seconds:");
int hourint = Integer.parseInt(hour);
int minuteint = Integer.parseInt(minute);
int secondint = Integer.parseInt(second);
hourint = hourint * 60 * 60;
minuteint = minuteint * 60;
secondint = secondint + minuteint + hourint;
JOptionPane.showMessageDialog(null,"here is your number of seconds:");
JOptionPane.showMessageDialog(null,secondint);

//second half of code: take seconds convert into hours, mins, and seconds.

String secondint2 = JOptionPane.showInputDialog("Enter number of seconds to convert into hours, minutes, and seconds:");
int secondint3 = Integer.parseInt(secondint2);     
secondint = secondint2 % 60;                              //1st error
secondint3 = (secondint3 - secondint)/60;           
minuteint = secondint2 % 60;                               //2nd error
secondint3 = (secondint3 - minuteint)/60;
hourint = secondint2;
JOptionPane.showMessageDialog(null,hourint);
JOptionPane.showMessageDialog(null,minuteint);
JOptionPane.showMessageDialog(null,secondint);

}
}
« Last Edit: March 24, 2011, 05:27:08 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline merthsoft

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 241
  • Rating: +63/-1
    • View Profile
Re: need help with program (java)
« Reply #7 on: March 24, 2011, 05:27:24 pm »
It's because secondint2 is a string, and it should be an int. Do:
Code: [Select]
int secondint2 = Integer.parseInt(JOptionPane.showInputDialog("Enter number of seconds to convert into hours, minutes, and seconds:"));
« Last Edit: March 24, 2011, 05:29:56 pm by merthsoft »
Shaun

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: need help with program (java)
« Reply #8 on: March 24, 2011, 05:33:32 pm »
yikes! when i try this program it gives me the wrong output for the second part (with the 3000 seconds) (grr I thought i had this, sorry for the confusion)

Code: [Select]
import javax.swing.JOptionPane;

public class Time
{
public static void main (String[] args)
{
String hour;
String minute;
String second;

hour = JOptionPane.showInputDialog("Enter number of hours:");
minute = JOptionPane.showInputDialog("Enter number of minutes:");
second = JOptionPane.showInputDialog("Enter number of seconds:");
int hourint = Integer.parseInt(hour);
int minuteint = Integer.parseInt(minute);
int secondint = Integer.parseInt(second);
hourint = hourint * 60 * 60;
minuteint = minuteint * 60;
secondint = secondint + minuteint + hourint;
JOptionPane.showMessageDialog(null,"here is your number of seconds:");
JOptionPane.showMessageDialog(null,secondint);

//second half of code: take seconds convert into hours, mins, and seconds.

String secondint2 = JOptionPane.showInputDialog("Enter number of seconds to convert into hours, minutes, and seconds:");
int secondint3 = Integer.parseInt(secondint2);
secondint = secondint3 % 60;
secondint3 = (secondint3 - secondint)/60;
minuteint = secondint3 % 60;
secondint3 = (secondint3 - minuteint)/60;
hourint = secondint3;
JOptionPane.showMessageDialog(null,hourint);
JOptionPane.showMessageDialog(null,minuteint);
JOptionPane.showMessageDialog(null,secondint3);

}
}
« Last Edit: March 24, 2011, 05:36:14 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.