Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Need Urgent Help for Java assignment..

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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:-
    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
    Last edited by SamanthaBenny93; May 19th, 2012 at 12:12 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default 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

    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.
    Last edited by Norm; May 19th, 2012 at 11:43 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    6
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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:-
    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 ...
    Last edited by SamanthaBenny93; May 19th, 2012 at 12:10 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    SamanthaBenny93 (May 19th, 2012)

  6. #5
    Junior Member
    Join Date
    May 2012
    Posts
    6
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Need Urgent Help for Java assignment..

    I found the problem
    Please explain what the problem is.

    What do the printlns show for the values used in those if statements?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    May 2012
    Posts
    6
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

  9. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,539
    Thanks
    24
    Thanked 296 Times in 280 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,961
    My Mood
    Hungover
    Thanks
    133
    Thanked 553 Times in 474 Posts

    Default Re: Need Urgent Help for Java assignment..

    Moving this from the Member Introduction forum. Please read this: This Forum is for Saying Hi, not for Asking Technical Questions (READ BEFORE POSTING)

    You might also consider the link in my signature on asking questions the smart way, especially the bit about using informative titles.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #11
    Junior Member
    Join Date
    May 2012
    Posts
    6
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need Urgent Help for Java assignment..

    Apologies for posting in the wrong section ...i joined pretty recently..ill take your info into consideration..

  13. #12
    Junior Member
    Join Date
    May 2012
    Posts
    6
    My Mood
    Depressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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 &&

Similar Threads

  1. help with array assignment ! urgent please :(
    By dre2327 in forum Collections and Generics
    Replies: 5
    Last Post: October 6th, 2011, 01:30 PM
  2. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  3. Need urgent help in assignment of JAVA, any idea suggestion plz
    By aesthete in forum Java Theory & Questions
    Replies: 2
    Last Post: January 6th, 2011, 04:58 AM
  4. nid help for my assignment~urgent
    By x3ahbi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 5th, 2010, 02:55 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM