Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Array month/day/hour/min/sec incomplete java code

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy 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:

    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:

    ----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.
    Last edited by mightyking; September 16th, 2011 at 08:53 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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*

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array month/day/hour/min/sec incomplete java code

    Quote Originally Posted by Sean4u View Post
    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.
    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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);

  7. #7
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Array month/day/hour/min/sec incomplete java code

    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.

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array month/day/hour/min/sec incomplete java code

    Quote Originally Posted by Norm View Post
    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?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array month/day/hour/min/sec incomplete java code

    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.

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:

    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:
    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?
    Last edited by mightyking; September 18th, 2011 at 07:11 PM.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array month/day/hour/min/sec incomplete java code

    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
    Last edited by Norm; September 18th, 2011 at 07:25 PM.

  12. The Following User Says Thank You to Norm For This Useful Post:

    mightyking (September 18th, 2011)

  13. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Array month/day/hour/min/sec incomplete java code

    ok so here is the code I got so far:

    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:

    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:

    		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);
    Last edited by mightyking; September 18th, 2011 at 08:49 PM.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array month/day/hour/min/sec incomplete java code

    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

Similar Threads

  1. How much per a month?
    By benglish in forum Totally Off Topic
    Replies: 14
    Last Post: September 23rd, 2013, 04:52 PM
  2. Hire Our Senior Java Developer @ 2200 USD / month
    By citigo in forum Paid Java Projects
    Replies: 0
    Last Post: July 11th, 2011, 09:31 PM
  3. Looking for java developer for 2 month long engagement
    By amitkgupta28 in forum Paid Java Projects
    Replies: 0
    Last Post: April 19th, 2011, 03:23 AM
  4. Tip of the Month
    By helloworld922 in forum The Cafe
    Replies: 6
    Last Post: July 5th, 2010, 02:42 AM

Tags for this Thread