The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

User inputs string to get calculated output

Java- two strings that equal separate math functions output when user enters either word?
Up to the strings, everything is fine. I am VERY new to java. I'm looking to see if I'm heading in the right direction. I would like the user to either enter 'total' or 'person' to get the grand total or total per person. If the enter neither, I want it to say 'Please enter either one'. I want that to loop until they enter the right word. When they do, I want it to output the answer. I have the formulas below but I'm not sure if the formatting is correct. I have to use two loops. I'm preferring a while and if else loop. Thank you!!!!!

import java.util.Scanner;
import java.util.Properties;

public class Un {

public static void main ( String[] args ) {

System.out.println("This program calculates the cost of a class reunion event.");
System.out.println("It gives back the total cost and the cost per person.");

User inputs string to get calculated output

Up to the strings, everything is fine. I am VERY new to java. I'm looking to see if I'm heading in the right direction. I would like the user to either enter 'total' or 'person' to get the grand total or total per person. If the enter neither, I want it to say 'Please enter either one'. I want that to loop until they enter the right word. When they do, I want it to output the answer. I have the formulas below but I'm not sure if the formatting is correct. I have to use two loops. I'm preferring a while and if else loop. Thank you!!!!!

import java.util.Scanner;

public class Reunion {

public static void main ( String[] args ) {

System.out.println("This program calculates the cost of a class reunion event.");
System.out.println("It gives back the total cost and the cost per person.");

Re: User inputs string to get calculated output

<importjava.util.Scanner;publicclass Reunion {publicstaticvoid main (String[] args ){System.out.println("This program calculates the cost of a class reunion event.");System.out.println("It gives back the total cost and the cost per person.");
Scanner in =new Scanner(System.in);int rent =200;double hour;double totalrent;System.out.println("Please enter the number of hours you would like to rent a room: ");
hour= in.nextDouble();
totalrent= rent * hour;System.out.printf("Your total cost for renting a room is: $%3.0f\n", totalrent);System.out.println("Enter number of guests for cost of dinner plates: ");int plate =40;int guest;int totalplate;
guest= in.nextInt();
totalplate= plate * guest;System.out.printf("Your total cost for dinner plates is: $%d%n", totalplate);System.out.println("Enter 'total' for grand total, which includes hourly rent, dinner, and band.");System.out.println("Enter 'person' to get total for each person; dividing the grand total evenly.");String.total=int total;String.person=int person;int band =350;int total;
total = band + totalplate + totalrent;
person = total/guest;while(!String.total.equals("total")&&!String.person.equals("person")){System.out.println("Please only enter 'total' or 'person'.");if(input.equals("total")){System.out.printf("Your grand total is: $%3.0f\n", total);elseif(input.equals("person")){System.out.printf("Your total per person is: $%3.2f\n", person);System.out.println("End of program");}}>

Last edited by Norm; May 1st, 2013 at 12:41 PM.
Reason: \ changed to / in code tag

Re: User inputs string to get calculated output

Now the code needs to be properly formatted. Statements should be indented 3-4 spaces to show nesting levels within {}s
All statements should NOT start in the first column.
The }s should NOT be in the same column.

Re: User inputs string to get calculated output

<importjava.util.Scanner;publicclass Reunion {publicstaticvoid main (String[] args ){System.out.println("This program calculates the cost of a class reunion event.");System.out.println("It gives back the total cost and the cost per person.");
Scanner in =new Scanner(System.in);int rent =200;double hour;double totalrent;System.out.println("Please enter the number of hours you would like to rent a room: ");
hour= in.nextDouble();
totalrent= rent * hour;System.out.printf("Your total cost for renting a room is: $%3.0f\n", totalrent);System.out.println("Enter number of guests for cost of dinner plates: ");int plate =40;int guest;int totalplate;
guest= in.nextInt();
totalplate= plate * guest;System.out.printf("Your total cost for dinner plates is: $%d%n", totalplate);System.out.println("Enter 'total' for grand total, which includes hourly rent, dinner, and band.");System.out.println("Enter 'person' to get total for each person; dividing the grand total evenly.");String.total=int total;String.person=int person;int band =350;int total;
total = band + totalplate + totalrent;
person = total/guest;while(!String.total.equals("total")&&!String.person.equals("person")){System.out.println("Please only enter 'total' or 'person'.");if(input.equals("total")){System.out.printf("Your grand total is: $%3.0f\n", total);elseif(input.equals("person")){System.out.printf("Your total per person is: $%3.2f\n", person);System.out.println("End of program");}}>

Re: User inputs string to get calculated output

I'm trying to say if the user does not type in neither total or person, there will be an error and a message will output "please enter 'total' or 'person'. I'm trying to say what they enter has to equal either 'total' or 'person'.