| | |
Question about Arrays
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 8
Reputation:
Solved Threads: 0
I'm working on a problem where I need to use a one-dimensional array to write an application that inputs five numbers, each between 10 and 100, inclusive. As each number is read, it displays it only if it is not a duplicate of a number already
read. The program should keep asking for input until 5 unique numbers are input. Use the smallest possible array to solve this program. Display the complete set of unique values input after the user enters each new value.
I'm stuck. Everything works correctly except for the nested if loop. It always accepts this for true. Any ideas on how I might rearrange my code to make this work properly? Thanks in advance!
Also, I apologize for the sloppy code. I don't know how to make it translate to look pretty on here.
read. The program should keep asking for input until 5 unique numbers are input. Use the smallest possible array to solve this program. Display the complete set of unique values input after the user enters each new value.
I'm stuck. Everything works correctly except for the nested if loop. It always accepts this for true. Any ideas on how I might rearrange my code to make this work properly? Thanks in advance!
Also, I apologize for the sloppy code. I don't know how to make it translate to look pretty on here.
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class UniqueTest { public static void main(String[] args) { //declare an array with 5 elements int num[] = new int[5]; int index = 0; Scanner input = new Scanner(System.in); //declare the input object (she didn't do this for us) while(index < num.length){ System.out.println("Enter an integer between 10 and 100: "); //accept a keyboard input and assign it to a variable int number = input.nextInt(); if (number>=10&&number<=100) { ++index; num[index] += number; System.out.printf("%d\n", num[index]); if (number == num[index]) { System.out.println("Value has already been entered."); } } else { System.out.println("Error: You did not enter a number between 10 and 100."); } } } }
Last edited by ~s.o.s~; Nov 18th, 2008 at 11:04 pm. Reason: Fixed code tags.
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 137
you are doing a single check with the if loop
do a for
do a for
Java Syntax (Toggle Plain Text)
bool contains = false; for(int i=0; i<index; i++) { if (number == num[i]) { System.out.println("Value has already been entered."); contains = true; } } if(!contains) { num[index] = number; ++index; }
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Sep 2008
Posts: 8
Reputation:
Solved Threads: 0
Thank you very much for your help. That worked out perfectly. Now I just noticed one final problem with my code. Instead of saving each value in the array and displaying it like 88, then 88,53 then 88,53,44 and so on, it just repeats whatever number I have most recently entered. How can I store each number permanently into the array and display it accordingly?
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 137
use the same for loop concept, put it at the bottom of the while loop
Java Syntax (Toggle Plain Text)
for(int i=0; i<index; i++) { System.out.println(num[i]); }
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Sep 2008
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
use the same for loop concept, put it at the bottom of the while loop
Java Syntax (Toggle Plain Text)
for(int i=0; i<index; i++) { System.out.println(num[i]); }
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 137
hey man, it takes time to grasp onto the concept of programming, (althought we all can't admit it :-) ), i commend you on trying and thats what we like to see here, not just give me an answer
keep up the good work and keep showing your effort!!!
keep up the good work and keep showing your effort!!!
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Similar Threads
- C++ Char question (C++)
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Newbie Problem: Arrays (C++)
- Question on arrays & methods (Java)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- array question (C++)
- Simple (I think) Code Question (PHP)
- Help with Arrays needed! (C++)
- Completely new to C++ and have question about using char (C++)
Other Threads in the Java Forum
- Previous Thread: need help getting program to sum numbers
- Next Thread: source code for Jfilechooser
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class classes client code component css csv database eclipse ee error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javaee javaprojects jmf jni jpanel julia jvm key linked linux list loan loop map method methods mobile netbeans newbie objects oriented output panel phone print printf problem program programming project projects recursion replaydirector reporting researchinmotion robot rotatetext scanner screen se server service set size sms software sort sql string swing test threads transfer tree ubuntu windows






