Results 1 to 17 of 17
  1. #1
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default [SOLVED] Need help with Java application

    I'm new to Java (only a few weeks under my belt) and struggling with an application. The project is to write an app that inputs 5 numbers between 10 and 100, not allowing duplicates, and displaying each correct number entered, using the smallest possible array to solve the problem. Output example:

    Please enter a number: 45
    Number stored.
    45
    Please enter a number: 54
    Number stored.
    45 54
    Please enter a number: 33
    Number stored.
    45 54 33

    etc.

    I've been working on this project for days, re-read the book chapter multiple times (unfortunately, the book doesn't have this type of problem as an example to steer you in the relatively general direction) and am proud that I've gotten this far. My problems are 1) I can only get one item number to input rather than a running list of the 5 values, 2) I can't figure out how to check for duplicate numbers. Any help is appreciated.

    My code is as follows:

    import java.util.Scanner; // program uses class Scanner

    public class Array
    {

    public static void main( String args[] )
    {
    // create Scanner to obtain input from command window
    Scanner input = new Scanner( System.in);

    // declare variables
    int array[] = new int[ 5 ]; // declare array named array
    int inputNumbers = 0; // numbers entered


    while( inputNumbers < array.length )
    {

    // prompt for user to input a number
    System.out.print( "Please enter a number: " );
    int numberInput = input.nextInt();

    // validate the input
    if (numberInput >=10 && numberInput <=100)
    System.out.println("Number stored.");
    else
    System.out.println("Invalid number. Please enter a number within range.");

    // checks to see if this number already exists
    boolean number = false;

    // display array values
    for ( int counter = 0; counter < array.length; counter++ )
    array[ counter ] = numberInput;

    // display array values
    System.out.printf( "%d\n", array[ inputNumbers ] );

    // increment number of entered numbers
    inputNumbers++;
    }
    }

    } // end close Array

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    I actually hadn't in my app. I'd used a pretty long name, so I'd just changed it to "Array" when I posted it since it was shorter. Good point, thought. Thx.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Here is one simple logic to do this comparison. Once you read a value from the user, compare it with the whole array. It's easy since you have a counter. Here in addition I use a label to skip the rest of the process.

    Java Code:
    public class CollectUserData {
    
        public static void main( String args[] ) {
    
            // create Scanner to obtain input from command window
            Scanner input = new Scanner( System.in);
    
            // declare variables
            int array[] = new int[ 5 ]; // declare array named array
            int inputNumbers = 0; // numbers entered
    
            outer:
            while( inputNumbers < array.length ) {
                // prompt for user to input a number
                System.out.println( "Please enter a number: " );
                int numberInput = input.nextInt();
    
                // Check for duplicate
                for(int i = 0; i < array.length; i++) {
                    if(array[i] == numberInput) {
                        System.out.println("Duplicate number is found");
                        continue outer;
                    }
                }
                
                // validate the input
                if (numberInput >=10 && numberInput <=100) {
                    System.out.println("Number stored.");
                    array[inputNumbers] = numberInput;
                }
                else {
                    System.out.println("Invalid number. Please enter a number within range.");
                }
                
                // display array values
                for(int val = 0; val <= inputNumbers; val++) {
                    System.out.print(array[val] + " ");
                }
    
                // increment number of entered numbers
                inputNumbers++;
            }
        }
    }

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Brackets

    jhrh95:
    Suggestion on your coding style... always use curly brackets "{}" with your if, else and for statements, even if it's a one liner. This way you can add lines of code without any problems and save yourself head aches later on.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also commenting the code. It's better if you can keep them much shorter with clearly descriptive.

    At the same time, please use CODE tags when you post code segments again here in the community.

  7. #7
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    I tried to do that, but it didn't take. Sorry! I didn't mean to make it difficult to read. Thank you so much for pointing me in the right direction. In reviewing the changes you made, I wasn't as far off as I thought I was... it looks like I was just missing pieces. I modified the changes you gave me so that it also doesn't show a 0 value for numbers entered outside of the range (<10 && >100). Thank you again for your guidance.

  8. #8
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    Thanks for the feedback.

  9. #9
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    For my own knowledge, how could I have made it work without using 'outer', and 'continue outer'? My understanding of Java (again, please remember, I'm a beginner) is they refer to nested classes and it's referring to an outer public class. Right? But that shouldn't be required to make the correct values show. I've been playing around with the code trying to understand it better. Rather than outer / continue outer, I tried another while statement to force a loop. It worked except that it now allows 0's to show if the input numbers were outside the specified range (<10 and > 100) and it also allowed duplicates to appear. :(

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Nope

    ... they refer to nested classes and it's referring to an outer public class. Right?
    Wrong... you only have one class: CollectUserData. "outer" is a label and is generally asociated with the "goto" statement. It can also be used effectivaly with branching statements. See the following link:
    Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by jhrh95 View Post
    I tried to do that, but it didn't take. Sorry! I didn't mean to make it difficult to read. Thank you so much for pointing me in the right direction. In reviewing the changes you made, I wasn't as far off as I thought I was... it looks like I was just missing pieces. I modified the changes you gave me so that it also doesn't show a 0 value for numbers entered outside of the range (<10 && >100). Thank you again for your guidance.
    What you mean 0 values. In your application value zero also excepted in the following line of code.

    Java Code:
    if (numberInput >=10 && numberInput <=100) {
    That's your implementation.

  12. #12
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    I wanted the results to show unique numbers with no 0's (i.e., 34 54 67 45 23). However, when I input a number < 10 (i.e., 4) or > 100 (i.e., 3223), or a duplicate number, it would store a 0 in the results rather then ignoring the value. For example, if I entered 344 rather than 34 from the set of numbers I'd given above, the results would have shown as 0 54 67 45 23). Does that better clarify what I was trying to say?

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

    Default

    You may want to post your latest code effort, but if you do, only post code related to this problem, and it would help us immensely if this code is compilable. This is called an SSCCE, and you can read more about creating this here: SSCCE.org

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have given lots of hints here. Read them and try to workout it first as Fubarable says.

    Think a way to check that input number is not equal to zero.

  15. #15
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    Yes, it's working just fine. Thanks to all for your help. I had just wanted to clarify that I'd made more changes on my own and it worked great.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    The best thing is when you ask a question show your effort as well, post code segment to clearly say what you have done.

    Anyway, if you have solve the problem please mark it as solved.

  17. #17
    jhrh95 is offline Member
    Join Date
    Jan 2009
    Posts
    19
    Rep Power
    0

    Default

    How do I mark it solved? I checked the FAQ/Bulletin FAQ but couldn't find anything to tell me how.

Similar Threads

  1. Using openoffice in java application
    By urzaaa in forum AWT / Swing
    Replies: 5
    Last Post: 08-25-2008, 01:38 PM
  2. java application
    By jithan in forum New To Java
    Replies: 4
    Last Post: 06-30-2008, 04:30 PM
  3. Using Java in SWF Application
    By ajayk in forum SWT / JFace
    Replies: 0
    Last Post: 06-25-2008, 08:26 AM
  4. Useful Java Application Components 1.0.0-rc1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-22-2008, 04:26 PM
  5. Running a application in java
    By barney in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 07:12 AM

Posting Permissions

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