Results 1 to 12 of 12
  1. #1
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default 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();	
    		}
    		}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  3. #3
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    TT^TT I don't get it.... but thnx for d reply....

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I need to block if the input
    Can you explain what you mean by "block"?
    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.

  5. #5
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    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(

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by waklo99 View Post
    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(
    Are you going to ignore my hint about using a Set<Integer> class just because you "don't get it"?

    kind regards,

    Jos

  7. #7
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    @ 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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by waklo99 View Post
    @ 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
    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

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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)

  10. #10
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    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.

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    well has that code worked?

  12. #12
    waklo99 is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

Similar Threads

  1. Need help with array and storing input
    By Bewitched1 in forum New To Java
    Replies: 8
    Last Post: 07-24-2010, 05:16 PM
  2. Inserting input into array
    By brmcdani in forum New To Java
    Replies: 3
    Last Post: 02-05-2010, 02:33 PM
  3. Array Input
    By Rose88 in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 10:39 PM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 07:29 AM
  5. input placed in array
    By smilejava in forum New To Java
    Replies: 1
    Last Post: 11-05-2007, 12:32 PM

Posting Permissions

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