In: Computer Science
I'm trying to get my code to loop back to let the user input multiple patients.
import java.util.Scanner;
public class XYZ {
public static void main(String[] args) {
String response;
do{
//import scanner
for input
Scanner input =
new Scanner(System.in);
//prompt user
input for last name
System.out.print("Enter last name ");
//output for
last name
String s1 =
input.nextLine();
//prompt user
input for first name
System.out.print("Enter first name ");
//output for
first name
String s2 =
input.nextLine();
//prompt user
for street address
System.out.print("Enter street address ");
//output for
street address
String address =
input.nextLine();
//prompt user
for city
System.out.print("Enter city ");
//output for
city
String city =
input.nextLine();
//prompt user
for state
System.out.print("Enter state ");
//output for
state
String state =
input.nextLine();
//prompt user
for zipcode
System.out.print("Enter zip code ");
//output for zip
code
String zip =
input.nextLine();
//prompt user
for owed amount
System.out.print("Enter amount owed ");
//output for
amount owed
String amount =
input.nextLine();
//prompt user
for payment amount
System.out.print("Enter payment amount ");
//output for
payment amount
String payment =
input.nextLine();
//prompt user
for payment date
System.out.print("Enter payment date ");
//output for
payment date
String date =
input.nextLine();
//final print out output
title
System.out.printf("%80s\n", "XYZ
Community Hospital");
//final print out spacing
System.out.printf(String.format("%150s\n", "").replace(' ',
'='));
//final print out titles and
spacing
System.out.printf("%10s%30s%80s\n",
"Name", "Address", "Payment Information");
//final print out titles and
spacing
System.out.printf("%-8s %-12s %-30s
%-15s %-5s %-10s %-15s %-15s %-15s \n", "Last", "First", "Address
Line 1", "City", "State", "Zip", "Amount Owed",
"Payment Amt.", "Payment Date");
System.out.printf(String.format("%150s\n", "").replace(' ',
'='));
//final output print out
System.out.printf("%-8s %-12s %-30s
%-15s %-5s %-10s %-15s %-15s %-15s \n", s1, s2, address, city,
state, zip, amount, payment, date);
//ask user for next entry
System.out.println("Would you like
to make another entry?[Y/N]");
response = input.nextLine();
input.close();
}
while (response==("Y")); }
}
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// XYZ.java
import java.util.Scanner;
public class XYZ {
public static void main(String[] args) {
String response;
Scanner input=null;
do{
//import scanner for input
input = new Scanner(System.in);
//prompt user input for last name
System.out.print("Enter last name ");
//output for last name
String s1 = input.nextLine();
//prompt user input for first name
System.out.print("Enter first name ");
//output for first name
String s2 = input.nextLine();
//prompt user for street address
System.out.print("Enter street address ");
//output for street address
String address = input.nextLine();
//prompt user for city
System.out.print("Enter city ");
//output for city
String city = input.nextLine();
//prompt user for state
System.out.print("Enter state ");
//output for state
String state = input.nextLine();
//prompt user for zipcode
System.out.print("Enter zip code ");
//output for zip code
String zip = input.nextLine();
//prompt user for owed amount
System.out.print("Enter amount owed ");
//output for amount owed
String amount = input.nextLine();
//prompt user for payment amount
System.out.print("Enter payment amount ");
//output for payment amount
String payment = input.nextLine();
//prompt user for payment date
System.out.print("Enter payment date ");
//output for payment date
String date = input.nextLine();
//final print out output title
System.out.printf("%80s\n", "XYZ Community
Hospital");
//final print out spacing
System.out.printf(String.format("%150s\n",
"").replace(' ', '='));
//final print out titles and spacing
System.out.printf("%10s%30s%80s\n", "Name", "Address",
"Payment Information");
//final print out titles and spacing
System.out.printf("%-8s %-12s %-30s %-15s %-5s %-10s
%-15s %-15s %-15s \n", "Last", "First", "Address Line 1", "City",
"State", "Zip", "Amount Owed",
"Payment Amt.", "Payment Date");
System.out.printf(String.format("%150s\n",
"").replace(' ', '='));
//final output print out
System.out.printf("%-8s %-12s %-30s %-15s %-5s %-10s
%-15s %-15s %-15s \n", s1, s2, address, city, state, zip, amount,
payment, date);
//ask user for next entry
System.out.println("Would you like to make another
entry?[Y/N]");
response = input.nextLine();
}
while (response.equalsIgnoreCase("Y"));
input.close();
}
}
_________________________
Output:
Enter last name Faulkner
Enter first name James
Enter street address 101 Church Street
Enter city Banglore
Enter state Karnataka
Enter zip code 560068
Enter amount owed 5000
Enter payment amount 45000
Enter payment date 12/12/2019
XYZ Community Hospital
======================================================================================================================================================
Name Address Payment Information
Last First Address Line 1 City State Zip Amount Owed Payment Amt.
Payment Date
======================================================================================================================================================
Faulkner James 101 Church Street Banglore Karnataka 560068 5000
45000 12/12/2019
Would you like to make another entry?[Y/N]
y
Enter last name Roy
Enter first name Rahul
Enter street address 2nd Lake View Street
Enter city Banglore
Enter state Karnataka
Enter zip code 560037
Enter amount owed 2300
Enter payment amount 17000
Enter payment date 12/11/2019
XYZ Community Hospital
======================================================================================================================================================
Name Address Payment Information
Last First Address Line 1 City State Zip Amount Owed Payment Amt.
Payment Date
======================================================================================================================================================
Roy Rahul 2nd Lake View Street Banglore Karnataka 560037 2300 17000
12/11/2019
Would you like to make another entry?[Y/N]
n
________________________
Mistakes You have done :
1) You close the scanner inside the loop.
2) We cant compare the two Strings objects using double equal (==) method.Since response variable is of type string we have to use equals() method or equalsIgnoreCase() method
_______________Could you plz rate me well.Thank You