I am getting the ')' expected error when I try to compile on one of my print f statements. The project is to write an application that will run on cash registers in a pizza restaurant. Here is the section of code. The problem line is the one inside the double asterisks. Any ideas?
System.out.println();
System.out.println (" Order Summary ");
System.out.println (" *************************");
System.out.println ();
if(NofTopp = 0){
System.out.printf( Size + ", " + crust + " Cheese Pizza : $%.2f", cost);
System.out.println();
}else if(NofTopp = 1){
System.out.printf( Size + ", " + crust + " Pizza with " + Top1 + ": $%.2f", cost);
System.out.println();
}else if(NofTopp = 2){
**System.out.printf (Size + ", " + crust + " Pizza with " + Top1 + " and " + Top2 ": $%.2f",
cost);**
System.out.println();
}else if(NofTopp = 3){
System.out.printf( Size + ", " + crust + " Pizza with " + Top1 + ", " + Top2 + " and " + Top3 +
": $%.2f", cost);
System.out.println();
}
System.out.printf ("Tax: $%.2f",SalesTax );
System.out.println();
System.out.printf ("Total: $%.2f",totalCost );
System.out.println();
I think you forgot a + before the ": $%.2f". Try:
System.out.printf (Size + ", " + crust + " Pizza with " + Top1 + " and " + Top2 + ": $%.2f", cost);
Related
i need your help!
So basically i made a Search Student Tab who read a .txt file and
output on textArea.
Image
But after search i take this:
Output result
But i want to take this:
as Output on TextArea
So here is my code:
StringBuilder rsl = new StringBuilder();
#FXML
public void PressedSearch(ActionEvent event) throws IOException { //Ektelei Search sto arxeio ton mathiton
if (searchStudent() == false) {
result.setStyle("-fx-font:13 sherif;-fx-text-fill: red");
result.setText("Failed to find the student");
} else {
result.setStyle("-fx-text-fill: black");
rsl.setLength(0);
}
}
public boolean searchStudent() throws IOException { //Sto search o admin mporei na dei perissotera pragmata
Path p = Paths.get("src", "inware", "users.txt");
Scanner in = null;
boolean i = false;
in = new Scanner(p);
while (in.hasNext()) {
String line = in.nextLine();
String[] fields = line.split("[,]");
if (line.contains(search_field.getText())) {
rsl.append(fields[4] + " " + fields[5]+ " " + fields[6] + " " + fields[7] + " " + fields[8]
+ " " + fields[9] + " " + fields[10]+ " " + fields[11]+ " " + fields[12]+ " " + fields[13]
+ " " + fields[14]+ " " + fields[15] + " " + fields[16] + " " + fields[17] + " " + fields[18]
+ " " + fields[19] + " " + fields[20] + " " + fields[21]);
rsl.append("\n"); //Gia na kanei print polla atoma
i = true;
}
}
result.setText(rsl.toString());
in.close();
return i;
Try to use String.format(String format,Object... args).
Instead of using:
rsl.append(fields[4] + " " + fields[5]+ " " + fields[6] + " " + fields[7] + " " + fields[8]
+ " " + fields[9] + " " + fields[10]+ " " + fields[11]+ " " + fields[12]+ " " + fields[13]
+ " " + fields[14]+ " " + fields[15] + " " + fields[16] + " " + fields[17] + " " + fields[18]
+ " " + fields[19] + " " + fields[20] + " " + fields[21]);
rsl.append("\n"); //Gia na kanei print polla atoma
You can use:
String format = "%10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s\n";
rsl.append(String.format(format,fields[4],fields[5],fields[6],fields[7],fields[8],fields[9],fields[10],fields[11],fields[12],fields[13],fields[14],fields[15],fields[16],fields[17],fields[18],fields[19],fields[20],fields[21]));
In format String, the number between '%' and 's'( Width field) represents the minimum number of characters to output. You can change it according to your needs.
My number one problem here is in the receipt page. When I order from the menu, it prints in the receipt but when I order the same/another order in the same menu(i.e I order first food in Chicken page menu then my second order is another or same food but in the same Chicken page menu), the last order will be print on the receipt page. What code I will use to print multiple orders even if I order in the same menu. Here's my code in my receipt page:
System.out.println("\n\t\t\t BT-RET FOOD CORPORATION"
+ "\n\t\t\t Ayala Blvd Ermita Manila"
+ "\n\t\t\tVAT Reg TIN: 000-000-000-001\n"
+ " \t\t\t" + dateFormat.format(cal.getTime())
+ "\n+--------------------------------------------------------------+"
+ "\n| Cashier: " + name1 + " |"
+ "\n| Official Receipt #: |"
+ "\n+--------------------------------------------------------------+");
if(counttc!=0){
do{
System.out.println("\t" + chickn + "\t\t\t" + chickprice);}
while(counttc==count);}
if(counttbu!=0){
do{
System.out.println("\t" + burgn + "\t\t\t" + burgprice);}
while(counttbu==count);}
if(counttr!=0){
do{
System.out.println("\t" + rmn + "\t\t\t" + rmprice);}
while(counttr==count);}
if(counttn!=0){
do{
System.out.println("\t" + noodn + "\t\t\t" + noodprice);}
while(counttn==count);}
if(countts!=0){
do{
System.out.println("\t" + sann + "\t\t\t" + sanprice);}
while(countts==count);}
if(counttbr!=0){
do{
System.out.println("\t" + brean + "\t\t\t" + breaprice);}
while(counttbr==count);}
if(counttd!=0){
do{
System.out.println("\t" + ddn + "\t\t\t" + ddprice);}
while(counttd==count);}
if(countta!=0){
do{
System.out.println("\t" + addn + "\t\t\t" + addprice);}
while(countta==count);}
if(counttk!=0){
do{
System.out.println("\t" + kidn + " " + kidprice);}
while(counttk==count);}
System.out.print("\n\tDelivery " + dec1.format(deli)
+"\n\t12% VAT " + dec1.format(vat)
+"\n\tAmount Due " + dec1.format(runningTotal)
+"\n\tCash " + dec1.format(cash)
+"\n\tChange " + dec1.format(change));
System.out.println("\n\n\t\t\tThis serves as your Official Receipt."
+"\n\t\t\t\t 12% VAT Included"
+"\n\t\t\tPrices inclusive of 10% delivery charge"
+"\n\t\t\t Thank you, and please come again.");
}
}
The purpose of this program is to build a 10x10 array grid made out of periods (.), allow the user to designate a starting point, and then the program will randomly choose numbers which are assigned to directions for the 'walker' to go. Each time it moves it marks its spot with the next letter in the alphabet (the starting point is marked by A). I haven't done this part yet but I know how I will; if the walker crosses out of the bounds of the array (AKA > 10 or < 0) it will say "you were arrested" and if the variable alpha == 'Z' it way say "You made it home".
The main problem as far as I can tell is with the method processing(), where I draw numbers from getRand (which I have confirmed is working) and then assign them directions which are given to the array. But for some reason when it prints the grid it only shows the starting point, and not the movement of the walker.
Another question I have of lesser importance is if there is a more brief way to display my grid than how I have been (A massive brick of text in a System.out.print)
package walktester;
import java.lang.Math;
import java.util.Random;
import java.util.Scanner;
class DrunkWalker {
private char[][] walkgrid = new char[10][10];
private static int randNSEW;
private int randomnum;
private int startrow;
private int startcol;
private char alpha = 'A';
private int nextrow;
private int nextcol;
public DrunkWalker(int r, int c) {
startrow = r;
startcol = c;
nextrow = startrow;
nextcol = startcol;
for (int i = 0; i < 10; i ++) {
for (int j = 0; j < 10; j++)
walkgrid[i][j] = '.';
}
walkgrid[r][c] = alpha;
}
public static void getRand(){
int x100 = 0;
double randomNum = 0.0;
randomNum = Math.random();
x100 = (int) (randomNum * 100);
randNSEW = x100 % 4;
}
public int getNextRow(){
return nextrow;
}
public int getNextCol(){
return nextcol;
}
public void processing(){
for(int i = 0; i < 26; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 1){
nextrow++;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 2){
nextcol++;
walkgrid[nextrow][nextcol] = alpha++;
}
if(randNSEW == 3){
nextrow--;
walkgrid[nextrow][nextcol] = alpha++;
}
}
}
public char[][] DisplayGrid() {
System.out.print(
walkgrid[0][0] + " " + walkgrid[0][1] + " " + walkgrid[0][2] + " " + walkgrid[0][3] + " " + walkgrid[0][4] + " " + walkgrid[0][5] + " " + walkgrid[0][6] + " " + walkgrid[0][7] + " " + walkgrid[0][8] + " " + walkgrid[0][9] + "\n" +
walkgrid[1][0] + " " + walkgrid[1][1] + " " + walkgrid[1][2] + " " + walkgrid[1][3] + " " + walkgrid[1][4] + " " + walkgrid[1][5] + " " + walkgrid[1][6] + " " + walkgrid[1][7] + " " + walkgrid[1][8] + " " + walkgrid[1][9] + "\n" +
walkgrid[2][0] + " " + walkgrid[2][1] + " " + walkgrid[2][2] + " " + walkgrid[2][3] + " " + walkgrid[2][4] + " " + walkgrid[2][5] + " " + walkgrid[2][6] + " " + walkgrid[2][7] + " " + walkgrid[2][8] + " " + walkgrid[2][9] + "\n" +
walkgrid[3][0] + " " + walkgrid[3][1] + " " + walkgrid[3][2] + " " + walkgrid[3][3] + " " + walkgrid[3][4] + " " + walkgrid[3][5] + " " + walkgrid[3][6] + " " + walkgrid[3][7] + " " + walkgrid[3][8] + " " + walkgrid[3][9] + "\n" +
walkgrid[4][0] + " " + walkgrid[4][1] + " " + walkgrid[4][2] + " " + walkgrid[4][3] + " " + walkgrid[4][4] + " " + walkgrid[4][5] + " " + walkgrid[4][6] + " " + walkgrid[4][7] + " " + walkgrid[4][8] + " " + walkgrid[4][9] + "\n" +
walkgrid[5][0] + " " + walkgrid[5][1] + " " + walkgrid[5][2] + " " + walkgrid[5][3] + " " + walkgrid[5][4] + " " + walkgrid[5][5] + " " + walkgrid[5][6] + " " + walkgrid[5][7] + " " + walkgrid[5][8] + " " + walkgrid[5][9] + "\n" +
walkgrid[6][0] + " " + walkgrid[6][1] + " " + walkgrid[6][2] + " " + walkgrid[6][3] + " " + walkgrid[6][4] + " " + walkgrid[6][5] + " " + walkgrid[6][6] + " " + walkgrid[6][7] + " " + walkgrid[6][8] + " " + walkgrid[6][9] + "\n" +
walkgrid[7][0] + " " + walkgrid[7][1] + " " + walkgrid[7][2] + " " + walkgrid[7][3] + " " + walkgrid[7][4] + " " + walkgrid[7][5] + " " + walkgrid[7][6] + " " + walkgrid[7][7] + " " + walkgrid[7][8] + " " + walkgrid[7][9] + "\n" +
walkgrid[8][0] + " " + walkgrid[8][1] + " " + walkgrid[8][2] + " " + walkgrid[8][3] + " " + walkgrid[8][4] + " " + walkgrid[8][5] + " " + walkgrid[8][6] + " " + walkgrid[8][7] + " " + walkgrid[8][8] + " " + walkgrid[8][9] + "\n" +
walkgrid[9][0] + " " + walkgrid[9][1] + " " + walkgrid[9][2] + " " + walkgrid[9][3] + " " + walkgrid[9][4] + " " + walkgrid[9][5] + " " + walkgrid[9][6] + " " + walkgrid[9][7] + " " + walkgrid[9][8] + " " + walkgrid[9][9] + "\n"
);
return walkgrid;
}
}
public class WalkTester {
public static void main(String[] args) {
Scanner inpr = new Scanner(System.in);
Scanner inpc = new Scanner(System.in);
Scanner inpchoice = new Scanner(System.in);
int r = 0;
int c = 0;
char choice = 'y';
while(choice == 'y' || choice == 'Y') {
System.out.println("Please enter x coordinate between 1 and 10.");
r = inpr.nextInt();
r = r - 1;
System.out.println("Please enter y coordinate between 1 and 10");
c = inpr.nextInt();
c = c - 1;
if(r < 0 || r > 9 || c < 0 || c > 9){
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
DrunkWalker drunkwalker = new DrunkWalker(r, c);
drunkwalker.DisplayGrid();
System.out.println("Restart? y/n");
choice = inpchoice.next().charAt(0);
if(choice == 'y' || choice == 'Y'){
continue;
}
else if(choice == 'n' || choice == 'N'){
return;
}
else{
System.out.println("Invalid Entry. Restart? y/n");
choice = inpchoice.next().charAt(0);
}
}
}
}
The code that moves the walker is in a method called processing(), but you never call it.
You can make your DisplayGrid() method simpler (and it will still print the same stuff):
public char[][] DisplayGrid() {
for(int y = 0; y < 10; y++) {
for(int x = 0; x < 10; x++) {
System.out.print(walkgrid[x][y] + " ");
}
System.out.println();
}
return walkgrid;
}
Your processing() method needs to check if the walker leaves the bounds of the area. If you don't, you'll get an ArrayIndexOutOfBoundsException.
public boolean processing(){
for(int i = 0; i < 26; i ++){
getRand();
if(randNSEW == 0){
nextcol--;
}
if(randNSEW == 1){
nextrow++;
}
if(randNSEW == 2){
nextcol++;
}
if(randNSEW == 3){
nextrow--;
}
if(nextrow < 0 || nextrow >= 10 || nextcol < 0 || nextcol >= 10) {
return false;
}
walkgrid[nextrow][nextcol] = alpha++;
}
return true;
}
Now you need to call processing() and check the return value to see if the walker succeeded:
DrunkWalker drunkwalker = new DrunkWalker(r, c);
boolean walkerSucceeded = drunkwalker.processing();
drunkwalker.DisplayGrid();
if(walkerSucceeded) {
System.out.println("You made it home");
} else {
System.out.println("You were arrested");
}
And keep in mind (as you will see when you test this), that the walker can cross their own tracks (so you might see some letters missing).
I'm trying to create another array but I want it to be the array double gainT[], which is the temperature in Fahrenheit, converted to Celcius to make another array called double gainTC[]. I am also trying to do the same thing but with the precipitation. I just can't seem to find a way to do it though.
import java.io.IOException;
import java.util.Scanner;
public class AnnualClimate1 {
public static void main(String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.print("Choose the temperature Scale (F = Fahrenheit, C = Celsius): ");
String tFC = in.nextLine();
System.out.print("Choose the precipitation Scale (I = Inches, C = Centimeters): ");
String pIC = in.nextLine();
System.out.println("");
System.out.println("");
System.out.println(" Climate Data");
System.out.println(" Location: Gainesville, Florida");
System.out.println(" Temperature " + tFC + " Precipitation " + pIC);
System.out.println("=================================================");
double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double gainTC[] = {(gainT[] - 32) / 1.8};
double gainP[]={3.5, 3.4, 4.3, 2.9, 3.2, 6.8, 6.1, 6.6, 4.4, 2.5, 2.2, 2.6};
double gainPC[] = {gainP[] / .3937};
if(tFC.equalsIgnoreCase("F") && pIC.equalsIgnoreCase("I")){
System.out.println("Jan. " + gainT[1] + " " + gainP[1]);
System.out.println("Feb. " + gainT[2] + " " + gainP[2]);
System.out.println("Mar. " + gainT[3] + " " + gainP[3]);
System.out.println("Apr. " + gainT[4] + " " + gainP[4]);
System.out.println("May " + gainT[5] + " " + gainP[5]);
System.out.println("Jun. " + gainT[6] + " " + gainP[6]);
System.out.println("Jul. " + gainT[7] + " " + gainP[7]);
System.out.println("Aug. " + gainT[8] + " " + gainP[8]);
System.out.println("Sep. " + gainT[9] + " " + gainP[9]);
System.out.println("Oct. " + gainT[10] + " " + gainP[10]);
System.out.println("Nov. " + gainT[11] + " " + gainP[11]);
System.out.println("Dec. " + gainT[12] + " " + gainP[12]);
}
else if(tFC.equalsIgnoreCase("C") && pIC.equalsIgnoreCase("C")){
System.out.println("Jan. " + gainTC[1] + " " + gainPC[1]);
System.out.println("Feb. " + gainTC[2] + " " + gainPC[2]);
System.out.println("Mar. " + gainTC[3] + " " + gainPC[3]);
System.out.println("Apr. " + gainTC[4] + " " + gainPC[4]);
System.out.println("May " + gainTC[5] + " " + gainPC[5]);
System.out.println("Jun. " + gainTC[6] + " " + gainPC[6]);
System.out.println("Jul. " + gainTC[7] + " " + gainPC[7]);
System.out.println("Aug. " + gainTC[8] + " " + gainPC[8]);
System.out.println("Sep. " + gainTC[9] + " " + gainPC[9]);
System.out.println("Oct. " + gainTC[10] + " " + gainPC[10]);
System.out.println("Nov. " + gainTC[11] + " " + gainPC[11]);
System.out.println("Dec. " + gainTC[12] + " " + gainPC[12]);
}
}
}
Here is an incomplete example so you can fill in the blanks yourself:
double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double gainTC[] = new double[gainT.length]; //create array which matches the size of gainT
//array.length is a property value returning the 'size' or length or the array
//Now just iterate through all the gainT[] values you populated above and read each one
for(int i = 0; i < gainT.length; i++){
double math = //use your conversion math here, and store the value
gainTC[i] = math; //assign the results of your math to the same spot in the new array
}
If you'd like more information on Loops and Arrays, check here:
For Loops: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Arrays: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Hi this is some code I've constructed for a project which calls several other methods to print a statement about the person. My problem is its printing in double and appears to be running through both if/else statements.
How can I prevent this? if needed I can add the text from my terminal. Code below;
void printPersonDescription(String name, boolean sex, int age) {
if(sex == true && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " years left until she is " +
nextMaturityLevel(age) + ".");
}
else if(sex == false && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " years left until he is " +
nextMaturityLevel(age) + ".");
}
if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " year left until she is " +
nextMaturityLevel(age) + ".");
}
else {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " year left until he is " +
nextMaturityLevel(age) + ".");
}
}
Note how similar your four cases are. You are just changing some words in the line you print. Those four if/else blocks can be simplified to this:
void printPersonDescription(String name, boolean sex, int age) {
int years = yearsUntilNextMaturityLevel(age);
String gender = sex ? "she" : "he";
String plural = years > 1 ? "s" : "";
println(name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" " + gender + " has " + years + " year" + plural + " left until " + gender + " is " +
nextMaturityLevel(age) + ".");
}
Also, you can make the println a bit more readable by using String.format (assuming that the maturity levels are strings)
String.format("%s is %s. At %d %s has %d year%s left until %s is %s.",
name, getMaturityLevel(age), age, gender, years, plural, gender,
nextMaturityLevel(age))
Third if condition should be else if in your code to avoid double printing.
Instead of:
if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {
Use:
else if (sex && yearsUntilNextMaturityLevel(age) <= 1) {
btw you don't need to use if (boolVar == true) you can just use if (boolVar)
void printPersonDescription(String name, boolean sex, int age) {
if(sex == true && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " years left until she is " +
nextMaturityLevel(age) + ".");
}
else if(sex == false && yearsUntilNextMaturityLevel(age) > 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " years left until he is " +
nextMaturityLevel(age) + ".");
}
else if (sex == true && yearsUntilNextMaturityLevel(age) <= 1) {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" she has " + yearsUntilNextMaturityLevel(age) + " year left until she is " +
nextMaturityLevel(age) + ".");
}
else {
println (name + " is " + getMaturityLevel(age) + ". " + "At " + age +
" he has " + yearsUntilNextMaturityLevel(age) + " year left until he is " +
nextMaturityLevel(age) + ".");
}
}
You need add else before the third if. Otherwise, the final else statement will match the third if, thus, the else statement means
if(sex != true || yearsUntilNextMaturityLevel(age) > 1)