Array month/day/hour/min/sec incomplete java code
Hello all, I was asked by a friend of mine to help him out with a program he needs completed, unlucky for him I have not much experience with it myself.
Wondering how the complete code would look like, or perhaps an easier way to write the whole code for this program.
The idea is that the user inputs x number of months/days/hours/minutes/seconds... and a final array will display all this information in the proper format.
Meaning:
max sec display would be 59 seconds.
Max min display would be 59 minutes.
Max hour display would be 23 hours.
Max day display would be 29 (always assuming every month has 30 days).
Max month display would be 11. If it all happens to exceed the 11 months, then the corresponding number of years would be shown in the array as well.
So here is the code:
Code Java:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class arreglohras {
private String fecha;
private int anio;
private int mes;
private int dia;
private int hora;
private int min;
private int seg;
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public int getAnio() {
return anio;
}
public void setAnio(int anio) {
this.anio = anio;
}
public int getMes() {
return mes;
}
public void setMes(int mes) {
this.mes = mes;
}
public int getDia() {
return dia;
}
public void setDia(int dia) {
this.dia = dia;
}
public int getHora() {
return hora;
}
public void setHora(int hora) {
this.hora = hora;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getSeg() {
return seg;
}
public void setSeg(int seg) {
this.seg = seg;
}
public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] arg) throws Exception {
arreglo cal1 = new arreglo();
arreglo cal2 = new arreglo();
arreglo cal3 = new arreglo();
System.out.println("Introduce seg:min:horas:dias:meses");
cal1.setFecha(in.readLine());
System.out.println("Introduce seg:min:horas:dias:meses");
cal2.setFecha(in.readLine());
StringTokenizer token;
token = new StringTokenizer(cal1.getFecha(), ":");
String uno = token.nextToken();
int numero = Integer.parseInt(uno);
cal1.setSeg(numero);
cal1.setMin(Integer.parseInt(token.nextToken()));
cal1.setHora(Integer.parseInt(token.nextToken()));
cal1.setDia(Integer.parseInt(token.nextToken()));
cal1.setMes(Integer.parseInt(token.nextToken()));
token = new StringTokenizer(cal2.getFecha(), ":");
cal2.setSeg(Integer.parseInt(token.nextToken()));
cal2.setMin(Integer.parseInt(token.nextToken()));
cal2.setHora(Integer.parseInt(token.nextToken()));
cal2.setDia(Integer.parseInt(token.nextToken()));
cal2.setMes(Integer.parseInt(token.nextToken()));
cal3.setSeg(cal1.getSeg() + cal2.getSeg());
cal3.setMin(cal1.getMin() + cal2.getMin() + cal3.getSeg() / 60);
cal3.setHora(cal1.getHora() + cal2.getHora() + cal3.getMin() / 60);
cal3.setDia(cal1.getDia() + cal2.getDia() + cal3.getHora() / 24);
cal3.setMes(cal1.getMes() + cal2.getMes() + cal3.getDia() / 30);
cal3.setAnio(cal3.getMes() / 12);
cal3.setSeg(cal3.getSeg() % 60);
cal3.setMin(cal3.getMin() % 60);
cal3.setHora(cal3.getHora() % 24);
cal3.setDia(cal3.getDia() % 30);
cal3.setMes(cal3.getMes() % 12);
System.out.println("La fecha es:");
System.out.println(cal3.getAnio() + " Años ");
System.out.println(cal3.getMes() + " Meses ");
System.out.println(cal3.getDia() + " Dias ");
System.out.println(cal3.getHora() + " Horas ");
System.out.println(cal3.getMin() + " Minutos ");
System.out.println(cal3.getSeg() + " Segundos ");
}
}
Here is the error message when compiling:
Code Java:
----jGRASP exec: javac -g arreglohras.java
arreglohras.java:61: error: cannot find symbol
arreglo cal1 = new arreglo();
^
symbol: class arreglo
location: class arreglohras
arreglohras.java:61: error: cannot find symbol
arreglo cal1 = new arreglo();
^
symbol: class arreglo
location: class arreglohras
arreglohras.java:62: error: cannot find symbol
arreglo cal2 = new arreglo();
^
symbol: class arreglo
location: class arreglohras
arreglohras.java:62: error: cannot find symbol
arreglo cal2 = new arreglo();
^
symbol: class arreglo
location: class arreglohras
arreglohras.java:63: error: cannot find symbol
arreglo cal3 = new arreglo();
^
symbol: class arreglo
location: class arreglohras
arreglohras.java:63: error: cannot find symbol
arreglo cal3 = new arreglo();
^
symbol: class arreglo
location: class arreglohras
6 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Re: Array month/day/hour/min/sec incomplete java code
The "cannot find symbol" message means that the compiler can not find a definition for the symbol mentioned in the error message.
Where is the class: arreglo defined? The compiler can not find it.
Re: Array month/day/hour/min/sec incomplete java code
I checked the usb he provided me with the code, and arreglo is nowhere.
What should arreglo look like?
*Sorry to bother but a complete code with whatever change would be highly appreciated... I'm not an ace at programing*
Re: Array month/day/hour/min/sec incomplete java code
Are you aware of the web meme "plz email me teh codez"?
plz email me teh codez - The Daily WTF
What is "Do you haz teh codez"? - Server Fault Meta
+1 for asking eloquently, but you should really direct 'your friend' to make an honest attempt at solving the problem for herself and posting their genuine attempt here for help and advice in the event that it fails.
Re: Array month/day/hour/min/sec incomplete java code
Quote:
Originally Posted by
Sean4u
I'm sorry, its just I'm only trying to help out, sadly only way I can truely help out is by getting help.
I would like to pm you about a doubt, just in case I shouldn't mention it here.
Re: Array month/day/hour/min/sec incomplete java code
What should arreglo look like?
It should be defined as a class:
class arreglo {
//?????
} // end of class
And it needs to have all the methods that are used in the code:
cal3.setSeg(cal3.getSeg() % 60);
cal3.setMin(cal3.getMin() % 60);
cal3.setHora(cal3.getHora() % 24);
cal3.setDia(cal3.getDia() % 30);
cal3.setMes(cal3.getMes() % 12);
Re: Array month/day/hour/min/sec incomplete java code
Quote:
sadly only way I can truely help out is by getting help
... your friend is learning to program, but is unable to post a question on the Internet?
Norm's help posted #6 is a good mini-guide to what must be done. Read in conjunction with Oracle's tutorial:
Declaring Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
... it'll give your friend a reasonably complete guide to how to begin implementing the arreglo class.
I posted the two links above as an example of how counter-productive it can be to employ a "give me the code" strategy. If your friend is learning programming, the best way you could help her would be to suggest that she post her own code and questions.
Re: Array month/day/hour/min/sec incomplete java code
Quote:
Originally Posted by
Norm
What should arreglo look like?
It should be defined as a class:
class arreglo {
//?????
} // end of class
And it needs to have all the methods that are used in the code:
cal3.setSeg(cal3.getSeg() % 60);
cal3.setMin(cal3.getMin() % 60);
cal3.setHora(cal3.getHora() % 24);
cal3.setDia(cal3.getDia() % 30);
cal3.setMes(cal3.getMes() % 12);
so basically is the rest of the coding correct? only thing left to implemente is the public class arreglo{ right?
Re: Array month/day/hour/min/sec incomplete java code
Quote:
is the rest of the coding correct?
There is no easy way to tell until you get it all together.
You will be able to tell when you get a definition for that class and can compile the program and exectute it and have it do what you want.
Re: Array month/day/hour/min/sec incomplete java code
Ok based on the fact I practically didn't understand most of the code in the other program, I've decided to start a new one.
Here is the code I have done so far:
Code Java:
import java.io.*;
public class apples {
public static void main(String[] args) throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader (System.in));
int array[]=new int[6];
int year,month,day,hr,min,sec;
int a,m,d,h,mn,s;
String year1,month1,day1,hr1,min1,sec1;
/* StringTokenizer token;
token=new StringTokenizer(date,":");
int cad1=Integer.parseInt(token.nextToken());
int cad2=Integer.parseInt(token.nextToken());
int cad3=Integer.parseInt(token.nextToken());
int cad4=Integer.parseInt(token.nextToken());
int cad5=Integer.parseInt(token.nextToken());
int cad6=Integer.parseInt(token.nextToken());
System.out.println("Introduce sec:min:hour:day:month:year");
cal1.setFecha(in.readLine());
*/
System.out.print("Introduce the number of years: ");
year = Integer.parseInt(year1 = in.readLine());
System.out.print("Introduce the number of months: ");
month = Integer.parseInt(month1 = in.readLine());
System.out.print("Introduce the number of days: ");
day = Integer.parseInt(day1 = in.readLine());
System.out.print("Introduce the number of hours: ");
hr = Integer.parseInt(hr1 = in.readLine());
System.out.print("Introduce the number of minutes: ");
min = Integer.parseInt(min1 = in.readLine());
System.out.print("Introduce the number of seconds: ");
sec = Integer.parseInt(sec1 = in.readLine());
System.out.println("Time\tAmmount");
for(int time=0;time<array.length;time++){
System.out.println(time+"\t"+array[time]);
}
}
}
Output:
Code Java:
Introduce the number of years: 50
Introduce the number of months: 11
Introduce the number of days: 8
Introduce the number of hours: 10
Introduce the number of minutes: 2
Introduce the number of seconds: 25
Time Ammount
0 0
1 0
2 0
3 0
4 0
5 0
I thought up of two ways of getting the user to input the specified information into the arrays, one is by the token part and another is simply asking for each single piece and storing them into variables.
I'm having trouble since this is my first time ever doing arrays.. First of all I would want to have the output display a table, under Time there would be: years,months,days,hours,minutes,seconds.
As you can see it will display from 0 to 5.. I find that obvious. And second column displays always 0.
How do I go about having the first column display strings for years,months,days,hours,minutes,seconds... and how do i get the array to order the variables instead of getting 0's?
Re: Array month/day/hour/min/sec incomplete java code
Quote:
how do i get the array to order the variables instead of getting 0's?
You must assign values to each slot in the array:
array[ix] = aValue; // set element ix to aValue
You should Try reading the tutorial.
Go to this site: http://download.oracle.com/javase/tu.../java/TOC.html
and Find array
Re: Array month/day/hour/min/sec incomplete java code
ok so here is the code I got so far:
Code Java:
import java.io.*;
public class apples {
public static void main(String[] args) throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader (System.in));
int array[]=new int[6];
int year,month,day,hr,min,sec;
int a=0,mo=12,d=30,h=24,mi=60,s=60;
String year1,month1,day1,hr1,min1,sec1;
System.out.print("Introduce the number of seconds: "); // 168
sec = Integer.parseInt(sec1 = in.readLine());
System.out.print("Introduce the number of minutes: "); // 190
min = Integer.parseInt(min1 = in.readLine());
System.out.print("Introduce the number of hours: "); // 50
hr = Integer.parseInt(hr1 = in.readLine());
System.out.print("Introduce the number of days: "); // 81
day = Integer.parseInt(day1 = in.readLine());
System.out.print("Introduce the number of months: "); // 23
month = Integer.parseInt(month1 = in.readLine());
System.out.print("Introduce the number of years: "); // 5
year = Integer.parseInt(year1 = in.readLine());
int secA = sec/s;
int secB = sec-(secA*s);
/* System.out.println("min "+secA);
System.out.println("sec "+secB);*/
int minA = (min+secA)/mi;
int minB = (min+secA)-(minA*mi);
/* System.out.println("hour "+minA);
System.out.println("minute "+minB);*/
int hrA = (hr+minA)/h;
int hrB = (hr+minA)-(hrA*h);
/* System.out.println("days "+hrA);
System.out.println("hour "+hrB);*/
int dayA = (day+hrA)/d;
int dayB = (day+hrA) -(dayA*d);
/* System.out.println("month "+dayA);
System.out.println("day "+dayB);*/
int monthA = (month+dayA)/mo;
int monthB = (month+dayA)-(monthA*mo);
/* System.out.println("year "+monthA);
System.out.println("month "+monthB);*/
int yearA = year+monthA;
/* System.out.println("years "yearA);*/
String[][] title = {{"Time ", "Time "}, {"Unit", "Duration"}};
System.out.println(title[0][0] + title[1][0]+"\t"+ title[0][1] + title[1][1]);
String[][] Units = {{"Seconds", "Minutes", "Hours"}, {"Days", "Months", "Years"}};
System.out.println(Units[0][0]+"\t"+secB);
System.out.println(Units[1][0]+"\t"+minB);
System.out.println(Units[2][0]+"\t"+hrB);
System.out.println(Units[0][1]+"\t"+dayB);
System.out.println(Units[0][2]+"\t"+monthB);
System.out.println(Units[0][3]+"\t"+yearA);
System.out.println("years: "+yearA+" months: "+monthB+" days: "+dayB+" hours: "+hrB+" minutes: "+minB+" seconds: "+secB);
}
}
the error/output is the following:
Code Java:
Introduce the number of seconds: 168
Introduce the number of minutes: 190
Introduce the number of hours: 50
Introduce the number of days: 81
Introduce the number of months: 23
Introduce the number of years: 5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at apples.main(apples.java:52)
Time Unit Time Duration
Seconds 48
Days 12
I'm confident there are easier ways to putting all this code... something more compact, but unfortunately I lack that knowledge, so rewarding the current structure of my code, how should I go about fixing this error?
-----------------
Fixed... sorry it's really late here and I just noticed my silly mistaken, here is the correction:
Code Java:
String[][] title = {{"Time ", "Time "}, {"Unit", "Duration"}};
System.out.println(title[0][0] + title[1][0]+"\t"+ title[0][1] + title[1][1]);
String[][] Units = {{"Seconds", "Minutes", "Hours"}, {"Days", "Months", "Years"}};
System.out.println(Units[0][0]+"\t"+secB);
System.out.println(Units[0][1]+"\t"+minB);
System.out.println(Units[0][2]+"\t"+hrB);
System.out.println(Units[1][0]+"\t"+dayB);
System.out.println(Units[1][1]+"\t"+monthB);
System.out.println(Units[1][2]+"\t"+yearA);
Re: Array month/day/hour/min/sec incomplete java code
Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at apples.main(apples.java:52)
At line 52 you have a index value that is past the end for the array on that line.
The array has less than 3 elements.
Check you logic to see how the index gets to the value of 2.
Remember that the max index for an array of two elements is 1. The indexes are 0 & 1