I need help to change my code as instructor asked below. So I need to remove bre
ID: 3571307 • Letter: I
Question
I need help to change my code as instructor asked below. So I need to remove breake statment on my code.
"break statements don't belong in an if statement, they are only allowed in a case statement when following good programming procedures. Remove them and resubmit for a grade.. It creates spaghetti code, it's not professional. It's considered poor programming practice.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*Java Programming
*Program allows to get user inputs as one number in the ###.### format and Store the number as one String field instead of breaking it down into two fields.
**/
import java.text.DecimalFormat;
import java.util.Scanner;
public class MyLibrary
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
int MAX_BOOK = 5;
Book book1,book2,book3,book4,book5;
while(true)
{
System.out.println("Please Enter 1 to Display a book and Enter 2 to Exit");
System.out.println();
int value = input.nextInt();
switch(value)
{
case(1):
for(int i=0; i {
if(i==0)
{
book1=createBook();
printDetails(book1);
System.out.println();
System.out.println("Enter 2 to Exit or 1 to continue create a book");
int y = input.nextInt();
if(y==2)
break;
else;
}
else if(i==1)
{
book2=createBook();
printDetails(book2);
System.out.println();
System.out.println("Enter 2 to Exit or 1 to continue creating book");
int b = input.nextInt();
if(b==2)
break;
else;
}
else if(i==2)
{
book3=createBook();
printDetails(book3);
System.out.println();
System.out.println("Enter 2 to Exit or 1 to continue creating book");
int d = input.nextInt();
if(d==2)
break;
else;
}
else if(i==3)
{
book4=createBook();
printDetails(book4);
System.out.println();
System.out.println("Enter 2 to Exit or 1 to continue creating book");
int f = input.nextInt();
if(f==2)
break;
else;
}
else if(i==4)
{
book5=createBook();
printDetails(book5);
}
else;
}
break;
case(2):
System.out.println();
System.out.println("Exit");
System.out.println();
break;
default:
}
break;
}
System.out.println();
System.out.println("Exit");
System.out.println();
}
public static Book createBook()
{
System.out.println("Please Enter Book Author");
String bookAuthor=input.next();
System.out.println("Please Enter Book Title");
String bookTitle=input.next();
System.out.println("Please Enter bookid in ###.### format ");
String bookid=input.next();
boolean checkin=true;
Book bk= new Book(bookAuthor,bookTitle,bookid, checkin);
System.out.println("----------------------");
return bk;
}
public static void printDetails(Book book)
{
DecimalFormat df = new DecimalFormat("###.###");
double d=Double.parseDouble(book.getbookId());
System.out.println();
System.out.println ("Title: " + book.getTitle());
System.out.println ("Author: " + book.getAuthor());
System.out.printf (" booknumber: %3.3f%n ",d);
System.out.println ("checked in: " + book.getisIn());
System.out.println(" count is : "+ book.getTotalBooks());
System.out.println(" count is : "+ book.gettotalBooksIn());
System.out.println("----------------------");
if((d<100)&&(d>=000))
System.out.println ("the bookclass category is: 000 – General, Computer science and Information ");
if((d<200)&&(d>=100))
System.out.println ("the bookclass category is: 100 – Philosophy and psychology ");
if((d<300)&&(d>=200))
System.out.println ("the bookclass category is: 200 – Religion ");
if((d<400)&&(d>=300))
System.out.println ("the bookclass category is: 300 – Social sciences ");
if((d<500)&&(d>=400))
System.out.println ("the bookclass category is: 400 – Language ");
if((d<600)&&(d>=500))
System.out.println ("the bookclass category is: 500 – Pure Science ");
if((d<700)&&(d>=600))
System.out.println ("the bookclass category is: 600 – Technology ");
if((d<800)&&(d>=700))
System.out.println ("the bookclass category is: 700 – Arts & recreation ");
if((d<900)&&(d>=800))
System.out.println ("the bookclass category is: 800 – Literature ");
if((d<1000)&&(d>=900))
System.out.println ("the bookclass category is: 900 – History & geography ");
if(d>=1000)
System.out.println ("Invalid Entry ! ");
else;
System.out.println();
}
}
Explanation / Answer
Instead of
break; Please replace with below line.
System.exit(0);
Example programme for System.exit
public class Test {
public static void main(String[] args) {
int a=1;
if(a==1)
{
System.out.println("this::");//Line 1
System.exit(0);//Line 2
System.out.println("after exit ");//Line3
}
}
}
From above programme
At line2 We are using System.exit(0) so on the console
This.. will be printing..
If we comment System.exit(0) then
This..
After exit. will be printing
so in your programme please use System.exit(0) instead of break; statement it will terminate from current excution of programme.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.