1. Member
Join Date
Feb 2013
Posts
4
Rep Power
0

## 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. Moderator
Join Date
Apr 2009
Posts
13,240
Rep Power
23

## Re: Sorting numbers

How would you do this by hand?

3. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## 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. Member
Join Date
Feb 2013
Posts
4
Rep Power
0

## Re: Sorting numbers

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

5. ## 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

6. Member
Join Date
Feb 2013
Posts
4
Rep Power
0

## 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. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: Sorting numbers

Originally Posted by JosAH
... 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. Member
Join Date
Feb 2013
Posts
4
Rep Power
0

## 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. Member
Join Date
Feb 2013
Posts
1
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,849
Rep Power
10

## Re: Sorting numbers

Originally Posted by Max
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

#### Posting Permissions

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