• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extracting numbers from document  RSS feed

 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help! I have an assignment where I have to extract numbers from a text document and then take the extracted numbers and print them if they are prime.

I have my code for determining if the numbers are prime and I can take a document and have it so that it reads the text and prints every word and number but when I change anything to just record the number I get a ton of errors or the program runs and is just blank

this is what I have for the code to print out everything from the document. Is there any advice you have? I have read the textbook and I have tried the nextInt() but that is when its giving me nothing.


 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors do you get? I used your code, and my own file and it worked just fine.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I think I see the problem. Its just a normal text file right? If so use .txt instead of .csv
 
Natasha Sherrod
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:Actually I think I see the problem. Its just a normal text file right? If so use .txt instead of .csv


This code runs perfectly. Its an excel document. Right now it is reading everything and printing everything. I need to to filter (extract) the numbers and only print the numbers. so I can then determine if they are prime. I have no clue how to go about doing it. I have tried everything. I'm not getting an error code anymore its just not printing anything
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your file look like?
 
Junilu Lacar
Sheriff
Posts: 11164
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a CSV file, it's a plain text document, not an Excel file per se. Programs like Excel can be used to view and edit CSV files.

As for "I have tried everything" you need to show us what you've tried that you think should work. Only then can we give you some concrete advice.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am going to have to edit your post to get rid of the long lines, which make the post harder to read; then you can see how to do it yourself.
Why does your URI have // in? I am sure that shou‍ld read \\ or / (I think / is better, even on a Windows® box).
Style thing: why have you got your Scanner reference in CAPITAL_LETTERS when the variable is not even final, never mind a constant?
Why are you setting the delimiter to single space? What is wrong with the default delimiter? If you use that delimiter and there are double spaces in the file, you will pick up a token equal to the empty/zero‑length String "" between those spaces. If you really have a csv file, why does your delimiter not contain commas?
Don't explicitly close the Scanner if possible. You shou‍ld use try with resources instead if possible. That will also get you out of having to write the throws clause.
Don't write comments to tell people what they already know; something like
myScanner.close(); // close my Scanner
...is completely unnecessary.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the fact that you should probably delete 90% of those comments as not telling anything people don't know already, and
Apart from the inconsistent indentation (lines 19 and 21 look odd because you are mixing tabs and spaces),
Something useful: you can use methods of the Scanner class to tell whether the next token in line will be a number. Go through the Scanner link I gave you a few minutes ago and you will find a similar example.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I echo Cody's request?
What is the structure of the CSV?

And which bits of it are you trying to extract.
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Try not to use File along with Scanner class. That could give you an unintended behavior in certain situations.
Instead, use:

2. Lets make it clear - indentation, naming are important techniques (there should be a space between lines 5 and 6; after exception on line 6 there should be a space, right before curly brace;).
Calling class Demo7, after 2 months will make you wonder what is that. Call it so it would make sense [app's short description in 2-3 words]. Variable names supposed to start with lower case, referring to "TempString".

I have reformatted your code, fixed path of the data file, removed not needed comments, so you can continue on (added some extra spacing for you to better distinct related parts):

3. Try now not to keep all that code without any given context to it in the main method. Try to create a method for each of your sub-tasks. For instance, that code you could try to change to:

4. You can see, that method extractPrimeNumbers could be and needs to be decomposed to more methods (added isPrime()):

5. And maybe finally to add function to print prime numbers (added print() method):

Have you noticed? Code gets more and more readable without any comments once you start adding methods with names which reveal their intention, that is way more powerful than spend time on writing comments which go out of date fairly quick.

Afterwards consider if you have to avoid using "static" keyword. That might be not needed in your stage yet. Good luck on your assignment
 
Piet Souris
Rancher
Posts: 1983
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:1. Try not to use File along with Scanner class. That could give you an unintended behavior in certain situations.

Since a Fiile is returned from a JFileChooser (getSelectedFile,  that is), I use this on a regular basis, without problem. What unintended behavior might this give?
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Liutauras Vilda wrote:1. Try not to use File along with Scanner class. That could give you an unintended behavior in certain situations.

Since a Fiile is returned from a JFileChooser (getSelectedFile,  that is), I use this on a regular basis, without problem. What unintended behavior might this give?

Hi Piet. Please check this Oracle text. A bit unfortunate that they didn't reflect that information in Java API documentation where most people are being referred to. Also check this topic for that possible certain situation.
 
Piet Souris
Rancher
Posts: 1983
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thank you for this information! Never knew this. So I guess it's: goodbye File, Hello Path  from now on...   
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're most welcome
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!