473,326 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Java: select 100 random words from String array

Hello, I have a text file dictionary.txt and I'm trying to get first 100 random words from each line of the text file and put them into String array, but it's not working. I figured how to separate files from text and put them into array, but I can't figure where to include 100 to get them sorted.
Thank you!!!
Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class New2 
  8. {
  9.          public static void main(String[] args) throws FileNotFoundException
  10.          {
  11.              Scanner sc = new Scanner(new File("dictionary.txt"));
  12.              while (sc.hasNext())     
  13.                  {
  14.                  String word = sc.next();
  15.                  sc.nextLine();      
  16.                  String[] wordArray = word.split(" "); 
  17.                  //System.out.println(Arrays.toString(wordArray));
  18.                  int idx = new Random().nextInt(wordArray.length);
  19.                  String random = (wordArray[idx]);
  20.                  System.out.println(random);
  21.  
  22.  
  23.             }
  24. }
  25. }
  26.  
  27.  
Oct 27 '15 #1
6 4553
chaarmann
785 Expert 512MB
Use
Expand|Select|Wrap|Line Numbers
  1. Collections.sort(arrayList);
to sort them.
In order to do so, you must convert your array into an arrayList.
You can do that with
Expand|Select|Wrap|Line Numbers
  1. List arrayList = Arrays.asList(wordArray);
If you only want the first 100, you can make a sublist:
Expand|Select|Wrap|Line Numbers
  1. Collections.sort(arrayList.subList(0,100));
Oct 27 '15 #2
I tried to do it your way and it threw me an error:
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" java.lang.IndexOutOfBoundsException: toIndex = 100
  2.     at java.util.SubList.<init>(Unknown Source)
  3.     at java.util.RandomAccessSubList.<init>(Unknown Source)
  4.     at java.util.AbstractList.subList(Unknown Source)
  5.     at New2.main(New2.java:20)
  6.  
Here is the code that I've changed according to your suggestion:
Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class New2 
  9. {
  10.     public static void main(String[] args) throws FileNotFoundException
  11.          {
  12.              Scanner sc = new Scanner(new File("dictionary.txt"));
  13.              while (sc.hasNext())     
  14.                  {
  15.                  String word = sc.next();
  16.                  sc.nextLine();      
  17.                  String[] wordArray = word.split(" "); 
  18.                  //System.out.println(Arrays.toString(wordArray));
  19.                  List<String> arrayList = Arrays.asList(wordArray);                                 
  20.                  Collections.sort(arrayList.subList(0, 100));
  21.  
  22.  
  23. // Here I tried to print out that arrayList.
  24. // It throws the same error even if I remove the code below.
  25.  
  26.  
  27.                  for (int i = 0; i < arrayList.size(); i++) 
  28.                  {
  29.                         String value = arrayList.get(i);
  30.                        System.out.println(value);
  31.                  }
  32.  
  33.                  }
  34.          }             
  35. }
  36.  
Oct 28 '15 #3
chaarmann
785 Expert 512MB
So that means one of your lines has less than 100 words! I did not asume that, because you wrote "get first 100 random words from each line".

So in case the line has less than 100 words, you only get what's available. (Or do you want to print out an error message instead?)

So instead of:
Expand|Select|Wrap|Line Numbers
  1. arrayList.subList(0, 100)
use:
Expand|Select|Wrap|Line Numbers
  1. arrayList.subList(0, Math.min(100, arrayList.size()))

Oh wait, does "get first 100 random words from each line" mean that there are 100 lines and you get a single word, at a random position, from each line ?
Or does that mean: get 100 words from each line and then shuffle-sort them randomly?

because I told you the second meaning. But still a small mistake from my side: use Collections.shuffle() instead of Collections.sort()
Oct 28 '15 #4
So that dictionary file has almost 1000 lines in it. First word of each line is the actual word and then the rest of the line is definition. I need to pull 100 of those random first words in each line and put them into array or array list and then I'll print them into separate file, but that's different story.
I did try Collections.shuffle, but it doesn't shuffle them, just prints the whole array with all first words from each line. However, if I declare array and add my words into it, Collections.shuffle works without a problem.
Oct 28 '15 #5
Do you know if there is way to get 100 random lines from text file and then separate first words from those lines, and then load them into array?
Oct 28 '15 #6
chaarmann
785 Expert 512MB
Maybe the shuffle did not work, because you shuffled a temporary copy and then threw the shuffled copy away?
like:
Expand|Select|Wrap|Line Numbers
  1. Collections.shuffle(Arrays.asList(arr))";
Here, Arrays.asList() makes a copy of the original int[] arr and returns it. So if we refactor this statement, we get:
Expand|Select|Wrap|Line Numbers
  1. String[] arr = { "a", "ab" "b", "c"};
  2. List al = Arrays.asList(arr);
  3. Collections.shuffle(al);
  4. System.out.println("original array" + arrays.asList(arr));
  5. Sytsem.out.println("shuffled array" + al);
Experts usually work with Collections and not with arrays of primitive types. Beginners usually only learn about primitive arrays at university, but not about Collections (List, Map, etc.) Especially your task would be simpler and less code when using only collections.

Following algorithm:
1.) define an empty ArrayList al
2.) read all text lines and put only the first word of each line into your al with
Expand|Select|Wrap|Line Numbers
  1. al.add(firstWord)
3.) at the end, shuffle your array list (see above)
4.) print only first 100 entries with:
Expand|Select|Wrap|Line Numbers
  1. System.out.println("first 100=" + al.subList(0, Math.min(100, arrayList.size()))
Oct 29 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Rick | last post by:
I have a program that reads from a file. In the file are a series of words. I read in all the words into a string array, find the average length, count the number of words, display the longest...
11
by: Olaf \El Blanco\ | last post by:
How can i generate random words? ('a'..'z') Is there any function that convert a number to it ascci char? My english is horrible! Here an example: function(65) return 'a'; Thank you!
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
10
by: mattias.k.nyberg | last post by:
So Im trying to learn to program with C#. And I have this question about why the string array won't work in the first class but it does in the second. To me it looks like they do the exact same...
7
by: Jay | last post by:
How would I be able to grab random words from an internet source. I'd like to grab a random word from a comprehensive internet dictionary. What would be the best source and the best way to go...
12
by: JackYee123 | last post by:
Hey, I need a structure to store a string array in c, for example Index Content -------- ----------- 0 word1 1 word2 2 3
5
by: Dbarten1982 | last post by:
I am brand new to C++ programming, and am completely stuck on a program. In the program I am supposed to create a string array that stores 5 user input words, and the string constant "END_OF_ARRAY'...
1
by: deneme birki | last post by:
hello. i want to print one of random words that i defined before, on screen. but using an external .txt or .dat file. for example i add 100 words on the words.txt or words.dat file and program will...
0
sunsolaris2000
by: sunsolaris2000 | last post by:
I have 3 sql tables: **professors(prof_id, name)**, **professors_courses(prof_id, course_id)** and **courses(course_id, title)**. **professors_courses** is the bridge table between professors and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.