Results 1 to 7 of 7
  1. #1
    begginer is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default 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.
    Last edited by rhexis; 02-17-2012 at 07:35 AM.

  3. #3
    begginer is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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 :)

  4. #4
    begginer is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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

  5. #5
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default 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
    Last edited by rhexis; 02-17-2012 at 08:13 AM.

  6. #6
    begginer is offline Member
    Join Date
    Feb 2012
    Posts
    4
    Rep Power
    0

    Default 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

  7. #7
    rhexis is offline Member
    Join Date
    Nov 2011
    Posts
    56
    Rep Power
    0

    Default 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.

Similar Threads

  1. prog
    By mamtha in forum New To Java
    Replies: 2
    Last Post: 01-11-2012, 05:59 PM
  2. Running my prog.
    By arcticM in forum AWT / Swing
    Replies: 3
    Last Post: 08-24-2009, 04:42 AM
  3. What I need to do more in this prog??
    By eliCanzee in forum AWT / Swing
    Replies: 2
    Last Post: 06-01-2009, 04:47 AM
  4. hellow you guys...
    By babyarle20 in forum Introductions
    Replies: 1
    Last Post: 08-19-2008, 12:51 PM
  5. Replies: 1
    Last Post: 07-13-2008, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •