This is just for the sake of formality/neatness. Backlash t (\t), which is all I know, doesn't seem to work very well. Here's my code:
import java.util.Scanner;
public class RegisterStudent {
public static void register() {
System.out.println("----------------------------------------");
System.out.println("Student Class Card Evaluation System");
System.out.println("----------------------------------------");
System.out.println("Personal Data");
Scanner input = new Scanner(System.in);
System.out.print("\tEnter student no.: ");
long studNo = input.nextLong();
System.out.print("\tEnter first name: ");
String fName = input.next();
System.out.print("\tEnter last name: ");
String lName = input.next();
System.out.print("\tEnter course: ");
String course = input.next();
System.out.println();
System.out.println("----------------------------------------");
System.out.println("Subject: Computer Programming 2");
System.out.println("----------------------------------------");
System.out.println();
System.out.print("\tPrelim Grade: ");
double pg = input.nextDouble();
System.out.print("\tMidterm Grade: ");
double mg = input.nextDouble();
System.out.print("\tPrefinal Grade: ");
double pfg = input.nextDouble();
System.out.print("\tFinal Grade: ");
double fg = input.nextDouble();
System.out.println();
System.out.println("----------------------------------------");
double gwa = (pg + mg + pfg + fg)/4;
System.out.println();
System.out.println("Student Evaluated successfully...");
System.out.println("Viewing Report...");
System.out.println();
System.out.println("--------------------------------------------------------------------------------------------");
System.out.println("ID No.\t\t" + "LASTNAME\t\t" + "FIRSTNAME\t\t" + "COURSE\t\t"
+ "PG\t\t" + "MG\t\t" + "PFG\t\t" + "FG\t\t" + "GWA");
System.out.println("--------------------------------------------------------------------------------------------");
System.out.println(studNo + "\t\t" + lName + "\t\t" + fName + "\t\t" + course + "\t\t"
+ pg + "\t\t" + mg + "\t\t" + pfg + "\t\t" + fg + "\t\t" + gwa);
The output should look something like this:
Everything that the user will input should be properly aligned. Please help me fix my program, thanks guys!
My current output is this:
You see it's not properly aligned
Since this is homework, here are a couple of hints:
The printf method on a PrintWriter or PrintStream classes can do alignment. So can String.format and the Formatter class.
Don't use TAB characters (i.e. '\t') for alignment in simple text output if you want the alignment to be portable. Different OSes have different ideas on the "width" of a TAB.
The number of tabs you need on each line of input will be determined by the length of the prompt text which appears first on the line i.e. the shorter the prompt, the more tabs required. Simple trial and error should sort out your output to give you the required result.
Related
I am having trouble with my while loop. After it executes the third time, it won't take user input it just outputs "Enter your first name". I have tried multiple things but they don't seem to be working. The loop should continue to run until the user types in "No".
I know I'm close!
System.out.print("Would you like to enter another patient? Type Yes or No: ");
String userInput = input.nextLine();
if (userInput.equals("Yes"))
{
while (userInput.equals("Yes"))
{
System.out.print("Enter your first name: ");
String firstName2 = input.nextLine();
System.out.print("Enter your last name: ");
String lastName2 = input.nextLine();
System.out.print("Enter your address: ");
String address2 = input.nextLine();
System.out.print("Enter your city: ");
String city2 = input.nextLine();
System.out.print("Enter your state: ");
String state2 = input.nextLine();
System.out.print("Enter your zip code + 4: ");
String zip2 = input.nextLine();
System.out.print("Enter amount owed: ");
String amount2 = input.nextLine();
System.out.print("Enter your payment amount: ");
String payment2 = input.nextLine();
System.out.print("Enter the date of payment: ");
String date2 = input.nextLine();
System.out.println("\t\t\t\t\t" + "XYZ Hospital");
System.out.println();
System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
System.out.println();
System.out.println("Last" + "\t" + "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
System.out.println();
System.out.println(lastName2 + " " + firstName2 + "\t" + address2 + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2 + "\t\t" + payment2 + "\t\t" + date2);
System.out.print("Would you like to enter another patient? Type Yes or No: ");
}
}
else if (userInput.equals("No"))
{
System.out.println("Goodbye");
}
For these type of programs you can use do-while loops. That runs first time without condition and then you can get input from user in userInput variable.
String userInput;
do{
System.out.print("Enter your first name: ");
String firstName2 = input.nextLine();
System.out.print("Enter your last name: ");
String lastName2 = input.nextLine();
System.out.print("Enter your address: ");
String address2 = input.nextLine();
System.out.print("Enter your city: ");
String city2 = input.nextLine();
System.out.print("Enter your state: ");
String state2 = input.nextLine();
System.out.print("Enter your zip code + 4: ");
String zip2 = input.nextLine();
System.out.print("Enter amount owed: ");
String amount2 = input.nextLine();
System.out.print("Enter your payment amount: ");
String payment2 = input.nextLine();
System.out.print("Enter the date of payment: ");
String date2 = input.nextLine();
System.out.println("\t\t\t\t\t" + "XYZ Hospital");
System.out.println();
System.out.println("Name Information" + "\t\t\t\t" + "Address" + "\t\t\t\t\t\t" + "Payment");
System.out.println();
System.out.println("Last" + "\t" + "First" + "\t\t\t" + "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t" + "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount" + "\t" + "Payment Date");
System.out.println();
System.out.println(lastName2 + " " + firstName2 + "\t" + address2 + " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2 + "\t\t" + payment2 + "\t\t" + date2);
System.out.print("Would you like to enter another patient? Type Yes or No: ");
userInput = input.nextLine();
}while (userInput.equals("Yes"));
I think the problem is that you are doing the while loop inside the if statement. Try to insert the if inside the loop , like this:
Scanner input=new Scanner(System.in);
System.out.println("enter details");
String answer=input.next();
while(answer.equals("yes")) {
if(answer.equals("yes")) {
System.out.println("name");
String name=input.next();
System.out.println("last name");
String last=input.next();
System.out.println("enter details");
answer=input.next();
} }
System.out.println("bye");
}
You never get out of your loop because userInput only is set once before the loop and is never set inside it, meaning you never change the while condition.
System.out.print("Would you like to enter another patient? Type Yes or No: ");
String userInput = input.nextLine();
if (userInput.equals("Yes")) {
while (userInput.equals("Yes")) {
//your data entry...
System.out.print("Would you like to enter another patient? Type Yes or No: ");
// change userInput here, else, you're never going out of the loop.
// so add this
userInput = input.nextLine();
}
} else if (userInput.equals("No")) {
System.out.println("Goodbye");
}
Although, your solution will probably fit your needs more in this format :
System.out.print("Would you like to enter another patient? Type Yes or No: ");
String userInput = input.nextLine();
while (userInput.equals("Yes")) {
//your data entry...
System.out.print("Would you like to enter another patient? Type Yes or No: ");
userInput = input.nextLine();
}
System.out.println("Goodbye");
This way, while the user enters "Yes", you will get in the loop and get out when he enters something else. If the user enters something else than "yes" from the start, you never get in the loop and close the app right away.
Working Code : Kindly check the Code
import java.util.Scanner;
public class temp {
static String userInput;
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
System.out
.print("Would you like to enter another patient? Type Yes or No: ");
userInput = input.nextLine();
while (userInput.equals("Yes")) { //while loop will iterate till "userInput is equals to Yes"
System.out.print("Enter your first name: ");
String firstName2 = input.nextLine();
System.out.print("Enter your last name: ");
String lastName2 = input.nextLine();
System.out.print("Enter your address: ");
String address2 = input.nextLine();
System.out.print("Enter your city: ");
String city2 = input.nextLine();
System.out.print("Enter your state: ");
String state2 = input.nextLine();
System.out.print("Enter your zip code + 4: ");
String zip2 = input.nextLine();
System.out.print("Enter amount owed: ");
String amount2 = input.nextLine();
System.out.print("Enter your payment amount: ");
String payment2 = input.nextLine();
System.out.print("Enter the date of payment: ");
String date2 = input.nextLine();
System.out.println("\t\t\t\t\t" + "XYZ Hospital");
System.out.println();
System.out.println("Name Information" + "\t\t\t\t" + "Address"
+ "\t\t\t\t\t\t" + "Payment");
System.out.println();
System.out.println("Last" + "\t" + "First" + "\t\t\t"
+ "Address Line 1" + "\t" + "City" + "\t" + "State" + "\t"
+ "Zip" + "\t" + "Amount Owed" + "\t" + "Payment Amount"
+ "\t" + "Payment Date");
System.out.println();
System.out.println(lastName2 + " " + firstName2 + "\t" + address2
+ " " + city2 + ", " + state2 + " " + zip2 + "\t" + amount2
+ "\t\t" + payment2 + "\t\t" + date2);
System.out
.print("Would you like to enter another patient? Type Yes or No: \n");
userInput = input.nextLine(); // Taking user choice for "Yes" or "No"
if (userInput.equals("No")) {
break; // will Exit loop when user enter "No"
}
}
if (userInput.equals("No")) {
System.out.println("Goodbye");
} else {
System.out.println(" Invalid input :("); // will print when user enter anything other than "Yes" or "No"
}
}
}
You were not taking the input from the user again once you finished
taking data from the user inside the while loop. Also check the code
Comments for more info.
Well the big issue is that you never change the value of userInput after the very first check. So regardless of whether they type "Yes" or "No" at the end of the loop, it will keep looping. You never even give the user the option to input anything at the end of the loop when you ask if they want to enter another patient. Secondly you should probably be checking if they entered "yes, Yes, YES, y" using a to lower function
I have no idea how to put this...
basically, We are supposed to make an Array. where we input the number of students, their names, and age. after that an output will be displayed. It will show the names and age of a student.
It seems easy for you to look at. but I really need advice on how to shorten the output. like if there is anything I could do to minimize the code and make an infinite number of outputs so that I can input any number of students.
public static void main(String[] args)
{
int ageInput;
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
System.out.print("Number of Students > ");
int studentValue = input.nextInt();
String[] arraylist = new String[studentValue];
int[] ageinput = new int[studentValue];
char count;
for (count = 0; count < studentValue; count++)
{
System.out.println("Please input a student name and age:");
System.out.println("------------------------------------");
System.out.println("Name: ");
arraylist[count] = input.next();
System.out.println("Age: ");
ageinput[count] = input2.nextInt();
System.out.println(" ");
}
for (String a: arraylist)
{
System.out.println("Student #1");
System.out.println("name: " +arraylist[0]);
System.out.println("age: " +ageinput[0]);
System.out.println("Student #2");
System.out.println("name: " +arraylist[1]);
System.out.println("age: " +ageinput[1]);
System.out.println("Student #3");
System.out.println("name: " +arraylist[2]);
System.out.println("age: " +ageinput[2]);
System.out.println("Student #4");
System.out.println("name: " +arraylist[3]);
System.out.println("age: " +ageinput[3]);
System.out.println("Student #5");
System.out.println("name: " +arraylist[4]);
System.out.println("age: " +ageinput[4]);
}
}
}
I'm not very good at this stuff. soo if you could answer the question I really appreciate it :) Thank you all!
In order to use a loop to print your results i suggest you are using the following code:
for(int i = 0; i <studentValue; i++)
{
System.out.println("Student #" + i + 1);
System.out.println("name: " + arraylist[i]);
System.out.println("age: " + ageinput[i]);
}
The code works for entering wife's name and age, but fails to print the names of sons, though it displays their ages properly.
import java.util.Scanner;
public class Check2
{
public static void main(String[] args)
{
String wife;
String son1;
String son2;
int wifeAge;
int son1Age;
int son2Age;
Scanner keyboard = new Scanner(System.in);
System.out.println("Wife's name? ");
wife = keyboard.nextLine();
System.out.println("Her age? ");
wifeAge = keyboard.nextInt();
System.out.println();
System.out.println("First son's name? ");
son1 = keyboard.nextLine();
keyboard.nextLine();
System.out.println("His age? ");
son1Age = keyboard.nextInt();
System.out.println();
System.out.println("Second son's name? ");
son2 = keyboard.nextLine();
keyboard.nextLine();
System.out.println("His age? ");
son2Age = keyboard.nextInt();
System.out.println();
keyboard.nextLine();
System.out.println("My wife's name is " + wife + ". She is " +
wifeAge + " years old.\nOur first son is " +
son1 + ". He is " + son1Age + ".\nOur " +
"second son is " + son2 + ". He is " +
son2Age + ".");
}
}
You have the extra keyboard.nextLine(); in the wrong place in your code.
son1 = keyboard.nextLine();
keyboard.nextLine(); // you don't need this here.
son2 = keyboard.nextLine();
keyboard.nextLine(); // nor here
Keep the extra keyboard.nextLine(); line after each keyboard.nextInt(); and your program should run fine.
wifeAge = keyboard.nextInt();
keyboard.nextLine(); // put it here
...
son1Age = keyboard.nextInt();
keyboard.nextLine(); // and here
The nextInt() reads only an integer value and not a new line. If you need to read a new line, you will need to put the keyboard.nextLine(); every time you read integer values from the keyboard.
Hope this helps!
import java.util.Scanner;
public class Check2
{
public static void main(String[] args)
{
String wife;
String son1;
String son2;
int wifeAge;
int son1Age;
int son2Age;
Scanner keyboard = new Scanner(System.in);
System.out.println("Wife's name? ");
wife = keyboard.nextLine();
System.out.println("Her age? ");
wifeAge = keyboard.nextInt();
System.out.println();
System.out.println("First son's name? ");
keyboard.nextLine();
son1 = keyboard.nextLine();
System.out.println("His age? ");
son1Age = keyboard.nextInt();
System.out.println();
System.out.println("Second son's name? ");
keyboard.nextLine();
son2 = keyboard.nextLine();
System.out.println("His age? ");
son2Age = keyboard.nextInt();
System.out.println();
keyboard.nextLine();
System.out.println("My wife's name is " + wife + ". She is " +
wifeAge + " years old.\nOur first son is " +
son1 + ". He is " + son1Age + ".\nOur " +
"second son is " + son2 + ". He is " +
son2Age + ".");
}
}
nextLine() isn't the method you want.
You want to use next() instead, as shown in the following corrected and simplified code. You don't really want to have to manage the newlines, just let the scanner treat all the input as a string of tokens separated by whitespace, and read them in sequence. Use the next() method for inputting strings and nextInt() for inputting integers...
import java.util.Scanner;
public class Check2
{
public static void main(String[] args)
{
String wife;
String son1;
String son2;
int wifeAge;
int son1Age;
int son2Age;
Scanner keyboard = new Scanner(System.in);
System.out.println("Wife's name? ");
wife = keyboard.next();
System.out.println("Her age? ");
wifeAge = keyboard.nextInt();
System.out.println();
System.out.println("First son's name? ");
son1 = keyboard.next();
System.out.println("His age? ");
son1Age = keyboard.nextInt();
System.out.println();
System.out.println("Second son's name? ");
son2 = keyboard.next();
System.out.println("His age? ");
son2Age = keyboard.nextInt();
System.out.println();
System.out.println("My wife's name is " + wife + ". She is " +
wifeAge + " years old.\nOur first son is " +
son1 + ". He is " + son1Age + ".\nOur " +
"second son is " + son2 + ". He is " +
son2Age + ".");
}
}
I cannot get the variable adjustedCharge to print out (the text is printed but not the value) I have tried to trace but still cannot get it to print properly. No errors are present either.
import java.util.Scanner;
public class Assignment {
public static void main(String[] args) {
//Declare scanner
Scanner input = new Scanner(System.in);
//Prompt for information
System.out.print("Customer's name:");
String name = input.nextLine();
System.out.print("Customer's address:");
String address = input.nextLine();
System.out.print("Customer's phone number:");
String phone = input.nextLine();
System.out.print("Customer's licence number:");
String licence = input.nextLine();
System.out.print("Customer's credit card number:");
String ccard = input.nextLine();
System.out.print("Credit card expiry date (MM/YYYY):");
String expiry = input.nextLine();
System.out.print("Hire length (in days):");
int hire = Integer.parseInt(input.nextLine());
System.out.print("Make/Model:");
String model = input.nextLine();
System.out.print("Registration of car:");
String rego = input.nextLine();
System.out.print("Hire rate (per day) Either S, M, L:");
char inputRate = input.nextLine().charAt(0);
System.out.print("Total days hired out:");
int totalDays = Integer.parseInt(input.nextLine());
//
double dtotalDays = totalDays;
double surcharge = dtotalDays - hire;
//Daily hire rate / Stage 2
double rate = 0;
if (inputRate == 'S' || inputRate == 's'){
rate = (char) 80.0;
}
if (inputRate == 'M' || inputRate == 'm'){
rate= 110.0;
}
if (inputRate == 'L' || inputRate == 'l'){
rate= 140.0;
}
//Calculate late fees
double penalty = rate * (surcharge * 2);
//Start Stage 3
double dCost=0;
double tCost=0;
StringBuilder dDetail = new StringBuilder("The damage Details are:" + "\n");
StringBuilder tDetail = new StringBuilder("The traffic fines are:" + "\n");
//Setup an exit statement
boolean quit = false;
while (!quit){
System.out.println("<<<Enter one of the following commands:>>>");
System.out.println("A - Damage Repair");
System.out.println("B - Traffic Infringement");
System.out.println("X - Exit Menu");
String schoiceEntry = input.next();
char choiceEntry = schoiceEntry.charAt(0);
//Integer.parseInt(input.nextLine());
double damageCost = 0;
switch (choiceEntry){
case 'A':
case 'a':
input.nextLine();
System.out.println("Enter a description of the damage:");
String damageDetail = input.nextLine();
System.out.println("Enter the damage cost:");
damageCost = input.nextInt();
System.out.print("The damage is: " + damageDetail + "\n");
System.out.print("The damage cost is: " + "$" + damageCost + "\n");
dDetail.append("-" + damageDetail + "\n");
dCost = dCost + damageCost;
System.out.println("----------------------------------");
break;
case 'B':
case 'b':
input.nextLine();
System.out.print("Enter a description of the traffic infringement:");
String trafficDetail = input.nextLine();
System.out.println("Enter the traffic infringement cost:");
double trafficCost = Integer.parseInt(input.nextLine());
tDetail.append("-" + trafficDetail + "\n");
tCost = tCost + trafficCost;
System.out.println("----------------------------------");
break;
case 'X':
case 'x':
quit = true;
System.out.print("***Menu entry has been terminated***" + "\n");
//System.out.printf("Total traffic cost is: %75s\n", "$" + tCost + "\n");
//System.out.printf("Total Damage cost is: %77s\n", "$" + dCost + "\n");
//System.out.printf(tDetail + "\n");
//System.out.print(dDetail + "\n");
break;
default:
System.out.print("Please enter either a valid menu selection (A, B, or X):" + "\n");
break;
}
}
double dhire = hire;
double charge = dhire*rate;
double adjustedCharge = charge + penalty;
//Print summary
System.out.println("---------------------------------------------"+
"------------------------------------------------------\n" +
"***CUSTOMER DETAILS:***\n");
System.out.printf("Name: %93s\n", name);
System.out.printf("Address: %90s\n", address);
System.out.printf("Phone Number: %85s\n", phone);
System.out.printf("Licence Number: %83s\n", licence);
System.out.printf("Credit Card Number: %79s\n", ccard);
System.out.printf("Credit Card Expiry: %79s\n", expiry);
System.out.println( "\n***CAR HIRE DETAILS:***\n");
System.out.printf("Make/Model: %87s\n", model);
System.out.printf("Registration Number: %78s\n", rego);
System.out.printf("Hire Length (days): %79s\n", model);
System.out.printf("Daily Hire Rate: %82s\n", rate);
System.out.printf("Basic Hire Charge: %80s\n\n", charge);
System.out.printf("Days hired: %87s\n", totalDays);
if (totalDays == hire){
System.out.printf("Late Return Surcharge: %76s\n", "$0.00");
}
if (totalDays > hire){
System.out.printf("Late Return Surcharge: %76s\n", + penalty);
}
System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");
System.out.print(dDetail + "\n");
System.out.printf("Total damage cost is: %78" + "s\n", "$" + dCost + "\n");
System.out.printf(tDetail + "\n");
System.out.printf("Total traffic fines incurred: %70s\n", "$" + tCost + "\n");
System.out.printf("Final hire charge: %79s\n", "$" + (adjustedCharge + dCost + tCost));
}
}
I just changed the way you printed out the adjustedCharge so you can still use printf
System.out.printf("Adjusted Hire Charge: %77s","$");
System.out.printf("%.1f",adjustedCharge);
System.out.printf("\n");
With printf you need to format the value you're printing and in this case, because you're trying to print a double, you'll need to format it with %f
I also noticed that my previous answer messed up the spacing you had for the rest of your output. So here's the fixed solution! Just change up the spacing to however you'd like
Or you can
use
Pass two arguments to printf
System.out.printf("Adjusted Hire Charge: %77s\n", "$" + adjustedCharge + "\n")
instead of
System.out.printf("Adjusted Hire Charge: %77s\n", "\n", "$" + adjustedCharge + "\n");
I am a beginner in Java so I wrote a program to understand the OOP concepts but it gives me an error when trying to use System.out.println().
Here's the code
public static void main(String[] args) {
sampleClass ADD = new sampleClass();
Scanner input = new Scanner(System.in);
int fNum;
int sNum;
System.out.println("Enter the first number to Add");
fNum = input.nextInt();
System.out.println("Now enter the second number");
sNum = input.nextInt();
int sum;
sum = ADD.add(fNum, sNum);
System.out.println("The sum of " fNum " and " sNum " is " sum);
}
Replace this line System.out.println("The sum of " fNum " and " sNum " is " sum); with
System.out.println("The sum of " +fNum+ " and " +sNum+ " is " +sum);
Please Note: In Java, the operator "+" normally acts as an arithmetic operator unless one of its operands is a String. If necessary it converts the other operand to a String before joining the second operand to the end of the first operand.
Examples:
If one of the operands is not a String it will be converted:
int age = 12;
System.out.println("My age is " + age);
Sign "+" is used for concatenation and you missed it between your
String.
The correct format is:
System.out.println("The sum of " + fNum + " and " + sNum + " is " +
sum);
You're missing some +'s between things like "The sum of " and fNum, e.g. "The sum of " + fNum + ....