Results 1 to 9 of 9
- 02-22-2013, 08:05 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
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 ..gif)
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(); } }
- 02-22-2013, 08:33 AM #2
Member
- Join Date
- Feb 2013
- Location
- Islamabad, Pakistan
- Posts
- 25
- Rep Power
- 0
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 ........
- 02-22-2013, 09:44 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
Re: Searching and result more than one.
Sorry I didnt get you Jamil37037.
- 02-22-2013, 09:55 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-22-2013, 11:29 AM #5
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
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
- 02-22-2013, 11:42 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 02-22-2013, 11:53 AM #7
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
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
- 02-22-2013, 12:26 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 02-25-2013, 10:42 AM #9
Member
- Join Date
- Jan 2013
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
After End Of Result set error?
By mrhid6 in forum JDBCReplies: 14Last Post: 10-09-2011, 05:03 PM -
Why I get the result is NULL
By okokok in forum New To JavaReplies: 5Last Post: 09-19-2011, 04:59 AM -
Keep getting result of 0.
By dookie1293 in forum New To JavaReplies: 7Last Post: 06-09-2011, 05:01 AM -
Struts 2 error : No result defined for action / result
By sameerk in forum Web FrameworksReplies: 1Last Post: 05-17-2011, 10:15 AM -
uncorrect result
By jamborta in forum New To JavaReplies: 3Last Post: 11-11-2009, 01:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks