Results 1 to 12 of 12
Thread: array input block
- 09-09-2010, 11:51 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
array input block
can someone help me with my code....:confused:
I need to block if the input has the same value ... example: if i input 1 in array[1] and then input another 1 in array[2] it will block...
plz help :(
here's the code:
Java Code:import java.util.Arrays; import java.io.*; public class Main { BufferedReader ui = new BufferedReader(new InputStreamReader(System.in)); public String yn; public void sortIntArray()throws IOException { int num = 1; int arrayToSort[] = new int[10]; int array; for(int i=0;i<arrayToSort.length;i++){ do{ try{ do{ System.out.print("NAME"+" "+ num+" :"); array=Integer.parseInt(ui.readLine()); // here i need to block the same number.... Dx arrayToSort[i]=array; else System.out.println("error! yo already typed this number"); }while(true); num++; if(array>=0)break; else System.out.println("error"); if(arrayToSort[i]==arrayToSort[i])System.out.println("error"); }catch(NumberFormatException nfe){System.out.println("error!");} }while(true); } Arrays.sort(arrayToSort); for (int i = 0; i < arrayToSort.length; i++) System.out.println(arrayToSort[i]); } public static void main(String[] args)throws IOException { Main main = new Main(); main.sortIntArray(); main.exit(); } public void exit()throws IOException{ System.out.println("Do you want to exit? type: yes or no"); yn=ui.readLine(); if (yn.equalsIgnoreCase("yes")) System.exit(0); if (yn.equalsIgnoreCase("no")) { sortIntArray(); } if (yn!="yes") { System.out.println("error");exit(); } } }
- 09-09-2010, 12:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Use a Set<Integer>. Each time you have read a number check if it's in the set. If it is the user had typed a duplicate number. If it isn't accept the number and add it to your set.
kind regards,
Jos
- 09-09-2010, 12:17 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
TT^TT I don't get it.... but thnx for d reply....
- 09-09-2010, 02:58 PM #4
Can you explain what you mean by "block"?I need to block if the input
One usage is that the code stops executing until an event happens.
For example a read operation will block waiting for the user to enter some thing.
- 09-11-2010, 09:24 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
by "block or trap" I mean if you enter the same integer like 3 and next 3 again it will go System.out.println("sorry you've inputed this number earlier, plz input another number");
..... x(
- 09-11-2010, 09:56 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 09-11-2010, 10:22 AM #7
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
@ Josah: Ive searched it in the internet on how to use Set<Integer> but don't seem to find any tutorial or explanation of how to use it TT^TT
- 09-11-2010, 10:33 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Never try to program anything without having this available; at least bookmark it or even better: download the entire thing so you can have it available everywhere.
kind regards,
Jos
- 09-11-2010, 11:25 PM #9
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Like Jos has being saying a Set will not accept duplicates, if you study the Set class and its methods you will solve the problem
Set (Java Platform SE 6)
- 09-12-2010, 07:14 AM #10
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
thnx guys............. I found this while searching... is it like this?
Java Code:import java.util.HashSet; import java.util.Set; public class FindDups { public static void main(String args[]) { Set s = new HashSet(); String[] values = new String[] { "java", "java2", "java2s", "javas", "java" }; for (int i = 0; i < values.length; i++) if (!s.add(values[i])) System.out.println("Duplicate detected: " + values[i]); System.out.println(s.size() + " distinct words detected: " + s); } }Last edited by waklo99; 09-12-2010 at 07:59 AM.
- 09-18-2010, 12:58 AM #11
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
well has that code worked?
- 09-20-2010, 03:04 AM #12
Member
- Join Date
- Jan 2010
- Posts
- 20
- Rep Power
- 0
Similar Threads
-
Need help with array and storing input
By Bewitched1 in forum New To JavaReplies: 8Last Post: 07-24-2010, 05:16 PM -
Inserting input into array
By brmcdani in forum New To JavaReplies: 3Last Post: 02-05-2010, 02:33 PM -
Array Input
By Rose88 in forum New To JavaReplies: 2Last Post: 04-19-2009, 10:39 PM -
input placed in array
By smilejava in forum New To JavaReplies: 5Last Post: 11-12-2007, 07:29 AM -
input placed in array
By smilejava in forum New To JavaReplies: 1Last Post: 11-05-2007, 12:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks