Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes One DIM array ??? continued Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "One DIM array ??? continued" Watch "One DIM array ??? continued" New topic
Author

One DIM array ??? continued

Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
i fear i may not be learning this or maybe you just need to repeat your mistakes a number of times.
ANY SUGGESTIONS?
compiler errors are:
MyArray.java [39:1] unreachable statement
System.out.println(sumVal);
^
MyArray.java [35:1] missing return statement
{
^
2 errors
Errors compiling MyArray.

//START main
import javax.swing.JOptionPane;
import java.text.*;
public class MyDriver
{ public static void main (String[ ] arg)
{ String firstNumber;
String kVal, fVal, minVal, maxVal, medVal;
int x;
firstNumber = JOptionPane.showInputDialog("Enter an integer number:");
try
{ x = Integer.parseInt( firstNumber ); }
catch ( NumberFormatException nfe)
{ x = 0; }
MyArray my = new MyArray( x );
my.fill();
my.Sum();
}//END main
//START method class
public class MyArray
{ private int [ ] a; // int array declaration as private
MyArray ( int x ) { a = new int [ x ]; }
void fill()
{ for (int counter = 0; counter < a.length; counter++)
a[ counter ] = 100 + ( int ) ( Math.random () * 100 );
System.out.println(counter +" " +a[ counter ]); }
}//END method fill
long sumVal = 0;
public long sum()
{
for (int counter = 0; counter < a.length; counter++)
{
return sumVal += a[ counter ];
System.out.println(sumVal);
}
}// end of method sum
} end method class
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
a return statement is like a "close the door - no more"
as written, the for loop will terminate on the first line of the first
iteration, because of the word 'return', and that is why the next line is
'unreachable'.
perhaps you meant to write it like this
Douglas Braxton
Ranch Hand

Joined: Jan 28, 2004
Posts: 36
that's the ticket
 
Consider Paul's rocket mass heater.
 
subject: One DIM array ??? continued
 
Similar Threads
help me with this minor mistake
How to get a method to call a method
Initializing a one dim array ??? need help plz
Array problem
Package Question