473,326 Members | 2,192 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.

I need to display first word from each line.

Hey guys I'm using dictionary.txt file and need to display first word from each line so I can then randomly pick 100 of them and find word definitions for it.
I'm having problem with displaying first words, for some reaso I can only display words starting with 'S'.
Here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[])
  2.       {
  3.       try{
  4.       // Open the file that is the first 
  5.       // command line parameter
  6.       FileInputStream fstream = new FileInputStream("dictionary.txt");
  7.       // Get the object of DataInputStream
  8.       DataInputStream in = new DataInputStream(fstream);
  9.       BufferedReader br = new BufferedReader(new InputStreamReader(in));
  10.       String strLine;
  11.       //Read File Line By Line
  12.       while ((strLine = br.readLine()) != null)   {
  13.  
  14.           String[] delims = strLine.split("  ");
  15.           String first = delims[0];
  16.  
  17.           for(int i = 0; i < delims.length-1; i++ )
  18.           {
  19.               System.out.println(delims[i]);
  20.           }          
  21.       }
  22.       //Close the input stream
  23.       in.close();
  24.         }catch (Exception e){//Catch exception if any
  25.       System.err.println("Error: " + e.getMessage());
  26.       }
  27.       }
Oct 26 '15 #1

✓ answered by chaarmann

If the dictionary.txt file is large, then just copy the first 10 lines from it.
Also, after fixing the for loop as described above, does the problem still exist?

3 2755
chaarmann
785 Expert 512MB
Can you please list the content of the file?
And also the output you get?

By the way,
your for-loop in line 17 is probably wrong: you are skipping the last splitted part.

correct:
Expand|Select|Wrap|Line Numbers
  1.           for(int i = 0; i < delims.length; i++ )
Maybe you want to split by a single space, but you are actually splitting by two spaces. So if you don't have 2 spaces in a line, then everything goes into index 0 of the splitting array (which you are skipping to show for some unknown reason as discussed above.)
Oct 26 '15 #2
There are 2 spaces after each word. The content of the file is dictionary: word, then two spaces and definition (all in one line). I'm not at home right now so don't have access to that dictionary.txt file.
Oct 26 '15 #3
chaarmann
785 Expert 512MB
If the dictionary.txt file is large, then just copy the first 10 lines from it.
Also, after fixing the for loop as described above, does the problem still exist?
Oct 27 '15 #4

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

Similar topics

4
by: NewAlias | last post by:
How to display comments for each enum entry? 'Enum example with commnents commented Private Enum MyEnum YourName =1 ' Set this to get your real name YourAge=2 ' Set this to get your real age...
4
by: vj6940 | last post by:
hi, i am writing a program to search first word from a line but i am not getting an idea to how to do this? if any one have a solution then mail me <Removed> thanks !!!!!
6
by: kronus | last post by:
Hi everyone, This is my first time posting to the UNIX form and it might be a strange request, but I believe I have most of the pieces. Here's the overall goal -- I am trying to find the links...
2
by: whodgson | last post by:
The following code accepts a string from the user and swaps the first and last letters in each word. It prints out the original string incorrectly by dropping off the first letter of the first word....
2
Lovro Mirnik
by: Lovro Mirnik | last post by:
Hello, I'm need of some help again. I've got a Folder, in which there is a File. In the File, there are two lines, and the data in the lines is seperated with a " // ". Example: There is a...
9
gcoaster
by: gcoaster | last post by:
Hello Group I need help, I need to use an to show results values from a table and display in a unbound textbox on a form. the results from each column in the table need to be on seperate...
10
by: wirejp | last post by:
This is a follow-on post from the link: http://bytes.com/topic/access/answers/958921-invoice-layout-tables I am using a Microsoft Access 2010. I have created a report to show the invoice amounts for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.