Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Integer.parseInt & a method

Status
Not open for further replies.

jeak

Technical User
Oct 9, 2002
16
0
0
US
I am working on a simple program. I am having problems with the lines beginning with two of the lines. The first problem is the line beginning with int.numEntered. The second problem is with my static method generateStats. I'm not sure how to incorporate a method other than the main method in this and then call it later in the code. Does anyone have any suggestions? Thanks!


import javax.swing.*;


public class Assignment5
{
public static void main(String[] args)
{

int[] newArray = new int[counter];//Initializing array
int counter = 0;//Initializing counter to zero
int arrayLength = 0;//Initializing counter to zero
int sum = 0;//Initializing counter to zero
int numArrayElements = 0;
int average = 0;


String input = JOptionPane.showInputDialog ("Please enter a positive integer (enter 0 to generate statistics): ");


int numEntered = Integer.parseInt(input);


public static generateStats (int sum, counter, int[] newArray){
numArrayElements = counter + 1;
average = sum/numArrayElements;
int[] newArray = new int [numArrayElements];
for (i = 0; i < numArrayElements; i++);{
myArray = newArray;
}
SelectionSort.sort(myArray);
min = myArray[0];
max = myArray[myArray.length() - 1];
}


if (numEntered > 0){//What to do if the input is greater than zero
newArray[counter] = numEntered;
counter ++;
arrayLength = counter + 1;
sum = sum + numEntered;
}

else {
generateStats();
}


JOptionPane.showMessageDialog(
null, &quot;The average is: &quot; + average);

JOptionPane.showMessageDialog(
null, &quot;The minimum is: &quot; + min);

JOptionPane.showMessageDialog(
null, &quot;The maximum is: &quot; + max);

JOptionPane.showMessageDialog(
null, &quot;Click \&quot;OK\&quot; to end program.&quot;);
String junk;
junk = SavitchIn.readLine();//Ending Program

System.exit(0);
}
}
 
Hi,

there is quite a lot of problems in this code, but to try and answer your points, there doesn't seem to be a problem with:
Code:
int numEntered = Integer.parseInt(input);
You should move the static method generateStats to outside the main method in the class itself, you probably want to make it private as well. Also, you are an identifier missing...
Code:
public static generateStats (int sum, counter, int[] newArray){
should be
Code:
public static generateStats (int sum,
[tt]int[/tt]
Code:
 counter, int[] newArray){

You've also got a few un-identified objects (min, max, myArray...), enjoy ! [pc3]

Alistair
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top