Skip to main content

Need some help using Reflection to list Methods

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
Sonicmixer
Offline
Joined: 2011-11-13
Points: 0

So far I can't get around the java.lang.ClassNotFoundException. I've tried using the names of the sub classes I created and the overall class, but to no avail. Here's what I have so far. I believe the offending code is in the try catch block close to the top.

Any help would be greatly appreciated.

<br />
package csci.homework.pkg2.part.pkg2;







import java.util.Scanner;

import java.lang.reflect.*;





public class CSCIHomework2Part2 {





    public static void main(String[] args) {

      

        CCNumber ccNumber1 = new CCNumber();

        verify ccCheck = new verify(ccNumber1.getCCNumber());

       

        if(&quot;0&quot;.equals(ccCheck.getCCValid1()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid2()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid3()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid4())){

            display ccDisplay0 = new display(ccCheck.getCCNumber(), ccCheck.getCCType());

        }

        if(&quot;1&quot;.equals(ccCheck.getCCValid1()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid2()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid3()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid4())){

            display ccDisplay1 = new display(ccCheck.getCCNumber(), ccCheck.getCCType(), ccCheck.getCCValid1());

        }

        if(&quot;1&quot;.equals(ccCheck.getCCValid1()) &amp;&amp; &quot;2&quot;.equals(ccCheck.getCCValid2()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid3()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid4())){

            display ccDisplay2 = new display(ccCheck.getCCNumber(), ccCheck.getCCType(), ccCheck.getCCValid1(), ccCheck.getCCValid2());

        }

        if(&quot;1&quot;.equals(ccCheck.getCCValid1()) &amp;&amp; &quot;2&quot;.equals(ccCheck.getCCValid2()) &amp;&amp; &quot;3&quot;.equals(ccCheck.getCCValid3()) &amp;&amp; &quot;0&quot;.equals(ccCheck.getCCValid4())){

            display ccDisplay3 = new display(ccCheck.getCCNumber(), ccCheck.getCCType(), ccCheck.getCCValid1(), ccCheck.getCCValid2(), ccCheck.getCCValid3());

        }

        if(&quot;1&quot;.equals(ccCheck.getCCValid1()) &amp;&amp; &quot;2&quot;.equals(ccCheck.getCCValid2()) &amp;&amp; &quot;3&quot;.equals(ccCheck.getCCValid3()) &amp;&amp; &quot;4&quot;.equals(ccCheck.getCCValid4())){

            display ccDisplay4 = new display(ccCheck.getCCNumber(), ccCheck.getCCType(), ccCheck.getCCValid1(), ccCheck.getCCValid2(), ccCheck.getCCValid3(), ccCheck.getCCValid4());

        }

       

       

        try {

           Class c = Class.forName(&quot;CSCIHomework2Part2&quot;);

           Method m[] = c.getDeclaredMethods();

           System.out.println(m[0].toString());

           

         }

         catch (Throwable e) {

            System.err.println(e);

         }

       

       

       

       

    }

   

public static class CCNumber {

            private String ccNum;

   

        public CCNumber(){

       

            Scanner in = new Scanner(System.in);

            System.out.println(&quot;Please enter a valid 15 or 16 digit credit card number.&quot;);

            this.ccNum = in.nextLine();

            }





        public String getCCNumber() {

            return(ccNum);

        }

   

}



   

    public static class verify {

            private String ccNum;

            private String ccType;

            private char[] ccNumArray;

            private String ccValid1 = &quot;0&quot;;

            private String ccValid2 = &quot;0&quot;;

            private String ccValid3 = &quot;0&quot;;

            private String ccValid4 = &quot;0&quot;;

       

