Results 1 to 9 of 9
  1. #1
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Searching and result more than one.

    Hi there,

    I am trying to search my data[] which is split by "\n".
    I can successfully search the data[] but however if I have more than one of the same data in the file I cannot display them out.
    I can only display the first in the order.
    I am not getting any errors, I believe I am not using the 'for loop' properly.
    Can someone guide me on this or advice ?

    Below is my code.
    And Screenshots for ref.

    Thank you in advance .

    Java Code:
    public class searching {
    
    	private Scanner s;
    
    	public void openFile() {
    		try {
    			s = new Scanner(new File("carsDB.csv"));
    		} catch (Exception e) {
    			JOptionPane.showMessageDialog(null, "File not found!",
    					"Car Database", JOptionPane.ERROR_MESSAGE);
    		}
    	}
    
    	public void readFile() {
    
    		String a = "";
    
    		while (s.hasNext()) {
    			a += s.next() + "\n";
    		}
    
    		String[] data = a.split("\n");
    		String[] search = a.split(",");
    
    		int result;
    		String searchKey;
    		int position;
    
    		searchKey = JOptionPane
    				.showInputDialog(
    						null,
    						"Pleaes enter the Model or Cost or Make of the car to search by.",
    						"Car Database Search", JOptionPane.INFORMATION_MESSAGE);
    
    		if (searchKey == null || searchKey.trim().equals("")) {
    			JOptionPane.showMessageDialog(null, "Unable to proceed.\n"
    					+ "Exiting program...", "Car Database Search",
    					JOptionPane.INFORMATION_MESSAGE);
    			System.exit(0);
    		}
    
    		result = resultSearch(data, searchKey);
    		position = positionSearch(search, searchKey);
    
    		if (result >= 0)
    			JOptionPane.showMessageDialog(null, "Successfully searched: "
    					+ searchKey + "\nModel - Cost - Make\n" + data[result],
    					"Car Database Search", JOptionPane.INFORMATION_MESSAGE);
    		else
    			JOptionPane.showMessageDialog(null,
    					"Search unsuccessfully, no match found.",
    					"Car Database Search", JOptionPane.ERROR_MESSAGE);
    	}
    
    	private int resultSearch(String[] data, String searchKey) {
    
    		for (int i = 0; i < data.length; i++) {
    			if (data[i].contains(searchKey))
    				return i;
    		}
    		return -1;
    	}
    
    	private int positionSearch(String[] search, String searchKey) {
    
    		for (int i = 0; i < search.length; i++) {
    			if (search[i].contains(searchKey))
    				return i;
    		}
    		return -1;
    	}
    
    	public void closeFile() {
    		s.close();
    	}
    }
    Searching and result more than one.-screen-shot-2013-02-22-3.03.30-pm.png
    Searching and result more than one.-screen-shot-2013-02-22-3.03.40-pm.png

  2. #2
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: Searching and result more than one.

    i may prefer you to use \t in first case you are getting right formatted output coz name,model of car are too long and they are taking enough space ...if you search a car of model M3 then it will show you unformatted output in first case as well ........

  3. #3
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: Searching and result more than one.

    Sorry I didnt get you Jamil37037.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Searching and result more than one.

    Your search method is going to have to return an array then, if you want multiple results.
    Which means your pop-up text is going to have to be built up from that array, rather than a single result.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Searching and result more than one.

    Hi jaymaan,

    have you to return the Array list because you returned only integer so you have only one variable...
    So try to return array list... Try like this...

    Java Code:
      class searching {
     
        String s;
           public static void main(String[] args) {
    searching s=new searching();
     s.openFile();
     s.readFile();
           }
     
        public void openFile() {
            try {
                s = "a,\nb,\nc,\na,";
               
                 System.out.println(s);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "File not found!",
                        "Car Database", JOptionPane.ERROR_MESSAGE);
                
    
            }
        }
     
        public void readFile() {
     
     
     
            String[] data = s.split("\n");
            
            String[] search = s.split(",");
     System.out.println(data[0]+" "+search[0]);
     
           ArrayList<String>  result = new ArrayList<String>();
            String searchKey;
            ArrayList<String>  position = new ArrayList<String>();
         
            searchKey =
            JOptionPane
                    .showInputDialog(
                            null,
                            "Pleaes enter the Model or Cost or Make of the car to search by.",
                            "Car Database Search", JOptionPane.INFORMATION_MESSAGE);
     
            if (searchKey == null || searchKey.trim().equals("")) {
                JOptionPane.showMessageDialog(null, "Unable to proceed.\n"
                        + "Exiting program...", "Car Database Search",
                        JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
            }
     
           result = resultSearch(data, searchKey);
           position = positionSearch(search, searchKey);
        
            if (result.size() >= 0) {
                System.out.println("result: "+result);
                JOptionPane.showMessageDialog(null, "Successfully searched: "
                        + searchKey + "\nModel - Cost - Make\n" + result,
                        "Car Database Search", JOptionPane.INFORMATION_MESSAGE);
            }
            else {
                JOptionPane.showMessageDialog(null,
                        "Search unsuccessfully, no match found.",
                        "Car Database Search", JOptionPane.ERROR_MESSAGE);
            }
        }
    
                    
     
        private ArrayList<String> resultSearch(String[] data, String searchKey) {
      ArrayList<String>  dat = new ArrayList<String>();
            dat.clear();
                for (int i=0;i<data.length;i++){
        if (data[i].contains(searchKey)){
           if(data[i].equals(",")){
               
           }
           else{
            dat.add(data[i].substring(0,data[i].length()-1) +"\n");
            System.out.println(data[i]); 
           }
        }
                }
           
          // return dat;
            return dat;
        }
     
       private ArrayList<String> positionSearch(String[] search, String searchKey) {
    ArrayList<String>  dat = new ArrayList<String>();
                   for (String s : search){
        if (s.startsWith(searchKey)){
            dat.add(s+"\n");
            System.out.println(s); 
        }
                }
           
          // return dat;
            return dat;
            
        }
     
        
    }

    I didn't format the code what you want... I am just return the array list using your code...
    Last edited by tamilarasi; 02-22-2013 at 12:37 PM.
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Searching and result more than one.

    I have mentioned to you before that you need to wrap code in [code] tags [/code], not [quote] tags [/quote].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Searching and result more than one.

    Hi tolls,

    I used the code tag from tool bar that is [QUOTE]tag</QUOTE]
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: Searching and result more than one.

    Quote Originally Posted by tamilarasi View Post
    Hi tolls,

    I used the code tag from tool bar that is [QUOTE]tag[/QUOTE]
    There is no CODE tag tool.
    You have to do the wrapping yourself.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    jaymaan is offline Member
    Join Date
    Jan 2013
    Posts
    21
    Rep Power
    0

    Default Re: Searching and result more than one.

    Hi tamilarasi,

    Thanks, I will try and let you guys know if works or not.

Similar Threads

  1. After End Of Result set error?
    By mrhid6 in forum JDBC
    Replies: 14
    Last Post: 10-09-2011, 05:03 PM
  2. Why I get the result is NULL
    By okokok in forum New To Java
    Replies: 5
    Last Post: 09-19-2011, 04:59 AM
  3. Keep getting result of 0.
    By dookie1293 in forum New To Java
    Replies: 7
    Last Post: 06-09-2011, 05:01 AM
  4. Struts 2 error : No result defined for action / result
    By sameerk in forum Web Frameworks
    Replies: 1
    Last Post: 05-17-2011, 10:15 AM
  5. uncorrect result
    By jamborta in forum New To Java
    Replies: 3
    Last Post: 11-11-2009, 01:17 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
  •