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
posted
0
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