Welcome to Dream.In.Code
Become a Java Expert!

Join 150,429 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,079 people online right now. Registration is fast and FREE... Join Now!




Printing a java array

 
Reply to this topicStart new topic

Printing a java array, max and min print but not the array

chachejave
16 Apr, 2008 - 12:33 PM
Post #1

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 4

Here is my code for a homework assignment to work with an array of random numbers. I am using JCreator. The file compiles just fine. But when I execute, I only get the min and max. I also want to print the array. (It printed fine until I added the search for the min and max. Any help will be appreciated.

import java.util.*;
import javax.swing.*;


public class HoorayArray


{
public static void main(String [] args){
int maxcrazy = Integer.parseInt(JOptionPane.showInputDialog("Enter an integer between 50 and 500"));
//if
//crazy>=50;
//crazy<=500;{
int maxmax = 0;
int minmin = maxcrazy;
int Arr1[] = new int[10];
Random gen1 = new Random();
for (int crazy=0; crazy<10; crazy++)
{
Arr1[crazy] = gen1.nextInt(maxcrazy);

System.out.println("Number ["+crazy +"] is " + Arr1[crazy ]);
{

//find min and max

for (crazy = 0; crazy < Arr1.length; crazy++)
if (maxmax < Arr1[crazy])
maxmax = Arr1[crazy];
System.out.println("The maximum value is " + maxmax);

for (crazy = 0; crazy < Arr1.length; crazy++)
if (minmin > Arr1[crazy])
minmin = Arr1[crazy];
System.out.println("The minimum value is " + minmin);


}
//}
//else
//{
//JOptionPane.showMessageDialog(null,"Let's keep it simple. /nEnter an integer between 50 and 500", "Message", JOptionPane.PLAIN_MESSAGE);
//}
}

}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Printing A Java Array
16 Apr, 2008 - 02:47 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,594



Thanked: 233 times
Dream Kudos: 75
My Contributions
Please paste your code like this: code.gif

why do people pass by Anchorage to go from LA to Vegas ?

java

import java.util.*;
import javax.swing.*;


public class HoorayArray


{
public static void main(String [] args){
int maxcrazy = Integer.parseInt(JOptionPane.showInputDialog("Enter an integer between 50 and 500"));
int maxmax = 0;
int minmin = maxcrazy;
// now if you prompt the user for a number maybe it will be a good idea to use that number to create the array
// int Arr1[] = new int[10];
int Arr1[] = new int[maxcrazy];
Random gen1 = new Random();
// for (int crazy=0; crazy<10; crazy++)
// init all elements of the array
// find if new max and/or new min
for (int crazy=0; crazy<maxcrazy; crazy++)
{
Arr1[crazy] = gen1.nextInt(maxcrazy);
System.out.println("Number ["+crazy +"] is " + Arr1[crazy ]);

//find min and max
if (maxmax < Arr1[crazy])
maxmax = Arr1[crazy];
if (minmin > Arr1[crazy])
minmin = Arr1[crazy];

}
System.out.println("Max: " + maxmax + " Min: " + minmin);
}

}


User is online!Profile CardPM
+Quote Post

m2s87
RE: Printing A Java Array
16 Apr, 2008 - 03:29 PM
Post #3

D.I.C Regular
Group Icon

Joined: 28 Nov, 2006
Posts: 390



Thanked: 3 times
Dream Kudos: 1225
My Contributions
I would only say - you have a problem with validation. With a little fix your code would look like:

java

import java.util.Random;
import javax.swing.JOptionPane;

public class hooray {
private static final int max = 500;
private static final int min = 50;

public static void main(String[] args) {
int
maxcrazy = Integer.parseInt(
JOptionPane.showInputDialog("Enter an integer between 50 and 500")
),
maxmax = Integer.MIN_VALUE,
minmin = Integer.MAX_VALUE;
Random gen1 = new Random();

if (maxcrazy > max || maxcrazy < min) {
System.out.println("Crazy number");
System.exit(1);
}

// now the size has been validated, it is a good idea to create the array
int Arr1[] = new int[maxcrazy];

// init all elements of the array and find if new max and/or new min
for (int crazy = 0; crazy < Arr1.length; crazy++) {
Arr1[crazy] = gen1.nextInt(maxcrazy);
System.out.println("Number [" + crazy + "] is " + Arr1[crazy]);

// find min and max
if (maxmax < Arr1[crazy])
maxmax = Arr1[crazy];
if (minmin > Arr1[crazy])
minmin = Arr1[crazy];
}
System.out.println("Max: " + maxmax + " Min: " + minmin);
}
}



User is offlineProfile CardPM
+Quote Post

chachejave
RE: Printing A Java Array
16 Apr, 2008 - 04:40 PM
Post #4

New D.I.C Head
*

Joined: 24 Feb, 2008
Posts: 4

Thats so helpful. Both of you make the programming much clearer. Also, I see what you mean about going to Anchorage, although, I do have family there. And I wasn't in that big of a hurry. And I never really understood the directions for getting to Vegas. And I only had pieces of a map.
jave
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month