12 Replies Latest reply: Feb 19, 2014 8:51 AM by rp0428 RSS

    Question on a java program.

    Sankalp

      Question : Here: http://kishorepandit.com/Downloads/Papers/ISCPrac2004KP.pdf . See question 3.

       

      Attempt :

       

      import java.io.*;

      class Login

      {

      int n,id;

      String lg,lgo,di,do1,s[];

      void inputData()throws IOException

      {

      DataInputStream k=new DataInputStream(System.in);

      System.out.print("Enter the number of users :");

      n=Integer.parseInt(k.readLine());

      int i=0;

      s=new String[n];

      for(i=0;i<n;++i)

      {

      System.out.print("USER IDENTIFICATION :");

      id=Integer.parseInt(k.readLine());

      System.out.print("LOGIN TIME :");

      lg=k.readLine();

      System.out.print("LOGIN DATE :");

      lgo=k.readLine();

      System.out.print("LOGOUT TIME :");

      lgo=k.readLine();

      System.out.print("LOGOUT DATE :");

      do1=k.readLine();

      s[i]=id+"            "+lg+"  "+di+"          "+lgo+"  "+do1;

      System.out.println();

      }

      }

      void artillaryDepot()

      {

      int h1=0,h2=0,min1=0,min2=0,d1=0,d2=0,m1=0,m2=0,c=0,i=0,j=0,durmin=0,durhr=0,p=0,t=0;

      int d[]=new int[n];

      int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

      int b[]={0,31,29,31,30,31,30,31,31,30,31,30,31};

      for(i=0;i<n;++i)

      {

      for(j=0;j<s[i].length();++i)

      {

      if(s[i].charAt(j)=='.' || s[i].charAt(j)=='-' || s[i].charAt(j)==':' || s[i].charAt(j)=='|')

      {

      ++c;

      if(c==1)

      {

      h1=Integer.parseInt(s[i].substring(j-2,j));

      min1=Integer.parseInt(s[i].substring(j+1,j+3));

      }

      if(c==2)

      {

      d1=Integer.parseInt(s[i].substring(j-2,j));

      m1=Integer.parseInt(s[i].substring(j+1,j+3));

      }

      if(c==3)

      {

      h2=Integer.parseInt(s[i].substring(j-2,j));

      min2=Integer.parseInt(s[i].substring(j+1,j+3));

      }

      if(c==4)

      {

      d2=Integer.parseInt(s[i].substring(j-2,j));

      m2=Integer.parseInt(s[i].substring(j+1));

      }

      }

      }

      if(m1!=m2 || d2-d1>2 || h1>=24 || h2>=24 || h1<0 || h2<0 || min1>=60 || min2>=60 || min1<0 || min2<0 || d1>a[m1] || d2>a[m2] || d1>b[m1] || d2>b[m2] || d1<0 || d2<0 )

      {

      System.out.println("Invalid and out of range!");

      ++p;

      break;

      }

      if(d1==d2)

      {

      if(min2>min1)

      {

      durmin=min2-min1;

      durhr=h2-h1;

      }

      else

      {

      durmin=min2+60-min1;

      durhr=h2-h1-1;

      }

      }

      if(d2-d1==1)

      {

      if(min1>0)

      {

      durmin=(60-min1)+min2;

      durhr=h1+h2-1;

      if(durmin>60)

      {

      durmin=durmin-60;

      durhr=durhr+1;

      }

      }

      else

      {

      durmin=min2;

      durhr=h1+h2;

      }

      }

      else

      {

      if(min1>0)

      {

      durmin=(60-min1)+min2;

      durhr=h1+h2-23;

      if(durmin>60)

      {

      durmin=durmin-60;

      durhr=durhr+1;

      }

      }

      else

      {

      durmin=min2;

      durhr=h1+h2+24;

      }

      }

      s[i]=s[i]+"          "+durhr+":"+durmin;

      d[i]=durhr*60+durmin;

      if(d[i]>t)t=d[i];

      d1=0;

      d2=0;

      m1=0;

      m2=0;

      min1=0;

      min2=0;

      h1=0;

      h2=0;

      durmin=0;

      durhr=0;

      c=0;

      }

      if(p==0)

      {

        System.out.println("USER IDENTIFICATION          LOGIN TIME & DATE              LOGOUT TIME & DATE              DURATION HOURS:MINUTES");

        for(i=0;i<n;++i)

      {

        System.out.println(s[i]);

      }

      for(i=0;i<n;++i)

      {

        if(d[i]==t)

        {

        System.out.println("The user who logged in for the longest duration :-");

        System.out.println(s[i]);

      break;

      }

      }

      }

      }

      public static void main(String[]args)throws IOException

      {

        Login l=new Login();

        l.inputData();

        l.artillaryDepot();

      }

      }

       

      Doubts: Why it is giving index out of bounds error ? I cannot find why ? Can you please help me know ? Thanks a lot !!!

        • 1. Re: Question on a java program.
          TPD-Opitz

          Please also post the error message including the stacktrace.

           

          In your code mark the line orccuring first in the stacktrace.

           

          bye

          TPD

          • 2. Re: Question on a java program.
            Sankalp

            Compile: Successful.

            Run:

             

            Output:

             

            Enter the number of users :3

            USER IDENTIFICATION :123

            LOGIN TIME :12:20

            LOGIN DATE :21:11

            LOGOUT TIME :12:30

            LOGOUT DATE :21:11

             

             

             

            USER IDENTIFICATION :173

            LOGIN TIME :12:30

            LOGIN DATE :20-12

            LOGOUT TIME :12:30

            LOGOUT DATE :21-12

             

             

             

            USER IDENTIFICATION :142

            LOGIN TIME :16:20

            LOGIN DATE :20-12

            LOGOUT TIME :16:30

            LOGOUT DATE :20-12

             

             

             

            Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

                    at Login.artillaryDepot(Login.java:37)

                    at Login.main(Login.java:156)

            Press any key to continue . . .

            • 3. Re: Question on a java program.
              TPD-Opitz

              So this is your stacktrace but which is Line 37 in your code?

               

              bye

              TPD

              • 4. Re: Question on a java program.
                Sankalp

                This ?

                 

                h2=Integer.parseInt(s[i].substring(j-2,j));

                • 5. Re: Question on a java program.
                  TPD-Opitz

                  In the sourrounding for loop, what's the biggest value j can have?

                   

                  bye

                  TPD

                  • 6. Re: Question on a java program.
                    rp0428
                    Doubts: Why it is giving index out of bounds error ? I cannot find why ? Can you please help me know ?

                    Why don't you troubleshoot your code like everyone elses does? Run it in DEBUB mode in a GUI like NetBeans and put a breakpoint right before the line where you get the exception.

                     

                    That watch the value of that variable in the 'watch' window as you iterate thru the loop.

                     

                    We don't have any magic answers - you need to trace thru your code and see what it is doing.

                    • 7. Re: Question on a java program.
                      Sankalp

                      Ok, I corrected one error. In j loop, i mistakedly wrote ++i. It should be ++j. But then again it is giving this error :

                       

                      Enter the number of users :1

                      USER IDENTIFICATION :123

                      LOGIN TIME :2:30

                      LOGIN DATE :12|12

                      LOGOUT TIME :5:30

                      LOGOUT DATE :12|12

                       

                       

                       

                      Exception in thread "main" java.lang.NumberFormatException: For input string: "

                      2"

                              at java.lang.NumberFormatException.forInputString(NumberFormatException.

                      java:48)

                              at java.lang.Integer.parseInt(Integer.java:447)

                              at java.lang.Integer.parseInt(Integer.java:497)

                              at Login.artillaryDepot(Login.java:44)

                              at Login.main(Login.java:156)

                      Press any key to continue . . .

                      • 8. Re: Question on a java program.
                        TPD-Opitz

                        Again I'm courious which line in your code is at Login.artillaryDepot(Login.java:44)? (no, I'm not going to count myself).

                         

                        The message tells that the string given to Integer.parseInt() is not parsable. Also in the message is the unparable string itself. I'm not sure if the line brake in this reported string is your actual data but at least it contains some whitespace. Integer.parseInt() doesn't like whitspaces at all. Extracting single wordes or numbers from a string is kind of cumbersome using indexOf() and substring() so I'd suggest to use regular expression wich are explained in JavaAPI of class Pattern.

                         

                        bye

                        TPD

                        • 9. Re: Question on a java program.
                          rp0428
                          Ok, I corrected one error. In j loop, i mistakedly wrote ++i. It should be ++j. But then again it is giving this error :

                          So when you put a breakpoint before that line of code what value do you see in the debugger of the GUI you are using?

                           

                          Post the details.

                          • 10. Re: Question on a java program.
                            Sankalp

                            Ok, forget the error. Just tell me or give me a start up as to how to make the above program. Just help me on the logic of the program. Question is given on the link of the original post.

                             

                            Thanks.

                             

                            Edit : I do not have any debugger. I am just a student, that's all. I use the editor textpad.

                            • 11. Re: Question on a java program.
                              gimbal2

                              I would rather listen to Wizzle if you want to get anywhere; his advice is excellent and the way forward for you. Its frustrating to be stuck, we all know that. Its how you deal with that frustration that will determine if you are going to do this or not.

                               

                              A forum is not a substitute personal instructor, so don't grasp onto the hope that someone is now going to spell out your own assignment for you. If you want more help, go to your teacher. He/she is paid to instruct you.

                              • 12. Re: Question on a java program.
                                rp0428

                                Edit : I do not have any debugger. I am just a student, that's all. I use the editor textpad.

                                That is just an excuse and, in this day of the internet and free downloads, a very poor excuse.

                                 

                                Your first responsibility as a student is to learn HOW to learn.

                                 

                                One of the best ways to learn Java is to actually 'watch it work' by stepping through your code one line at a time so you can actuall see what each line does.

                                 

                                You can download the 'free' NetBeans GUI and use it. There are plenty of tutorials and examples if you need them.