# can someone look at this

• 02-02-2011, 05:52 PM
d2011
can someone look at this
i want to make sure this is right i have to:

1. prompt the user to input 5 decimal numbers (1,2,3,4,5)

2. print the 5 decimal numbers

3, convert decimal to nearest integer

5. prints the sum and average

import java.util.*;

public class chapter 2_1
{
public static void main (string[] args)
{
scanner input = new scanner( System.in);

int number1;
int number2;
int number3;
int number4;
int number5;
double sum;
double average;

number1 = 1;

number2 = 2;

number3 = 3;

number4 = 4;

number5 = 5;

System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.println;

System.out.print( "Enter second integer: " );
number2 = input.nextInt();
System.out.println;

System.out.print( "Enter third integer: " );
number3 = input.nextInt();
System.out.println;

System.out.print( "Enter fourth integer: " );
number4 = input.nextInt();
System.out.println;

System.out.print( "Enter fifth integer: " );
number5 = input.nextInt();
System.out.println;

System.out.println("Enter 5 decimal numbers");
num = console.nextInt();
System.out.println;

sum = number1 + number2 + number3 + number4 + number5;

System.out.print("sum is")

average = sum / 5;

outfile.printf(number1, number2, number3, number4, number5, sum, average)
}

input.close();
outfile.close();
}
• 02-02-2011, 05:59 PM
JosAH
Why don't you use an array int[] numbers= new int[5]? and what is that outfile thing near the end of your code?

kind regards,

Jos
• 02-02-2011, 06:02 PM
d2011
cool ill try that out where would i put the int [] at?
• 02-02-2011, 06:04 PM
JosAH
Quote:

Originally Posted by d2011
cool ill try that out where would i put the int [] at?

I'd say just before you want to use the array, so instead of defining those five numbers you define your array there.

kind regards,

Jos
• 02-02-2011, 06:14 PM
sunde887
I don't see the step where you convert from decimal to integer, do you mean to be able to take 1.34 as an input and convert it to 1? If so you may want to declare a float array and store everything as floats, then use Math.round(float num) to round them to ints.
• 02-02-2011, 06:14 PM
d2011
So I Would put it after the scanner input
• 02-02-2011, 06:15 PM
d2011
Yea that's the part I need help on right now
• 02-02-2011, 06:20 PM
sunde887
Code:

```int num1; int num2; .. int numn;```
it would be much better to declare JUST
Code:

`float[] nums = new float[5];`
You can then make a loop to prompt for numbers and store items in the array.
Code:

```for(int i = 0; i < nums.length; i++){ //prompt //store input in array positions }```
Hope this helps, this will allow you to ask for numbers, and store the inputs.
• 02-02-2011, 06:53 PM
d2011
so it would be this :

import java.util.*;

public class chapter 2_1
{
public static void main (string[] args)
{
scanner input = new scanner( System.in);

float[] nums = new float [5];
double sum;
double average;

for (int i = 0; i < nums.length; i ++0){

System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.println;

System.out.print( "Enter second integer: " );
number2 = input.nextInt();
System.out.println;

System.out.print( "Enter third integer: " );
number3 = input.nextInt();
System.out.println;

System.out.print( "Enter fourth integer: " );
number4 = input.nextInt();
System.out.println;

System.out.print( "Enter fifth integer: " );
number5 = input.nextInt();
System.out.println;

System.out.println("Enter 5 decimal numbers");
num = console.nextInt();
System.out.println;

sum =
• 02-02-2011, 06:56 PM
JosAH
Quote:

Originally Posted by d2011
So I Would put it after the scanner input

What happens when you try it? Remember, I wrote: define your array just before you want to use it.

kind regards,

Jos
• 02-02-2011, 07:11 PM
sunde887
Thats close, except the loop will print all that code 5 times.

Is there a way you can print one statement where the item number changes with each pass through the loop? What happens if you print i? How can you include i to create one print statement which will change?

Also, you got rid of the variables number1, number2, ...numbern. To reference an item in an array you do
Code:

`arrayName[index number]`
• 02-02-2011, 07:20 PM
d2011
so it would be this :

import java.util.*;

public class chapter 2_1
{
public static void main (string[] args)
{
scanner input = new scanner( System.in);

float[] nums = new float [5];
double sum;
double average;

for (int i = 0; i < nums.length; i ++0){

arrayName[index number]

do i get rid of all of the

System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.println;
• 02-02-2011, 07:26 PM
sunde887
no.
Code:

`arrayName[index number]`
are place holders, you want to use your arrays name for arrayName, and some index number in between brackets
Code:

```arr[i]; arr[0]; arr[1];```
are all valid
which one sounds the most important to you?
also, this simple references that item in the array, if you want to store something there you need
Code:

`arr[index] = add something;`
you can figure out what add something should be yourself.
Also, EACH time through the loop it should prompt a message asking for a number
Code:

```System.out.println("Please enter a number:"); System.out.println("Please enter number " + something + ": ");```
are all valid things you can print, choose one which you like the best.

also, proper loop structure is
Code:

```for(initialization; condition; some change){   statements }```
I usually use
Code:

```for(int i = 0; i < something; i++){  statements }```
• 02-02-2011, 09:38 PM
d2011
i looked over the web pages you gave me about arrays and came up with this

import java.util.*;

public class chapter 2_1
{
public static void main (string[] args)
{
scanner input = new scanner( System.in);

float[] nums = new float [5];
double sum;
double average;
arrayName[index number]
arr[1] = 1;
arr[2] = 2;
arr[3] = 3;
arr[4] = 4;
arr[5] = 5;

for(int i = 0; i < something; i++){
statements
}

System.out.println("Please enter number " + something + ": ");

System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.println;

System.out.print( "Enter second integer: " );
number2 = input.nextInt();
System.out.println;

System.out.print( "Enter third integer: " );
number3 = input.nextInt();
System.out.println;

System.out.print( "Enter fourth integer: " );
number4 = input.nextInt();
System.out.println;

System.out.print( "Enter fifth integer: " );
number5 = input.nextInt();
System.out.println;

System.out.println("Enter 5 decimal numbers");
num = console.nextInt();
System.out.println;

sum = number1 + number2 + number3 + number4 + number5;

System.out.print("sum is")

average = sum / 5;
}
}
• 02-02-2011, 09:41 PM
Fubarable
Quote:

Originally Posted by d2011
i looked over the web pages you gave me about arrays and came up with this ...

So you've solved your problem then?

Also you'll want to use code tags when posting code in this forum so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Code:

```[cod&#101;]   // your code goes here   // notice how the top and bottom tags are different [/cod&#101;]```
• 02-03-2011, 03:03 AM
sunde887
Im sorry to sound rude if you really just don't understand, but, are you even trying this, or just taking what I say and adding it in word for word?

If you are trolling, oh well, but if you really are trying to understand this and just having some trouble I will continue to help.

When I say something like statements or something, I am giving you a general idea of how the program should look. However, you must decide what something and statements should be equal to.

This program should consist of 4 important things, a scanner, an array and 2 for loops. One loop should prompt for input and add something to the array with each pass. The second loop should print out the array.

This is the basic part of the program, to add the functionality you need you need 2 additional int items, sum and average. and another for loop. This third loop should loop through the array, round the number, then add it to sum.

Finally compute the avg with the sum.

This is basically a step by step procedure for this, however, you will need to translate it into java yourself.