Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with beginner code

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with beginner code

    Hi, My child is appearing for class x exams and is having difficulty with java programming. Is writing different programs but is having difficulty understanding where is going wrong. Can someone please look at this code to sort 15 numbers in descending order using selection sort

    //program to accept 15 numbers and print them in descending order using selection sort
    import java.io.*;
    public class sort
    {
    public static void main(String[]args)throws IOException
    {
    BufferedReader userin= new BufferedReader(new InputStreamReader(System.in));
    int i;int j;int pos;int max;int t; //
    int n[]=new int[15];
    for(i=0;i<15;i++)
    {
    System.out.println("Enter a number");
    n[i]=Integer.parseInt(userin.readLine());
    }

    for(i=0;i<14;i++);
    {
    max=n[i];
    pos=i;

    for(j=i+1;j<15;j++);
    {
    if(n[i] < n[j])
    n[j]=max;
    pos=j;
    }

    }

    t=n[i];
    n[i]=n[pos];
    n[pos]=t;
    for(i=0;i<15;i++)
    {
    System.out.println(n[i]);

    }
    }
    }


  2. #2
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Help with beginner code

    Try to post code in a formatted way so we can see what's going on.

    One error that jumps out is this:
    for(j=i+1;j<15;j++);
    {
    //do something
    }

    The semi-colon will end the for loop right there and it won't do what's in the curly brackets.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,225
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: Help with beginner code

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Also format the code with proper indentations to show logic nesting levels.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner code
    By Skybear in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 1st, 2012, 06:15 PM
  2. Hello, I'm a beginner and I really need help for my code..
    By aimEhjO in forum Member Introductions
    Replies: 10
    Last Post: August 4th, 2012, 09:22 AM
  3. Need Help with code ...beginner
    By Envious1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 19th, 2012, 07:52 AM
  4. (beginner) if else code.
    By dumb1101 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 16th, 2012, 01:47 AM
  5. Beginner - Help with my code.
    By eddross in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2010, 09:30 AM