Results 1 to 12 of 12

Thread: Help with my java program.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Timefool's Avatar Primicerius
    Join Date
    Feb 2009
    Location
    Florida
    Posts
    3,921

    Default Help with my java program.

    I am making a raffle type program using java. I ask the user for the name of the entrant and number of entries. Once all the entries are in, a random number generator will pick a random number, and the entrant that relates to that number is the winner. So far, this is what I have:
    Spoiler Alert, click show to read: 
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;


    public class drawing {


    public static void main(String[] args) throws IOException {

    boolean newInput = true;

    while (newInput == true){
    System.out.println("What is the entrants name?");

    BufferedReader stdin = new BufferedReader(
    new InputStreamReader(System.in));

    String entrant = stdin.readLine();
    System.out.println(entrant);

    System.out.println("How many entries?");


    String entries = stdin.readLine();
    int numOfEntries = Integer.parseInt(entries);

    System.out.println(numOfEntries + " = numOfPosts.");
    System.out.println(entries + " = posts.");
    System.out.println("Is there another user? Y/N");



    String anotherUser = stdin.readLine();
    if (anotherUser.equalsIgnoreCase ("Y")){
    newInput = true;
    }
    else if (anotherUser.equalsIgnoreCase ("N")){
    newInput = false;
    }
    }


    int totalEntries = numOfEntries;
    int raffle[] = new int [totalEntries];

    }

    }


    The two problems I am running into are (1) how do you save a variable outside a while loop? and (2) How do I get my while loop to save each user and entry separately and at the same time add them up and put them into the drawing?

  2. #2
    GrnEyedDvl's Avatar Liberalism is a Socially Transmitted Disease
    Artifex Technical Staff

    Join Date
    Jan 2007
    Location
    Denver CO
    Posts
    23,851
    Blog Entries
    10

    Default Re: Help with my java program.

    The two problems I am running into are (1) how do you save a variable outside a while loop? and
    I am not sure what the question is here, not the way you wrote it I guess.


    (2) How do I get my while loop to save each user and entry separately and at the same time add them up and put them into the drawing?
    You need to use an array.

    Inside your loop
    List PersonsName = new ArrayList();
    PersonsName.add("entrant");


    Then just have a counter that adds +1 for each iteration of the loop.

    BEFORE the while statement
    int NumberEntries = 0


    INSIDE the while statement
    NumberEntries = NumberEntries +1


    Then when you call your random number, obviously the minimum value is 1, the maximum value is NumberEntries.

    So basically the psuedocode looks like this:

    declare PersonsName arraylist
    declare NumberEntries

    while
    Read PersonsName
    increment NumberEntries by 1

    ask for more entries, false = end loop

    generate random number

    winner = PersonsName(RandomNumber)

  3. #3
    Timefool's Avatar Primicerius
    Join Date
    Feb 2009
    Location
    Florida
    Posts
    3,921

    Default Re: Help with my java program.

    Thanks Grn, that helps alot. But your answer led to the same problem I had in the first question I asked. When I declare winner = PersonsName(RandomNumber) the PersonsName is error'd out because it cannot get the data from inside the while loop. The only solution Eclipse is giving me is to make the PersonsName a method.

  4. #4
    GrnEyedDvl's Avatar Liberalism is a Socially Transmitted Disease
    Artifex Technical Staff

    Join Date
    Jan 2007
    Location
    Denver CO
    Posts
    23,851
    Blog Entries
    10

    Default Re: Help with my java program.

    You are probably calling the variable the wrong way. Its not that the variable is stuck "inside" the loop.

    If you declare that array list, and then give it static values, you should be able to reference a particular value by its index. Like this:

    List PersonsName = new ArrayList();

    PersonsName.add("Jason")
    PersonsName.add("Joe")
    PersonsName.add("John")
    PersonsName.add("Jim")

    System.out.println(PersonsName(1));


    That should print "Joe"

    If you replace the 1 with the variable you stored your random number in, it will print that index name.

  5. #5
    Timefool's Avatar Primicerius
    Join Date
    Feb 2009
    Location
    Florida
    Posts
    3,921

    Default Re: Help with my java program.

    It still gives me the error. Here is my updated code.
    Spoiler Alert, click show to read: 

    public class drawing {


    public static void main(String[] args) throws IOException {

    . . boolean newInput = true;
    . . int NumberEntries = 0;
    . . String entrant;


    . . while (newInput == true){
    . . . System.out.println("What is the entrants name?");

    . . . BufferedReader stdin = new BufferedReader(
    . . . . new InputStreamReader(System.in));

    . . . entrant = stdin.readLine();

    . . . System.out.println("How many entries?");


    . . . String entries = stdin.readLine();
    . . . int numOfEntries = Integer.parseInt(entries);

    . . . System.out.println(entrant + " has " + numOfEntries + " posts.");
    . . . System.out.println("Is there another user? Y/N");

    . . . ArrayList PersonsName = new ArrayList();
    . . . PersonsName.add(entrant);

    . . . NumberEntries = NumberEntries + numOfEntries;

    . . . String anotherUser = stdin.readLine();
    . . . if (anotherUser.equalsIgnoreCase ("Y")){
    . . . . newInput = true;
    . . . }
    . . . else if (anotherUser.equalsIgnoreCase ("N")){

    . . . . newInput = false;

    . . . }

    . . }



    . . Random random_stuff = new Random();

    . . int winner = random_stuff.nextInt(NumberEntries) + 1;

    . . System.out.println(PersonsName (winner));


    . }
    }


    The red is what is giving me an error.

  6. #6
    GrnEyedDvl's Avatar Liberalism is a Socially Transmitted Disease
    Artifex Technical Staff

    Join Date
    Jan 2007
    Location
    Denver CO
    Posts
    23,851
    Blog Entries
    10

    Default Re: Help with my java program.

    Whats the error?

  7. #7
    Timefool's Avatar Primicerius
    Join Date
    Feb 2009
    Location
    Florida
    Posts
    3,921

    Default Re: Help with my java program.

    Its highlighted in red, the very last line of code. It is giving me the error "The method PersonsName(int) is undefined for the type drawing"

  8. #8
    GrnEyedDvl's Avatar Liberalism is a Socially Transmitted Disease
    Artifex Technical Staff

    Join Date
    Jan 2007
    Location
    Denver CO
    Posts
    23,851
    Blog Entries
    10

    Default Re: Help with my java program.

    I see the problem:

    NumberEntries = NumberEntries + numOfEntries;


    That line needs to be:
    NumberEntries = NumberEntries + 1;

    This way the number of entries goes up by 1 for every iteration of the loop. Your way its adding itself to itself. So the first loop it was 1, then 2, then 4, then 8 then 16, then 32.

    When you called your random number, it picked a number greater than your number of entries. So you had 4 entries (or whatever) and got a random number of 10. Then you asked the program to print entry 10, but there is no entry 10.

  9. #9
    Friend
    Moderator Emeritus

    Join Date
    Oct 2006
    Location
    Beautiful America
    Posts
    8,626

    Default Re: Help with my java program.

    The error he's getting there is that he is trying to call PersonsName() as a method. I guess he wants to print the element at winner in the PersonsName arraylist. Java doesn't have overloadable array indexers so instead of PersonsName[winner] you have to do it like this: PersonsName.get(winner)


    Retired moderator of TWC
    | Under the patronage of Atterdag

  10. #10
    Timefool's Avatar Primicerius
    Join Date
    Feb 2009
    Location
    Florida
    Posts
    3,921

    Default Re: Help with my java program.

    Grn what you are saying would allow each entrant to only get one ticket. I have to have it so each entrant can input more than one ticket. And the equation you quoted has two variables, though they are close in wording. Garnier I tried that and the problem remains. I did a test to see if it was specific to arrays and created the variable "int g = 7;" right under the array in the loop. I replaced what was the error with "g" and it gave me the same error.

  11. #11
    Friend
    Moderator Emeritus

    Join Date
    Oct 2006
    Location
    Beautiful America
    Posts
    8,626

    Default Re: Help with my java program.

    So you changed this:

    System.out.println(PersonsName (winner));

    To this:
    int g = 7;
    System.out.println(g);

    And still got this:
    "The method PersonsName(int) is undefined for the type drawing"

    I don't believe it. You probably got a different error because you try to println an int, while println only takes strings. That would be a completely different issue.


    Retired moderator of TWC
    | Under the patronage of Atterdag

  12. #12
    Timefool's Avatar Primicerius
    Join Date
    Feb 2009
    Location
    Florida
    Posts
    3,921

    Default Re: Help with my java program.

    Just replaced the variable int g = 7 with String g = "winner." Same result as before, variable could not be resolved.

Posting Permissions

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