Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
urgent help today Posted by Pete1972 on 19 Mar 2005 at 3:48 PM
I have an assignment due and i cannot get this to work.

public class InitArray
{

public static void main( String args[ ] )
{
// check number of command-line arguments
if ( args.length != 3 )
System.out.println(
"Error: Please re-enter the entire command, including\n" +
"an array size, initial value and increment." );
else
{
// get array size from first command-line argument
int arrayLength = Integer.parseInt( args[ 0 ] );
String args1[] = new String[ arrayLength ]; // create array

// get initial value and increment from command-line argument
double initialValue = Double.parseDouble( args[ 1 ] );
double increment = Double.parseDouble( args[ 2 ] );

double total = 0.0;

for ( String argument : args1 )
total = total + Double.parseDouble( argument ) * initialValue + increment;

System.out.printf( "The sum of the values in the array are %f", total );
}

} // end main

} // end class

i have to use the enhanced for statement to calculate the sum of the array. the values are passed using the command-line. here is the error message that i get.

C:\Java>java InitArray 3 1 5
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at InitArray.main(InitArray.java:32)

any help would be appreciated. thanks.
Report
Re: urgent help today Posted by Vilanye on 20 Mar 2005 at 6:53 PM
Is argument ever initialized to a value?



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.