Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By SurfMan
  • 1 Post By SurfMan

Thread: I need help!

  1. #1
    Crius is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default I need help!

    hello, we're ask to write a program that includes conditional statement, user entry, loops and array

    so far here's my work, i am confused on how and where to a conditional statement.

    import java.util.*;

    class Test {

    Public class static void main(String[] args)
    {
    int size, num[], test;

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter Array Size");
    size = scan.nextInt();
    num = new int[size]
    System.out.println();
    for (int i = 0; i < num.length; i++)
    {
    System.out.println("Type a number please");
    num[i] = scan.nextInt();

    }
    System.out.println();
    System.out.println("You have entered this numbers");
    for ( int i = 0; i < num.length; i++)
    System.out.println(num[i]);
    }

    }


    EDIT:

    here's my GF's work, our instructor ask her to loop "enter the number"
    any suggestions?

    same requirements:

    conditional statement
    array
    loop
    and user entry

    here's her code:


    import java.util.scanner;
    class test2 {

    public static void main(String []args){

    int n, c, d, swap;
    scanner in = new Scanner(system.in);

    System.out.println("input number of integers to sort");
    n = in nextInt();

    int array[] = new int[n];

    Sytem.out.println ("enter " + n + integers");

    for (c = 0; c < n; c++)
    array[c] = in.nextInt()

    for (c = 0; c < n ( n - 1 ); c++) {
    for (d = 0; d < n - c - 1; d++) {
    if (array[d] > array[d+1])
    {
    swap =array[d];
    array[d] =array[d+1];
    array[d+1] =swap;
    }
    }
    }
    SYstm.out.println("sorted list of number");
    for (c = 0; c < n; c++)
    System.out.println(array[c]);
    }
    }

    }



    anyways thanks for the corrections..
    Last edited by Crius; 10-21-2014 at 04:31 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: I need help!

    First observations:
    - Public is lowercase: public
    - the signature for the main method is wrong. That is supposed to read: public static void main(String args[])

    Second, for your while loop, use pen and paper. Write down how you would do this by hand. Create little boxes, draw lines where to go next. See if you can find the part that is inside the loop.
    Fluffy93 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: I need help!

    Quote Originally Posted by SurfMan View Post
    - the signature for the main method is wrong. That is supposed to read: public static void main(String args[])
    (String[] args) is also fine. So is (String... args)

    Crius, every time you add a few lines of code, make sure it compiles before moving on. That way, whenever you make a mistake you only have to look at what you've just typed.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: I need help!

    Quote Originally Posted by DarrylBurke View Post
    (String[] args) is also fine. So is (String... args)
    I know. I should have mentioned the other ways. Didn't want to complicate things for him/her. Valid points though.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    Crius is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: I need help!

    Quote Originally Posted by SurfMan View Post
    First observations:
    - Public is lowercase: public
    - the signature for the main method is wrong. That is supposed to read: public static void main(String args[])

    Second, for your while loop, use pen and paper. Write down how you would do this by hand. Create little boxes, draw lines where to go next. See if you can find the part that is inside the loop.
    thanks for this... but can you elaborate more about that pen and paper method,.. thanks!

  6. #6
    Crius is offline Member
    Join Date
    Oct 2014
    Posts
    3
    Rep Power
    0

    Default Re: I need help!

    Quote Originally Posted by DarrylBurke View Post
    (String[] args) is also fine. So is (String... args)

    Crius, every time you add a few lines of code, make sure it compiles before moving on. That way, whenever you make a mistake you only have to look at what you've just typed.

    db
    okay got it, next time! Thanks!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: I need help!

    Get this too: Forum Rules

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: I need help!

    Don't edit the original post with extra code. Chances are they are not read, since I only read the new replies.

    Quote Originally Posted by Crius View Post
    thanks for this... but can you elaborate more about that pen and paper method,.. thanks!
    Before you start writing code that makes your eyes bleed and gives your professor a heart attack, you want to make sure know what you are doing. Explain your goals to me like a five year old:

    1) I have to get a number from you that indicates the amount of numbers you will enter
    2) I create an array of that specific size
    3) I will start a loop that runs the times you entered
    4) I will ask you for a number
    5) I will put that number in the array I created
    6) I have to go back to step 4 until I have all the numbers
    7) Do something awesome with the array

    Call your mom and get her to understand the flow of your program. If she gets it, you nailed it and you can start translating your steps into code.

    You will see that you can create a loop that runs a preconfigured amount of times with a for-loop. (There are different ways to do it, but for sake of simplicity, I'll keep it at this).
    That loop is created in step 3, and runs to 6. Step 4 and 5 are in the loop.

    Edit PS: your code does not contain a conditional statement yet.
    gimbal2 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: I need help!

    Quote Originally Posted by SurfMan View Post
    Don't edit the original post with extra code. Chances are they are not read, since I only read the new replies.
    It can also confuse readers who are trying to follow the logic of the replies to see what was wrong.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Posting Permissions

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