Hello....First of all if you get an Exception as you did when running
a program something is not ok.....Your problem comes from this lines

        int max = 6;

        String [] names = new String [max];
        String [] ages = new String [max];


Basically you are creating 2 arrays: one for names and the second for
ages. The problem is that you already create the arrays with the
maximum allowed number of names and ages and later you use a "for" to
loop these arrays. But if you enter only 3 or four names there will be
2 or 3 aditionally places in the array. For example if you enter 3
names with 3 ages, and you create those arrays with dimension of 6 ,
when you use the "for" loop in the generateName method  "for(int i=0;i
< names.length;i++)" the loop will try to acces the chars at the
positions 3, 4 and 5 which were null. The solution is simple.. Create
the array of the dimension given by the args.length.. Here is the code
which is going to solve your problem:

Instead of the line :

    int max = 6;

 write this one:

   int max = args.length / 2;


I already tried it and it works..... You really complicated this
program...It could be done much more easy and besides that ,the
exercise was to concatenate the characters and create a new name with
them not to display them on new lines... If you want me to show you
how to make it more easy write a reply..I will show you how to do
it..Good luck..


-----------------------------------------------------


On Oct 28, 7:45 pm, "Karen Doyle" <[email protected]>
wrote:
> Hi,
>
> I'm working on the homework for Lab 1011. My program compiles clean and
> the expected results are produced BUT I have extra messages. Can someone
> confirm if they are warning messages or is my code wrong? The results
> are confusing.
>
> Thank You!
>
> Here is the output of my code:
>
> -------------------------------------------------------
>
> These are the argument names:
>
> Trevor
>
> Karen
>
> Jenna
>
> Brooke
>
> The new name for Trevor is r
>
> The new name for Karen is a
>
> The new name for Jenna is e
>
> Exception in thread "main" java.lang.NullPointerException
>
> The new name for Brooke is r
>
>         at BuiltinClasses.generateNewName(BuiltinClasses.java:52)
>
>         at BuiltinClasses.main(BuiltinClasses.java:46)
>
> Java Result: 1
>
> BUILD SUCCESSFUL (total time: 0 seconds)
>
> ---------------------------------------------------------------
>
> Here is my code:
>
> ------------------------------------------------------------------
>
> public class BuiltinClasses {
>
>     /**
>
>      * @param args the command line arguments
>
>      */
>
>     public static void main(String[] args) {
>
>         // Check for correct command line argument
>
>         if((args.length < 6) || (args.length > 12)) {
>
>             System.out.println("Argument is incorrect.");
>
>             System.out.println("Expecting 3 to 6 names and ages.");
>
>             System.out.println("example: Jane 30 Joe 35 Jill 13");
>
>             System.exit(0);
>
>         }
>
>         // Move arguments into an array
>
>         int max = 6;
>
>         int j = 0;
>
>         int k = 0;
>
>         String [] names = new String [max];
>
>         String [] ages = new String [max];
>
>         System.out.println("These are the argument names:");
>
>         for(int i=0;i < args.length;i++){
>
>             if (i==1 | i==3 | i==5 | i==7 | i==9 | i==11) {
>
>                 ages [j] = args[i];
>
>                 j++;
>
>             } else {
>
>                 names [k] = args[i];
>
>                 System.out.println(names[k]);
>
>                 k++;
>
>             }
>
>         }
>
>         // call method to generate new names
>
>         generateNewName(names);
>
>     }
>
>     // Static method to alter name array
>
>     public static void generateNewName(String [] names){
>
>         for(int i=0;i < names.length;i++) {
>
>             char newName = names[i].charAt(1);
>
>             System.out.println("The new name for " + names[i] + " is " +
> newName);
>
>         }
>
>     }
>
> }
>
> --------------------------------------------------------------

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to