I've a problem in displaying the attribute of object from list...

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

52 Replies - 1313 Views - Last Post: 01 December 2013 - 10:56 AM Rate Topic: -----

#46 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 30 November 2013 - 02:01 PM

blackcompe
I did look it up, but really couldn't understand. I even tried using it in the loop I'm having problem with, yet no difference. Kindly, let me know exactly its use so that I can be able to solve this problem I'm facing.

Thanks alot for your time and help. I appreciate it...
Was This Post Helpful? 0
  • +
  • -

#47 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: I've a problem in displaying the attribute of object from list...

Posted 30 November 2013 - 03:13 PM

Look through the Java 6 EE docs in the section on page directives or search google for a tutorial. I'm sure there are plenty.

Edit: Wrong thread.

ammaar12: I'm not about to sit here to teach you how to use 'continue' statements. Look through the Java tutorials or one of the million tutorials in any programming language that has a 'continue' keyword.

You said you want to remove the redundant code from the second else if block, and I told you that you can by just restarting the loop again. Just put a 'continue' statement in the block and remove everything else.

This post has been edited by blackcompe: 30 November 2013 - 03:16 PM

Was This Post Helpful? 1
  • +
  • -

#48 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 30 November 2013 - 04:47 PM

Yeah, i saw some of the coding examples on 'continue' and added to my code, but, there was an error in the continue statement..Please check it out as I've posted the code below.. Thanks!

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
	public static void main(String[] args) {
		Scanner read = new Scanner (System.in);
		String name ;
		Integer sc,qty,inp;
		ShoppingCart myCart = new ShoppingCart();
	    double priceFind;
		boolean cont=false;
		
		 Store s = new Store();
		 
	    do {
			System.out.println("Choose the department from the following: \n1)Grocery \n2)Electronics \n3)" +
					"Clothing and Accessories \n4)Health and Beauty \n5)Stationery");
			sc=Integer.parseInt(read.next());
			System.out.print("Items\t\t\tPrice\n");
		here:
				s.getDepartment(sc);			
			System.out.println("ENTER THE NAME OF ITEM AND QUANTITY:");
			System.out.print("Name of the item: ");
			name = read.next();
			priceFind=s.checkDept(name);
			System.out.print("Its Quantity: ");
			qty = Integer.parseInt(read.next());
			myCart.cart.add(new Item(name,priceFind,qty));
			/*if(qty>1){
			System.out.println(qty +" "+ name.toUpperCase()+"S have been added to your Shopping Cart!!\n");}
			else{
				if(qty==1)
			System.out.println(qty +" "+ name.toUpperCase()+" has been added to your Shopping Cart!!\n");}
				*/		
			System.out.println("WHAT DO YOU WANT TO DO NEXT? \n1) Shop here more... " +
					"\n2) Leave this department..  \n3) Checkout");
			inp=Integer.parseInt(read.next());
			switch(1) {
			case 1: continue here; [b]//ERROR HERE[/b]
			case 2: cont=true;
			case 3: cont=false;
			} 
	    }
		while(cont);
	    
	    System.out.print("***SHOPPING CART***\n ITEMS\t\tPRICE\n");
		myCart.getCartItems();
		DecimalFormat newFormat = new DecimalFormat("0.00");
		double twoDecimalBill =  Double.valueOf(newFormat.format(myCart.totalBill()));
		System.out.print("TOTAL BILL= "+ "AED "+ twoDecimalBill);
	    }
	}



And the error says 'the label here is missing'. I've tried so many things like replacing the if-else with switch, removing the brackets etc.. I can't figure out the mistake!!

This post has been edited by andrewsw: 30 November 2013 - 04:53 PM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#49 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3518
  • View blog
  • Posts: 12,012
  • Joined: 12-December 12

Re: I've a problem in displaying the attribute of object from list...

Posted 30 November 2013 - 04:58 PM

When using continue label; the label must appear just before the opening loop-statement, to signify which loop it is continuing from. It is used when there are nested-loops.

You don't have nested-loops so you only need to use continue;. Otherwise, move the label to the line before the do-statement.
Was This Post Helpful? 0
  • +
  • -

#50 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: I've a problem in displaying the attribute of object from list...

Posted 30 November 2013 - 05:11 PM

ammaar12: Don't complicate this. All you had to do was literally remove the code inside that else if block and put 'continue'. That says OK, just skip the rest of the loop code and start at the beginning.

 else if (inp==2) continue;


Was This Post Helpful? 1
  • +
  • -

#51 ammaar12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 21-September 13

Re: I've a problem in displaying the attribute of object from list...

Posted 01 December 2013 - 12:56 AM

View Postblackcompe, on 30 November 2013 - 05:11 PM, said:

ammaar12: Don't complicate this. All you had to do was literally remove the code inside that else if block and put 'continue'. That says OK, just skip the rest of the loop code and start at the beginning.

 else if (inp==2) continue;



Yeah, I've been trying the same thing, but it skips the statements after it and exits the loop and shows the total bill.. :look:

do {
			System.out.println("Choose the department from the following: \n1)Grocery \n2)Electronics \n3)" +
					"Clothing and Accessories \n4)Health and Beauty \n5)Stationery");
			sc=Integer.parseInt(read.next());
			System.out.print("Items\t\t\tPrice\n");

     //I've to start the loop from here If user wants to shop more in the same department....
			s.getDepartment(sc); 			
			System.out.println("ENTER THE NAME OF ITEM AND QUANTITY:");
			System.out.print("Name of the item: ");
			name = read.next();
			priceFind=s.checkDept(name);
			System.out.print("Its Quantity: ");
			qty = Integer.parseInt(read.next());
			myCart.cart.add(new Item(name,priceFind,qty));
			/*if(qty>1){
			System.out.println(qty +" "+ name.toUpperCase()+"S have been added to your Shopping Cart!!\n");}
			else{
				if(qty==1)
			System.out.println(qty +" "+ name.toUpperCase()+" has been added to your Shopping Cart!!\n");}
				*/		
			System.out.println("WHAT DO YOU WANT TO DO NEXT? \n1) Shop here more... " +
					"\n2) Checkout..  \n3)Leave this department...");
			inp=Integer.parseInt(read.next());
			if(inp==1)continue; //it should show the same department items.
			else if(inp==2)cont=false;
			else if(inp==3)cont=true; 
	    }
		while(cont);


Was This Post Helpful? 0
  • +
  • -

#52 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: I've a problem in displaying the attribute of object from list...

Posted 01 December 2013 - 01:24 AM

View Postandrewsw, on 30 November 2013 - 07:58 PM, said:

You don't have nested-loops so you only need to use continue;. Otherwise, move the label to the line before the do-statement.


As andrewsw said, you should add the label to the loop. The continue inside the loop is illegal, because you can only continue on the conditional branch.

Here is an example of loops with labels: http://www.java2s.co...eledforloop.htm

I highly suggest that you create functions instead of gotos and jump statements. This creates spaghetti-code and degrades readability heavily.

This post has been edited by x68zeppelin80x: 01 December 2013 - 01:26 AM

Was This Post Helpful? 1
  • +
  • -

#53 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: I've a problem in displaying the attribute of object from list...

Posted 01 December 2013 - 10:56 AM

ammaar12: Lol, no offense, but you don't pay attention. What you're doing is different from what I'm doing. Again, do exactly what I said. I won't post back until you do, because you have no hope of moving forward.
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4