Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One DIM array ??? continued

 
Douglas Braxton
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's the ticket
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic