hi everyone
i need help how to
-creates an array for 5 integers and store it in array
-use the highest & lowest integer and cube it

i stuck in array, i confuse how to input integers to array with JOptionPane

so far for the input

import javax.swing.*;
import java.text.DecimalFormat;
import java.util.Arrays;

public class WS06
{
public static void main (String[]args)
{
String numStr; int num=0;
int[] number = new int[5];
Arrays.sort(number);
int maximum=number[0];
do
{
    numStr = JOptionPane.showInputDialog ("How many numbers do you want to enter"); //reprompt if the input is not integer
    try
    {
        num = Integer.parseInt(numStr);
    }
    catch (NumberFormatException e)
    {
        JOptionPane.showInputDialog("How many numbers do you want to enter"); 
    }
}
while (num <1 || num > 10);

for (int i = 0; i < number.length(); i++) // i get problem here <<<<<<<
 {
    number[i] = JOptionPane.showInputDialog("Enter a number between 1 and 100"); //store the input to array
 }

for (int i=1; i<number.length; i++) //read trough the array and save the highest integer
    {
        if (number[i] > maximum)
        {
            maximum = number[i];   
        }
    }


}
}

hi everyone
i need help how to
-creates an array for 5 integers and store it in array
-use the highest & lowest integer and cube it

i stuck in array, i confuse how to input integers to array with JOptionPane

so far for the input

import javax.swing.*;
import java.text.DecimalFormat;
import java.util.Arrays;

public class WS06
{
public static void main (String[]args)
{
String numStr; int num=0;
int[] number = new int[5];
Arrays.sort(number);
int maximum=number[0];
do
{
    numStr = JOptionPane.showInputDialog ("How many numbers do you want to enter"); //reprompt if the input is not integer
    try
    {
        num = Integer.parseInt(numStr);
    }
    catch (NumberFormatException e)
    {
        JOptionPane.showInputDialog("How many numbers do you want to enter"); 
    }
}
while (num <1 || num > 10);

for (int i = 0; i < number.length(); i++) // i get problem here <<<<<<<
 {
    number[i] = JOptionPane.showInputDialog("Enter a number between 1 and 100"); //store the input to array
 }

for (int i=1; i<number.length; i++) //read trough the array and save the highest integer
    {
        if (number[i] > maximum)
        {
            maximum = number[i];   
        }
    }


}
}
    This is part of a program I dev that used 6 integers its stupidly close to what you are doing, by the way your class name is in an  interesting format, my instructor would mark points off for that (ie. Wso6). Try using the math.pow function for your cubed issue. My code does sum and average. Hope it helps you and thanks for making an effort instead of just posting your problem.

import java.util.*;
import javax.swing.*;
public class Lab6
{
public static void main(String[] args)
{
double ave, sum = 0;
int minV, maxV;
int[] userVal = new int[6];
//Minimum
for(int i=0; i<userVal.length; i++)
{
String answer = JOptionPane.showInputDialog("Enter an integer");
userVal= Integer.parseInt(answer);
sum=sum+userVal;
System.out.println("The sum of the array is: "+sum);
ave = (double)sum/userVal.length;
System.out.println("The average of the array is: "+ave);
minV = userVal[0];
for(int i=1; i<userVal.length; i++)
if(userVal < minV)
minV=userVal;
System.out.println("Minimum value: " + minV);
//Maximum
maxV = userVal[0];
for(int i=1; i<userVal.length; i++)
if(userVal > maxV)
maxV=userVal;
System.out.println("Maximum value: " + maxV);
Arrays.sort(userVal);
for(int i=0; i<userVal.length; i++)
{

System.out.print(" "+userVal);
}

}

Jamesonh20

This is part of a program I dev that used 6 integers its stupidly close to what you are doing, by the way your class name is in an interesting format, my instructor would mark points off for that (ie. Wso6). Try using the math.pow function for your cubed issue. My code does sum and average. Hope it helps you and thanks for making an effort instead of just posting your problem.

import java.util.*;
import javax.swing.*; 
public class Lab6
{
public static void main(String[] args)
{
double ave, sum = 0;
int minV, maxV;
int[] userVal = new int[6]; 
//Minimum
for(int i=0; i<userVal.length; i++)
{ 
String answer = JOptionPane.showInputDialog("Enter an integer");
userVal[i]= Integer.parseInt(answer); 
sum=sum+userVal[i];
System.out.println("The sum of the array is: "+sum);
ave = (double)sum/userVal.length;
System.out.println("The average of the array is: "+ave);
minV = userVal[0];
for(int i=1; i<userVal.length; i++)
if(userVal[i] < minV)
minV=userVal[i];
System.out.println("Minimum value: " + minV);
//Maximum
maxV = userVal[0];
for(int i=1; i<userVal.length; i++)
if(userVal[i] > maxV)
maxV=userVal[i];
System.out.println("Maximum value: " + maxV); 
Arrays.sort(userVal);
for(int i=0; i<userVal.length; i++)
{

System.out.print(" "+userVal[i]);
}

}

Sorry bout that reply ;)

grrrr... this thread is not posting my code right, I'm sure it's just me but you get what I'm trying to do. Everytime I copy past directly from BlueJ I get the same issues (ie leaving out {} for some reason).

Jamesonh20

for (int i = 0; i < number.length(); i++) // i get problem here

remove "()". ;)

{
    number[i] = JOptionPane.showInputDialog("Enter a number between 1 and 100"); //store the input to array
 }

Add Integer.parseInt. :)

number[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 100"));

last problem
how to get the Highest and Lowest from array and calculate it together?

the code only calculate & display the maximum only
can't display the minimum and the result of calculation

int max=number[0];
int min=number[1];
double Cubed;
double Squared;

for (int i=1; i<number.length; i++) //read trough the array and save the highest integer
    {
        if (number[i] > max)
        {
            max = number[i];   
        }
    }

    
for (int i=1; i<number.length; i++) //read trough the array and save the highest integer
    {
        if (number[i] < min)
        {
            min = number[i];   
        }
    }

Cubed = max*max*max;
Squared = min*min;

JOptionPane.showMessageDialog(null,min+ "Squared is " +Squared + "\n"+max+" Cubed is " +Cubed );
int min=number[2147483647]

:-O

what's the problem with >> int min ??
so far i know that the code only read the first array after sort it, how to set the second array as minimum/lowest integer?

int max=number[0];
int min=number[1];

what's the problem with >> int min ??
so far i know that the code only read the first array after sort it, how to set the second array as minimum/lowest integer?

int max=number[0];
int min=number[1];

Lets assume that the inputted numbers are 5,10,15,7 and 0.

This is your code:

int min=number[1];

        if (number[i] < min)
        {
            min = number[i];   
        }

if(5<1)
if(10<1)
if(15<1)
if(7<1)

It does nothing cause the statement is false

if(0<1)

Because 0 is less than 1 the statement is true so
min=0;

number[i] = JOptionPane.showInputDialog("Enter a number between 1 and 100");

Since your program asks for a number between 1 and 100 , the value of your min should be 100 or greater.

int min=number[100];
int min=number[100];

number is for array

so after input 5 times (ie 11 12 13 14 15)
the array should be
number[0]=11
number[1]=12
number[2]=13
number[3]=14
number[4]=15

after sort it
number[0]=15
number[1]=11
number[2]=12
number[3]=13
number[4]=14

but my code only calculate and display number[0] or first array
second array or number[1] displayed as 0

import javax.swing.*;
import java.text.DecimalFormat;
import java.util.Arrays;

public class WS06
{
public static void main (String[]args)
{
String numStr; int num=0;
int[] number = new int[5];
Arrays.sort(number);
int max=number[0];
int min=number[1]; [U]//<<problem [/U]
double Cubed1;
double Squared1;
double Cubed2;
double Squared2;
do
{
    numStr = JOptionPane.showInputDialog ("How many numbers do you want to enter"); //reprompt if the input is not integer
    try
    {
        num = Integer.parseInt(numStr);
    }
    catch (NumberFormatException e)
    {
        JOptionPane.showInputDialog("How many numbers do you want to enter"); 
    }
}
while (num <1 || num > 10);


for (int i = 0; i < number.length; i++)
{
number[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter a number between 1 and 100"));
}


for (int i=1; i<number.length; i++) //read trough the array and save the highest integer
    {
        if (number[i] > max)
        {
            max = number[i];   
        }
    }

for (int i=1; i<number.length; i++) //read trough the array and save the lowest integer
    {
        if (number[i] < min) [U]//<<problem [/U]
        {
            min = number[i];   [U]//<<problem [/U]
        }
    }


Cubed1 = max*max*max;
Cubed2 = min*min*min;

Squared1 = max*max;
Squared2 = min*min;

JOptionPane.showMessageDialog(null,max+ "Squared is " +Squared1 + "\n"+max+" Cubed is " +Cubed1 );

JOptionPane.showMessageDialog(null,min+ "Squared is " +Squared2 + "\n"+min+" Cubed is " +Cubed2 );

}
}

the first display in max was successful
but on second display in min...square and cube was set to 0 ( i never input 0 to my array )

int max=0;
int min=100;
double Cubed;
double Squared;

for (int i=0; i<number.length; i++) //read trough the array and save the highest integer
    {
        if (number[i] > max)
        {
            max = number[i];   
        }
    }

    
for (int i=0; i<number.length; i++) //read trough the array and save the highest integer
    {
        if (number[i] < min)
        {
            min = number[i];   
        }
    }

here

solved thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.