Need Urgent Help for Java assignment..
I am suppose to read a text file which contains info on cars and i have to display the filtered result on a defaultlistmodel....my code works fine when one combo box is chosen....but wen i give the condition if two combo boxes are chose it displays the || result on the list....how do i solve it....i need display only the && result on list(but the filter works just fine in the console)
Here is the code:-
Code java:
public void actionPerformed(ActionEvent a) {
// TODO Auto-generated method stub
String result[];
String combobox[]={combo[0].getSelectedItem().toString(),combo[1].getSelectedItem().toString(),
combo[2].getSelectedItem().toString(),combo[3].getSelectedItem().toString(),
combo[4].getSelectedItem().toString(),combo[5].getSelectedItem().toString()};
// search is a JButton..
if(a.getSource()==search){
list.clear();
try{
FileInputStream fin=new FileInputStream ("carList.txt");
Scanner scanner = new Scanner(fin);
scanner.useDelimiter(";");
scanner.skip("// List of cars to use:");
char d='"';
while (scanner.hasNext()){
String s1=scanner.next().replace(d, ' ');
String s2=s1.replace("(", " ");
String s3=s2.replace(")", " ");
String s4=s3.replace("new","");
String s5=s4.replace(" ", "");
String s6=s5.replace("int", "*");
String s7=s6.replace("float", "");
result=scan.split(",");
// When only one combobox option is chosen
String scan=s7.replace("CarSpec", "");
if(scan.contains(combobox[0])||scan.contains(combobox[1])||scan.contains(combobox[2])||scan.contains(combobox[3])
||scan.contains(combobox[4])||scan.contains(combobox[5])){
list.addElement(result[2]+" "+result[3]+","+result[0]);
}
// when all the option are any
if(combobox[0]=="Any"&&combobox[1]=="Any"&&combobox[2]=="Any"&&combobox[3]=="Any"&&
combobox[4]=="Any"&&combobox[5]=="Any"){
list.addElement(result[2]+" "+result[3]+","+result[0]);
}
//when options from combobox1 and combobox2 are chosen...combobox[0] =combo[1].getSelectedItem().toString
if(scan.contains(combobox[0])&&scan.contains(combobox[1])){
list.addElement(result[2]+" "+result[3]+","+result[0]);
System.out.println(result[2]+" "+result[3]+","+result[0]);
}
if(scan.contains(combobox[0])&&scan.contains(combobox[2])){
list.addElement(result[2]+" "+result[3]+","+result[0]);
System.out.println(result[2]+" "+result[3]+","+result[0]);
}
}
Please Help
Re: Need Urgent Help for Java assignment..
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Quote:
wen i give the condition if two combo boxes are chose it displays the || result on the list
Can you explain what the code is supposed to do?
What is it supposed to display in the list?
One problem I see is the use of == to compare Strings. You should use the equals() method for comparing the contents of String objects.
Re: Need Urgent Help for Java assignment..
Sorry for not being informative.....this code is suppose to read a text file line by line and add to the DefaultlistModel when a an item from a combo box is chosen.....my assignment is to filter the no. of cars When an option is chosen from the combobox.....its works fine wen one combobox is chosen but when two combo boxes are selected it displays all the items selected from 1st combo and 2nd combo.
below code is the code that is having issues:-
Code java:
list.addElement(result[2]+" "+result[3]+","+result[0]);
System.out.println(result[2]+" "+result[3]+","+result[0]);
}
if(scan.contains(combobox[0])&&scan.contains(combobox[2])){
list.addElement(result[2]+" "+result[3]+","+result[0]);
System.out.println(result[2]+" "+result[3]+","+result[0]);
}
>
the && method doesnt work for the list but it works fine for the console are..
i think it calls the first if statement as well....please tell me if you need the whole code ...
Re: Need Urgent Help for Java assignment..
&& is an operator, not a method.
Are you having trouble with the if conditions not being true (or false) when you think they should be?
Try debugging the code by adding a println before the if statement that prints out the values of all the variables used in the if statement so you can see what the computer sees. Be sure to add id Strings and delimiters so you the printed out put shows you values:
println("var=" + var + "<");
where you should replace var with the names of the variables in the program.
Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Re: Need Urgent Help for Java assignment..
I found the problem but dont know how to solve it
the problem is when this if statement is done
Code java:
if(scan.contains(combobox[0])&&scan.contains(combobox[1])){
list.addElement(result[2]+" "+result[3]+","+result[0]);
System.out.println(result[2]+" "+result[3]+","+result[0]);
}
another if statement is called as well:
Code java:
if(scan.contains(combobox[0])||scan.contains(combobox[1])||scan.contains(combobox[2])||scan.contains(combobox[3])
||scan.contains(combobox[4])||scan.contains(combobox[5])){
System.out.println(result[2]+" "+result[3]+","+result[0]);
list.addElement(result[2]+" "+result[3]+","+result[0]);
}
Re: Need Urgent Help for Java assignment..
Quote:
I found the problem
Please explain what the problem is.
What do the printlns show for the values used in those if statements?
Re: Need Urgent Help for Java assignment..
if i choose Saloon from the first and Ford from the second combobox
the first if statement with the && operator statements is only suppose to display:
Saloon Ford,MA06XVU
but instead it displays:
Saloon Ford,MA06XVU // this one is from the if statement with && operator
Saloon Ford,MA06XVU// the rest below is from the second if statement with the || operator...dont know why is it being displayed
Estate Ford,MA06UXW
Saloon BMW,DV06UHQ
Saloon BMW,BN06DHQ
Saloon Honda,LA54VUN
Saloon BMW,DV06MHQ
Estate Ford,MW04CXW
Saloon Toyota,VZ55LRT
Saloon Toyota,GH03RPK
Estate Ford,WT05KXW
Re: Need Urgent Help for Java assignment..
Perhaps you want to use else if instead of if for all of your if statements after the first one. An else if statement ONLY executes if the preceeding if (or preceeding else if) evaluated to false.
Re: Need Urgent Help for Java assignment..
Please show the code that made the print out you show in post#7.
The printout does not include the suggestions I made in post#4. How do you know what was in combobox[0] etc?
Also print out the contents of scan.
Re: Need Urgent Help for Java assignment..
Moving this from the Member Introduction forum. Please read this: http://www.javaprogrammingforums.com...e-posting.html
You might also consider the link in my signature on asking questions the smart way, especially the bit about using informative titles.
Re: Need Urgent Help for Java assignment..
Apologies for posting in the wrong section ...i joined pretty recently..ill take your info into consideration..
Re: Need Urgent Help for Java assignment..
Also i found what was wrong with my code, i found if(a&&b) and if(a||b), this type of evaluation will not occur since the || condition is also true for &&