i am having prob in looping this java program...the 1st section is to choose the cloth material...then the length of material needed n lastly calculate the price of the needed length after discount...have done all of them...but having a prob were should i insert the loop to make the program run again...

i have attached the code...n i will post the code in thread too...

//Name: Kumaresen Nair    2110465
//      Muhammad Bukhari  2110453

//Algorith
// 1)allow user to make choice of cloth material
// 2)allow user to enter needed length of material
// 3)calculate the price of needed length of material after discount
// 4)print the price of the neede length of material after discount




import java.io.*;         //java package

public class ClothingMaterial{
 public static void main(String[] args) throws Exception{
  int choice;
  double length;
  double cost;
  BufferedReader object = new BufferedReader(new InputStreamReader(System.in));

  try{
     //alow user to make choice of clothing material
    System.out.println("Choose a material");
   System.out.println("1. satin    RM25/meter");
   System.out.println("2. cotton   RM20/meter  ");
   System.out.println("3. silk     RM50/meter");
   choice = Integer.parseInt(object.readLine());
   
   //alow user to enter needed length for clothing material
   switch (choice){   
    case 1:
      System.out.println("Enter how many length needed=" );
      length = Double.parseDouble(object.readLine());
      cost=((25*length)-(0.1*25*length));               //calculate the price of needed length of material after discount
      System.out.println(String.format ("Total cost of satin cloth after discount is RM%.2f" , cost ) );    //print receipt
      break;
      
    case 2:
     System.out.println("Enter how many length needed=");
      length = Double.parseDouble(object.readLine());
      cost=((20*length)-(0.15*20*length));                 //calculate the price of needed length of material after discount
      System.out.println(String.format("Total cost of cotton cloth after discount is RM%.2f" , cost  ));         //print receipt
     break;
     
    case 3:
     System.out.println("Enter how many length needed=");
     length = Double.parseDouble(object.readLine());
     cost=((50*length)-(0.2*50*length));                    //calculate the price of needed length of material after discount
     System.out.println(String.format("Total cost of silk cloth after discount is RM%.2f" , cost  ));              //print receipt
     break;
     
       default:
     System.out.println("Invalid Entry!:");
   }
  }
  catch(NumberFormatException ne){
   System.out.println(ne.getMessage() + " is not a numeric value.");
   System.exit(0);
  }

 }
}

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Attachments
//Name: Kumaresen Nair    2110465
//      Muhammad Bukhari  2110453

//Algorith
// 1)allow user to make choice of cloth material
// 2)allow user to enter needed length of material
// 3)calculate the price of needed length of material after discount
// 4)print the price of the neede length of material after discount




import java.io.*;         //java package

public class ClothingMaterial{
 public static void main(String[] args) throws Exception{
  int choice;
  double length;
  double cost;
  BufferedReader object = new BufferedReader(new InputStreamReader(System.in));

  try{
     //alow user to make choice of clothing material
    System.out.println("Choose a material");
   System.out.println("1. satin    RM25/meter");
   System.out.println("2. cotton   RM20/meter  ");
   System.out.println("3. silk     RM50/meter");
   choice = Integer.parseInt(object.readLine());
   
   //alow user to enter needed length for clothing material
   switch (choice){   
    case 1:
      System.out.println("Enter how many length needed=" );
      length = Double.parseDouble(object.readLine());
      cost=((25*length)-(0.1*25*length));               //calculate the price of needed length of material after discount
      System.out.println(String.format ("Total cost of satin cloth after discount is RM%.2f" , cost ) );    //print receipt
      break;
      
    case 2:
     System.out.println("Enter how many length needed=");
      length = Double.parseDouble(object.readLine());
      cost=((20*length)-(0.15*20*length));                 //calculate the price of needed length of material after discount
      System.out.println(String.format("Total cost of cotton cloth after discount is RM%.2f" , cost  ));         //print receipt
     break;
     
    case 3:
     System.out.println("Enter how many length needed=");
     length = Double.parseDouble(object.readLine());
     cost=((50*length)-(0.2*50*length));                    //calculate the price of needed length of material after discount
     System.out.println(String.format("Total cost of silk cloth after discount is RM%.2f" , cost  ));              //print receipt
     break;
     
       default:
     System.out.println("Invalid Entry!:");
   }
  }
  catch(NumberFormatException ne){
   System.out.println(ne.getMessage() + " is not a numeric value.");
   System.exit(0);
  }

 }
}

You can just put a

while(true)

loop around your try-catch structure. That way it will keep running forever. It you want it to run say 5 times, you just replace the while(true) loop with a for loop. As a suggestion, doing System.exit(0); if the user types something that can't be parsed into a double, isn't very user friendly.

Good luck.

This question has already been answered. Start a new discussion instead.