Results 1 to 17 of 17
- 01-02-2009, 05:25 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
[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
- 01-02-2009, 06:05 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do not use the class name Array here. It's a standard API class name.
- 01-02-2009, 06:11 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
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.
- 01-02-2009, 06:41 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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++; } } }
- 01-02-2009, 02:59 PM #5
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-02-2009, 04:45 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-02-2009, 05:46 PM #7
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
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.
- 01-02-2009, 05:47 PM #8
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
Thanks for the feedback.
- 01-02-2009, 06:49 PM #9
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
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. :(
- 01-02-2009, 07:18 PM #10
Nope
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:... they refer to nested classes and it's referring to an outer public class. Right?
Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-03-2009, 03:53 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-03-2009, 04:12 AM #12
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
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?
-
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
- 01-03-2009, 04:27 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-03-2009, 05:16 AM #15
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
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.
- 01-03-2009, 05:22 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 01-04-2009, 07:14 PM #17
Member
- Join Date
- Jan 2009
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Using openoffice in java application
By urzaaa in forum AWT / SwingReplies: 5Last Post: 08-25-2008, 01:38 PM -
java application
By jithan in forum New To JavaReplies: 4Last Post: 06-30-2008, 04:30 PM -
Using Java in SWF Application
By ajayk in forum SWT / JFaceReplies: 0Last Post: 06-25-2008, 08:26 AM -
Useful Java Application Components 1.0.0-rc1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-22-2008, 04:26 PM -
Running a application in java
By barney in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks