# hellow im new here..can somebody help me with this prog i think its easy for you->

• 02-17-2012, 06:43 AM
begginer
hellow im new here..can somebody help me with this prog i think its easy for you->
for you guys to understand i am 1st yr college. begginer in java pls help me with this guys i just need the prog that will stop if you top a negative number for exmple this prog cant figure it out.

import java.util.Scanner;

public class ReverseOrder
{

public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);

double[] numbers = new double[10];

System.out.println ("The size of the array: " + numbers.length);

for (int index = 0; index < numbers.length; index++)
{
System.out.print ("Enter number " + (index+1) + ": ");
numbers[index] = scan.nextDouble();
}

System.out.println ("The numbersyou entered:");

for (int index = numbers.length-1; index >= 0; index--)
System.out.print (numbers[index] + " ");
}
}
help me pls when you type - number the prog will stop and display the numbers you entered..thank you asap
output
enter number 1: 3
enter number 2: 3
enter number 3: 3
enter number 4: 3
enter number 10: 4

the number you entered :
3.0 3.0 3.0 3.0.3.0 4.0
• 02-17-2012, 07:11 AM
rhexis
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
Basically, you'll have to check for it inside your first for loop.

You can do so by using an if else statement.

In this case, check if the number[index] if it is less than 0.

***DO WHAT YOU WANT***

to break out from the loop, use a

break;

With your current code, after you enter a negative number at say Number 3, it will stop and print something like

The size of the array: 10
Enter number 1: 1
Enter number 2: 3
Enter number 3: -3
The numbersyou entered:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 -3.0 3.0 1.0

if that is what you want.
• 02-17-2012, 07:58 AM
begginer
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
thank you i cant still figure it out i tried to use if else break;
maybe i can use another variable maybe if use this if (index <0)
break;
stil working on it sorry im not good in english

thanks for replz u gve me an idea :)
• 02-17-2012, 08:03 AM
begginer
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
size of the array 10 you entered 10 number but if type -neagtive the prog stop in print the num you type
enter num 1: 90
enter num 2: -56

the number you entered :
90.0 -56.0

like this
• 02-17-2012, 08:07 AM
rhexis
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
Can the user type 0 as a number?

If the user can enter 0 as a number, I don't think you should use an Array as it will store 0.0 if no values are entered, and would therefore print out all the values 1-10 in reverse order.

You can change your Array to an ArrayList. An ArrayList grows dynamically with no fixed size.

if you assume that the user won't enter 0 as a number, all you have to do is add another if else statement at the 2nd for loop to check that if (numbers[index] != 0), print that number. The downside is of course, if the user enters a number 0, it won't print.

An example of what I mean

The size of the array: 10
Enter number 1: 3
Enter number 2: 0
Enter number 3: -3
The numbersyou entered:
-3.0 3.0
• 02-17-2012, 10:16 AM
begginer
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
import java.util.Scanner;

public class Hirap
{

public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);

int[] numbers = new int[10];

System.out.println ("The size of the array: " + numbers.length);

for (int index = 0; index <numbers.length; index++){

System.out.print ("Enter number " + (index+1) + ": ");
numbers[index] = scan.nextInt();
if(numbers[index]<0){
break;
}

}

System.out.println ("The numbers you entered:");

for (int index = numbers.length-1; index>= 0; index--)

System.out.print (numbers[index] + " ");

}
}

ihave a little prob when i entered a -number the prog wil stop and print the ouput like this

size of array 10:

enter num 1: 15
enter num 2: 16

but when you entered - numbers the prog wil stop but print the 10 array size:((

enter num 3 : -19

the num you entered:

15 16 -19 0 0 0 0 0 0 0 0

huhuh haha itsto hard for a begginer zzzz
• 02-17-2012, 12:33 PM
rhexis
Re: hellow im new here..can somebody help me with this prog i think its easy for you-
Like I have mentioned, the problem is because you are using an Array that you have already declared a size too, which in this case is 10

In this case, any unfilled slots in an Array will be filled with the int 0.

I recommend you use an ArrayList. Just google on how to use an ArrayList, it's very simple.

An ArrayList has no size limit, and allows you to dynamically add elements into it.