        public verify(String string) {

           

            this.ccNum = string;

            ccNumArray = this.ccNum.toCharArray();

           

            if (Array.getLength(ccNumArray) == 16 &amp;&amp; ccNumArray[0] == '4') {

                ccValid1 = &quot;1&quot;;

            } else if(Array.getLength(ccNumArray) == 15 &amp;&amp; ccNumArray[0] == '3' &amp;&amp; (ccNumArray[1] == '4' || ccNumArray[1] == '7')) {

                ccValid1 = &quot;1&quot;;

                ccValid2 = &quot;2&quot;;

            } else if(Array.getLength(ccNumArray) == 16 &amp;&amp; ccNumArray[0] == '5' &amp;&amp; (ccNumArray[1] &gt;= '1' || ccNumArray[1] &lt; '6')) {

                ccValid1 = &quot;1&quot;;

                ccValid2 = &quot;2&quot;;

                ccValid3 = &quot;3&quot;;

            } else if(Array.getLength(ccNumArray) == 16 &amp;&amp; ccNumArray[0] == '6' &amp;&amp; ccNumArray[1] == '5') {

                ccValid1 = &quot;1&quot;;

                ccValid2 = &quot;2&quot;;

                ccValid3 = &quot;3&quot;;

                ccValid4 = &quot;4&quot;;

            } else {

                ccType = null;

               

            }

        

           

           

        }

       

        public String getCCNumber() {

            return(ccNum);

        }

       

        public String getCCType() {

            return(ccType);

        }

       

        public String getCCValid1() {

            return(ccValid1);

        }

       

        public String getCCValid2() {

            return(ccValid2);

        }

       

        public String getCCValid3() {

            return(ccValid3);

        }

       

        public String getCCValid4() {

            return(ccValid4);

        }

       

    }



   

    public static class display {

       

        public display(String ccNum, String ccType){

           

            System.out.println(&quot;Invalid Credit Card Number&quot;);

           

        }

       

        public display(String ccNum, String ccType, String ccValid1){

           

            if (&quot;1&quot;.equals(ccValid1)) {

                System.out.println(&quot;&quot;);

                System.out.println(&quot;&quot;);

                System.out.println(&quot;Credit Card Information:&quot;);

                System.out.println(&quot;Type: Visa&quot;);

                System.out.println(&quot;Number: &quot; + ccNum);

                System.out.println(&quot;Valid: True&quot;);

            }

           

        }

       

        public display(String ccNum, String ccType, String ccValid1, String ccValid2){

           

            if (&quot;1&quot;.equals(ccValid1) &amp;&amp; &quot;2&quot;.equals(ccValid2)) {

                System.out.println(&quot;&quot;);

                System.out.println(&quot;&quot;);

                System.out.println(&quot;Credit Card Information:&quot;);

                System.out.println(&quot;Type: American Express&quot;);

                System.out.println(&quot;Number: &quot; +ccNum );

                System.out.println(&quot;Valid: True&quot;);

            }

           

        }

       

        public display(String ccNum, String ccType, String ccValid1, String ccValid2, String ccValid3){

           

            if (&quot;1&quot;.equals(ccValid1) &amp;&amp; &quot;2&quot;.equals(ccValid2) &amp;&amp; &quot;3&quot;.equals(ccValid3)) {

                System.out.println(&quot;&quot;);

                System.out.println(&quot;&quot;);

                System.out.println(&quot;Credit Card Information:&quot;);

                System.out.println(&quot;Type: Master Card&quot;);

                System.out.println(&quot;Number:&quot; +ccNum );

                System.out.println(&quot;Valid: True&quot;);

            }

           

        }

       

        public display(String ccNum, String ccType, String ccValid1, String ccValid2, String ccValid3, String ccValid4){

           

            if (&quot;1&quot;.equals(ccValid1) &amp;&amp; &quot;2&quot;.equals(ccValid2) &amp;&amp; &quot;3&quot;.equals(ccValid3) &amp;&amp; &quot;4&quot;.equals(ccValid4)) {

                System.out.println(&quot;&quot;);

                System.out.println(&quot;&quot;);

                System.out.println(&quot;Credit Card Information:&quot;);

                System.out.println(&quot;Type:: Discover&quot;);

                System.out.println(&quot;Number: &quot; +ccNum);

                System.out.println(&quot;Valid: True&quot;);

            }

           

        }

       

       

       

       

    }
</p> <p>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Rocco_Casaburo
Offline
Joined: 2011-03-17
Points: 0

Try using the full name of the class:

Class c= Class.forName(&quot;csci.homework.pkg2.part.pkg2.CSCIHomework2Part2&quot;);

...but it seems to me that there is not any public method declared by CSCIHomework2Part2, so what m[0].toString() should retrieve?