Results 1 to 7 of 7
  1. #1
    aznmanjj is offline Member
    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Default AP Computer assignment help

    I'm trying to attempt an assignment that is past due date, and I'm really struggling. It's an assignment where I have to generate three random integers from a given .dat file (provided by the teacher). We have to output the numbers that are in the file. He recommends using nested loops. Any suggestions on how to fix my coding? here is what I have (please dont make fun, I'm totally new to programming and am having troubles understanding this in class):

    //Random Numbers

    import java.io.*;
    import java.util.Scanner;


    class RandomNumbers{
    public static void main(String[] args){
    Scanner reader = new Scanner(new File("numbers.dat"));
    double number, sum = 0;
    int count = 0;

    while (reader.hasNext()){
    double number > 0 && < 100;


    }
    }
    }

    If anyone could help me, I'd appreciate it (explain ur help, because I don't understand these terms,)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: AP Computer assignment help

    You check if the Scanner variable, reader, has a next token to read via reader.hasNext(), but you never actually read it in via reader.next(). I suggest reading up on how to use a Scanner object including the Scanner API.

  3. #3
    srossd is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: AP Computer assignment help

    A Scanner object will read your file, and can retrieve doubles one at a time. Here's a way to read all the numbers from the file (assuming you want to check that those conditions are true):

    Java Code:
    ArrayList<Double> nums = new ArrayList<Double>();
    while(reader.hasNext()) {
    double d = reader.nextDouble();
    if(d > 0 && d < 100)
    nums.add(d);
    }
    From there, I recommend you read about Collections - there's an easy way of picking three random numbers.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: AP Computer assignment help

    To srossd: In my experience you are much better of to match the scanner hasNextXXX with the same nextXXX. So if you are checking hasNext(), then you can call next(), or if you are checking hasNextDouble() then you can call nextDouble(), but you're asking for a lot of trouble if you check with hasNext() and then callNextDouble() since you're not going to screen out non-numeric input.

  5. #5
    srossd is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: AP Computer assignment help

    True, I hadn't thought about that - could you loop over d=reader.nextDouble(), and then add d to the ArrayList inside the loop?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: AP Computer assignment help

    Why not test it out for yourself and see? That's what I would do if it were my project.

  7. #7
    srossd is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: AP Computer assignment help

    Well, I was having a bit of trouble getting it to work, but I figured it out - thanks.

Similar Threads

  1. how to get computer ip by hostname
    By vic_Rattlehead in forum Networking
    Replies: 3
    Last Post: 10-07-2011, 02:19 PM
  2. Help with Computer Ai
    By SilentCoder in forum Java Gaming
    Replies: 14
    Last Post: 06-10-2011, 01:37 AM
  3. How to access to a LAN computer
    By cjavierm in forum New To Java
    Replies: 5
    Last Post: 12-02-2010, 05:57 PM
  4. how to log data being sent to another computer
    By jdizon in forum Networking
    Replies: 4
    Last Post: 08-10-2010, 12:19 PM
  5. I am new, can anyone help me in Computer Vision?
    By howardL in forum Introductions
    Replies: 0
    Last Post: 04-23-2009, 09:37 AM

Tags for this Thread

Posting Permissions

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