File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes AP Computer Science assignment help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "AP Computer Science assignment help" Watch "AP Computer Science assignment help" New topic
Author

AP Computer Science assignment help

Jay Jeong
Greenhorn

Joined: Nov 16, 2011
Posts: 6
Ok, so with a given numbers.dat file, my assignment is to have the program read the file and generate three random integers from the numbers.dat file. this is my code so far:



How would I fix this so that my program can generate the numbers? any tips?


[Edit - added code tags - MB]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

So, your question is how to generate a random number? See java.util.Random.

Also, I don't know what you're trying to do here:

but guessing at syntax is not an effective approach.
Jay Jeong
Greenhorn

Joined: Nov 16, 2011
Posts: 6
hey jeff, thanks for replying to my posts. WHen you mean java.util.random, should I replace it with the java.Scanner.util? and as for double number > 0 && < 100; , I'm trying to get the scanner to read all numbers less than 100 and greater than 0, because the assignment requires me to generate three numbers (1-99) and have it declare whether the random numbers are in the numbers.dat file or not
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11302
    
  16

Forget your code. describe in English how you would do this. no computers, no data structures, no 'int's or floats, no methods...just English words. For example...

read the list of numbers
count how many there are (call it 'total numbers in file')
generate a random number between 0 and how many counted (call it 'random number 1')
find the 'random number 1'th number in the file
repeat steps 3 and 4 to more times (possibly check to make sure I don't get the same random number more than once)

once you have the steps like that, code them ONE PIECE at a time. Once you have (for example) the 'read the list of numbers' coded, compile and test it. When you think you are done testing, test it MORE. Use lots of println statements to verify that it really is working. ONLY when you are really, really, really sure that it works should you even CONSIDER writing the code to count. Then test the heck out of THAT code.

con't forget to test such things as the file not existing, the file being empty, and everything else you can think of...You will NEVER go wrong by compiling and testing too much.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jay Jeong
Greenhorn

Joined: Nov 16, 2011
Posts: 6
//Random Numbers




What does this mean?
RandomNumbers.java:12: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Scanner reader = new Scanner(new File("numbers.dat"));
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

If you look at the API for Scanner(File) you will see it throws a FileNotFoundException. If you follow the link for FileNotFoundException, you will see when and why it would be thrown (though it should be relatively obvious from the name).

What the error message says is that the FileNotFoundException is a show-stopper. If the exception occurs then the application can't complete and will crash in a flaming mess. So you are left with two options:
1) Deal with it now by using a try{}catch(FileNotFoundException fnfe){} and determine what to do to work around the exception, or
2) Deal with it in a different part of the application (the calling method) by adding throw(FileNotFoundException) to this method's signature. Of course if you do this, you are just passing the buck and you will be left with the same choice elsewhere.

Which you choose depends on where you think you can handle it best. If you still aren't following, then you should look here: Java Tutorial: Exceptions


Steve
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7772
    
  21

Jay Jeong wrote:What does this mean?
RandomNumbers.java:12: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Scanner reader = new Scanner(new File("numbers.dat"));

I notice you didn't even acknowledge fred's post and are still ploughing ahead with Java.

His advice, and for your benefit, I'll give it to you again:
Forget your code. describe in English how you would do this. no computers, no data structures, no 'int's or floats, no methods...just English words.
is as good as it gets when it comes to starting out programming.

Until you can describe the problem (and it's solution) in English (or your native language), no amount of Java will help you; and in fact it will probably distract you.

Good programmers spend a lot more time thinking than they do coding.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jay Jeong wrote:hey jeff, thanks for replying to my posts. WHen you mean java.util.random, should I replace it with the java.Scanner.util?


No. They serve two completely different purposes. You would still use Scanner to read the input. You'd use Random to generate random numbers to pic which of the inputs to select.

and as for double number > 0 && < 100; , I'm trying to get the scanner to read all numbers less than 100 and greater than 0, because the assignment requires me to generate three numbers (1-99) and have it declare whether the random numbers are in the numbers.dat file or not


If you read the docs for Random, you'll see that it provides a method to generate numbers in a range.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AP Computer Science assignment help