Manipulating Data in an Array

Need help extracting data from an array.

Page 1 of 1

6 Replies - 2931 Views - Last Post: 07 December 2008 - 08:50 AM Rate Topic: ***** 1 Votes

#1 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Manipulating Data in an Array

Post icon  Posted 03 December 2008 - 12:31 PM

Okay I am trying to create an array of 100 integers. Then allow the user to enter as many as they want to up to 100 total integers with a SENTINEL of -999 as an escape. (since it is likly no user will want to enter all 100 integers).

From there I am trying to display the integers the user entered to the screen. Then display the highest integer entered, then the lowest integer entered. and lastly I need to calculate the sum of the integers entered by the user.

I also need to format the output so that when all the integers entered by the user are shown in the screen they are seperated by a space and a comma except for the last integer.

This is how far I got and it is not working!!!

import java.io.*;
import java.util.*;

public class intArray
{
	static Scanner console = new Scanner(System.in);
	static final int SENTINEL = -999;
	public static void main(String[] Args) throws IOException
	{
	
	BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
	
	//Declare Variables
	int[] listA = new int[100];
	String strNumEntered;
	int number, sum = 0, count = 0;
	
	//Have the user provide the input.
	System.out.println("When you are done enter -999");
	System.out.println("Enter positive numbers & watch...");
	number = console.nextInt();
	
	for (number = 0; number < listA.length; number++);
		listA[number] = console.nextInt();
		/* [i](this is commented out as of right now)[/i]
		{
			sum = sum + number;
			count++;
			number = console.nextInt();
		}
		*/
		
	//Output to the user.
	System.out.println("The integers entered were: ");
	System.out.println();
	System.out.println("The highest integer entered was: ");
	System.out.println();
	System.out.println("The loweest integer entered was: ");
	System.out.println();
	System.out.printf("The average of the integers entered is: %d%n", (sum / count));
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Manipulating Data in an Array

#2 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Re: Manipulating Data in an Array

Posted 03 December 2008 - 02:14 PM

Message Bump, Need help
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Manipulating Data in an Array

Posted 03 December 2008 - 08:08 PM

Your code is kind of complicated... and you for( on number for nothing
int[] array = new int[100];

int idx;
for(idx = 0; idx < array.length; idx++)
{
	  int number = console.nextInt();
	  if(number == -999)
		break;
	  array[idx] = number;
}
// number of elements in array
idx--;

int minValue = array[0];
int maxValue = array[0];
int total - array[0];
for(int i = 1; i < idx; i++) 
{
	if(array[i] < minValue)
	   minValue = array[i];
	if(array[i] > maxValue)
	   maxValue = array[i];
	total += array[i];
}


Was This Post Helpful? 1
  • +
  • -

#4 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Re: Manipulating Data in an Array

Posted 04 December 2008 - 08:36 PM

Thank you for the help, but I am much newer to Java programming and I would like to understand what it is that you coded, is there anyway this can be simplified to more basic operations? (dumb it down) or add some comments letting me know what does what? I am having a hard time figuring it out. I don't understand the [i] I reworded and rearranged a lot of what you did to help me understand. I got everything to work except displaying all the integers the user enters to the screen as output...

import java.io.*;
import java.util.*;

public class intArray
{
	static Scanner console = new Scanner(System.in);
	static final int SENTINEL = -999;
	public static void main(String[] Args) throws IOException
	{
	
	BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
	
	//declare variables
	int[] array = new int[100];
	int index, number, averageNum;
	
	//Have the user provide the input.
	System.out.println("Enter positive numbers, and when your done enter -999");

	for(index = 0; index < array.length; index++)
		{
								number = console.nextInt();
								if(number == -999)
								break;
								array[index] = number;
		}
		
	// number of elements in array
	int minValue = array[0];
	int maxValue = array[0];
	int total = array[0];
	for(int i = 1; i < index; i++)
		{
		if(array[i] < minValue)
		minValue = array[i];
		if(array[i] > maxValue)
		maxValue = array[i];
		total += array[i];
		}
	
	//get the average
	averageNum = total / index;
		
	//Output to the user.
	System.out.println("The integers entered were: ");
	System.out.println();
	System.out.println("The highest integer entered was: " + maxValue);
	System.out.println();
	System.out.println("The loweest integer entered was: " + minValue);
	System.out.println();
	System.out.printf("The average of the integers entered is: %d%n", + averageNum);
	}
}


This post has been edited by krich87: 04 December 2008 - 08:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Re: Manipulating Data in an Array

Posted 06 December 2008 - 06:38 PM

Can someone take a look at this code and help me print the numbers the user enters into the array back to the screen? thats the only part of the program I can't get to work.

I want the user to enter integers and when they are done I want to print out a message that says...

"The integers you entered were: " and then print the integers.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Manipulating Data in an Array

Posted 06 December 2008 - 10:26 PM

System.out.print("The numbers entered were: ");
for(int i = 0; i < index; i++)
  System.out.print(" "  + array[i]);
System.out.println();


???
Was This Post Helpful? 1
  • +
  • -

#7 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Re: Manipulating Data in an Array

Posted 07 December 2008 - 08:50 AM

Thanks a lot, that was what I was doing wrong, I didn't know that block of code had to go where you put it. Here is my final project.
import java.io.*;
import java.util.*;

public class intArray
{
	static Scanner console = new Scanner(System.in);
	static final int SENTINEL = -999;
	public static void main(String[] Args) throws IOException
	{
	
	BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
	
	//declare variables
	int[] array = new int[100];
	int index, number, averageNum;
	
	//Have the user provide the input.
	System.out.println("Enter positive numbers, and when your done enter -999");

	for(index = 0; index < array.length; index++)
		{
		number = console.nextInt();
		if(number == -999)
		break;
		array[index] = number;
		}
		
	// number of elements in array
	int minValue = array[0];
	int maxValue = array[0];
	int total = array[0];
	for(int i = 1; i < index; i++)
		{
		if(array[i] < minValue)
			minValue = array[i];
		if(array[i] > maxValue)
			maxValue = array[i];
			total += array[i];
		}
	
	//get the average
	averageNum = total / index;
		
	//Output to the user.
	System.out.print("The integers entered were:");
	for(int i = 0; i < index; i++)
	System.out.print(" "  + array[i]);
	System.out.println();
	System.out.println();
	System.out.println("The highest integer entered was: " + maxValue);
	System.out.println();
	System.out.println("The loweest integer entered was: " + minValue);
	System.out.println();
	System.out.printf("The average of the integers entered is: %d%n", + averageNum);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1