Results 1 to 10 of 10

Thread: Sorting numbers

  1. #1
    Max
    Max is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Sorting numbers

    Hi.
    Im not java programmer but i want to learn.
    I need to solve one problem but i have no clue how to make this with java.
    In this process im using netbeans on linux.

    I have number list ( separated with tab ) and this list is in file called numbers.txt.
    One row consists from cca 10 or less numbers and there are around 20 columns.

    How can i do the following :
    - check all the numbers
    - separate all those numbers that are repeated more then 3 times
    - write the result in one row.

    E.g.
    1 6 12 28 29 11 2
    1 2 17 24 74 11 59
    1 9 20 21 23 11 62
    ----------------------
    result : 1, 11

    Max

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Sorting numbers

    How would you do this by hand?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Sorting numbers

    Hi Max, welcome to the forums.

    For this you will need to use of combination of IO and a bubble sort. If this makes no sense then I would suggest forgetting about the text file and look into using for loops.
    If you really want to start with basics then google for the 'hello world' program and start from there.

    Regards.

  4. #4
    Max
    Max is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Sorting numbers

    Im ASM programmer so my area of work is quite different.
    Main problem is the syntax....

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Sorting numbers

    ... and don't bother looking for a bubble sort explanation; it is one of the worst sorting methods around; using it is just a bad habit.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Max
    Max is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Sorting numbers

    OK. Let's take step at the time.

    I created this to read the file and its working without try/catch

    Java Code:
    package test;
    
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String[] args)throws IOException
        {
            java.io.File file = new java.io.File("testNum.txt");
        
                Scanner input = new Scanner(file);
                while(input.hasNext())
                
                   {
                    String num = input.nextLine();
                    System.out.println(num);
                    }
            }
        
    }
    So now i should find a way how to detect tab separator or something like this ?
    Last edited by Max; 02-13-2013 at 10:38 PM.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: Sorting numbers

    Quote Originally Posted by JosAH View Post
    ... and don't bother looking for a bubble sort explanation; it is one of the worst sorting methods around; using it is just a bad habit.

    kind regards,

    Jos
    True, but given the OP's stated they wanted to learn Java I assumed no prior knowledge. The bubble sort is a basic algorithm just to get them started but given the OP's history perhaps they would have a better understanding of the alternatives.


    Max,

    Have a look at the useDelimiter() method for the Scanner class, this may be what you are looking for.

    Regards.

  8. #8
    Max
    Max is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Re: Sorting numbers

    I saw some examples on how to do this but in the meantime i have one more problem.
    I want to generate rendom numbers from 0 - 50 so am using this:
    public class Test {

    public static void main(String[] args) {
    System.out.println("Random numbers");
    for(int i=0; i < 5 ; i++)
    System.out.println("Random Number ["+ (i+1) + "] : " + (int)(Math.random()*50));

    }
    }
    ----------
    but how to get random number if i have predefined range.
    Like 10 - 40 ?

    Max

  9. #9
    zach is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default Re: Sorting numbers

    public class Test {

    public static void main(String[] args) {
    System.out.println("Random numbers");
    for(int i=0; i < 5 ; i++)
    System.out.println("Random Number ["+ (i+1) + "] : " + ((int)(Math.random()*31) + 10));

    }
    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Sorting numbers

    Quote Originally Posted by Max View Post
    but how to get random number if i have predefined range.
    Like 10 - 40 ?

    Max
    To generate a random number between two values a and b inclusive, for a < b, simply generate one between 0 and (b-a), inclusive, and then add a. So:
    Java Code:
    int numb = (int)(Math.random() * ((b-a)+1)) + a;
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. how to change numbers into word numbers?
    By akeni in forum New To Java
    Replies: 13
    Last Post: 11-18-2011, 09:46 AM
  2. Replies: 11
    Last Post: 01-14-2011, 07:36 PM
  3. Sorting 10 numbers
    By perito in forum New To Java
    Replies: 56
    Last Post: 03-05-2009, 02:52 AM
  4. Replies: 5
    Last Post: 02-07-2009, 08:48 AM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-15-2008, 12:46 AM

Posting Permissions

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