Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Redefine Intergers

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Redefine Intergers

    Hi,
    For my program (dunno if you'd call it a program really yet ) I want to create a 'Do you want to Continue sort of thing incorporating a while loop to redo it, but because the interger will already have been defined there's an error in compilation. How can I tell Java to redefine/overwrite the number so this'll work? This is the code.

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
     
    public class ChoiceofRandomNumberwithPeople
    {
        public static void main(String[] args) throws IOException
        {
            Scanner r = new Scanner(System.in);
            Scanner l = new Scanner(System.in);
            Scanner p = new Scanner(System.in);
            Scanner q = new Scanner(System.in);
            System.out.println("Ok! Enter 4 People");
            String firstname = r.nextLine();
            String secondname = l.nextLine();
            String thirdname = p.nextLine();
            String fourthname = q.nextLine();
     
            int aNumber = 0;
            {
            int myRandom = (int) ((Math.random()) * 4);
            while (aNumber < 2)
     
            if((int)(myRandom) == 1)
            {
                System.out.println(firstname);  
                System.out.println("Do you want another Random Person?");
                int aNumber = r.nextInt();
            }
     
            if((int)(myRandom) == 2)
            {
                System.out.println(secondname);
                System.out.println("Do you want another Random Person?");
                int aNumber = r.nextInt();
            }
     
            if((int)(myRandom) == 3)
            {
                System.out.println(thirdname);
                System.out.println("Do you want another Random Person?");
                int aNumber = r.nextInt();
            }
     
            if((int)(myRandom) == 4)
            {
                System.out.println(fourthname);
                System.out.println("Do you want another Random Person?");
                int aNumber = r.nextInt();
            }
            }
        }
    }

    Thanks for your help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Redefine Intergers

    here's an error in compilation.
    Please copy and paste the full text of the error message here.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Redefine Intergers

    File: C:\Users\Edmund Cox\Java Programming\Random\Choose Maximum Random Number\ChoiceofRandomNumberwithPeople.java [line: 28]
    Error: C:\Users\Edmund Cox\Java Programming\Random\Choose Maximum Random Number\ChoiceofRandomNumberwithPeople.java:28: aNumber is already defined in main(java.lang.String[])
    File: C:\Users\Edmund Cox\Java Programming\Random\Choose Maximum Random Number\ChoiceofRandomNumberwithPeople.java [line: 35]
    Error: C:\Users\Edmund Cox\Java Programming\Random\Choose Maximum Random Number\ChoiceofRandomNumberwithPeople.java:35: aNumber is already defined in main(java.lang.String[])
    File: C:\Users\Edmund Cox\Java Programming\Random\Choose Maximum Random Number\ChoiceofRandomNumberwithPeople.java [line: 42]
    Error: C:\Users\Edmund Cox\Java Programming\Random\Choose Maximum Random Number\ChoiceofRandomNumberwithPeople.java:42: aNumber is already defined in main(java.lang.String[])
    File: C:\Users\Edmund Cox\Java Programming\Random\Choose Maximum Random Number\ChoiceofRandomNumberwithPeople.java [line: 49]
    Error: C:\Users\Edmund Cox\Java Programming\Random\Choose Maximum Random Number\ChoiceofRandomNumberwithPeople.java:49: aNumber is already defined in main(java.lang.String[])

    It's just saying it's already been defined.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Redefine Intergers

    Why do you want to define more than one variable with the same name?
    Could you give a new name to the variables where the compiler is complaining?

    Do you really want to define a new variable? Or do you want to assign a new value to an existing variable?
    Do you know the difference between
    defining a variable: <datatype> <variable name> ...
    and using an existing variable: <variable name> = ....

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Redefine Intergers

    Ok. I just removed "int" like you said and it works. Thanks.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Redefine Intergers

    Some comments on your code:
    if((int)(myRandom) == 1)
    You don't need to cast an int: if(myRandom == 1)

    You could use one Scanner here instead of 4:
            Scanner aScanner = new Scanner(System.in);
            System.out.println("Ok! Enter 4 People");
            String firstname = aScanner.nextLine();
            String secondname =aScanner.nextLine();
            String thirdname = aScanner.nextLine();
            String fourthname = aScanner.nextLine();

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Redefine Intergers

    Some advice: don't cram all your code into the main method. Learn how methods work. Then you can break your program down into smaller steps and place each step in its own method.
    Improving the world one idiot at a time!

Similar Threads

  1. Quick question (intergers and accuracy)
    By petemyster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2011, 06:22 PM