Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: User inputs string to get calculated output

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking 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.");


    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);

    else if(input.equals("person")){
    System.out.printf("Your total per person is: $%3.2f\n", person);



    System.out.println("End of program");
    }
    }
    }


  2. #2
    Junior Member
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking 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.");


    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);

    else if(input.equals("person")){
    System.out.printf("Your total per person is: $%3.2f\n", person);



    System.out.println("End of program");
    }
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: User inputs string to get calculated output

    Threads merged. One is enough.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User inputs string to get calculated output

    <
    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.");
     
     
    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);
     
    else if(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

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User inputs string to get calculated output

    <
    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.");
     
     
          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);
     
           else if(input.equals("person")){
     
                  System.out.printf("Your total per person is: $%3.2f\n", person);
     
     
     
                 System.out.println("End of program");
    }
    }
    >
    [/QUOTE]

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: User inputs string to get calculated output

    Please copy and paste here the full text of the error messages.

    The code looks like it is missing some }s at the end.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: User inputs string to get calculated output

    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:57: error: '(' expected
    if else (input.equals("person")){
    ^
    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:57: error: illegal start of expression
    if else (input.equals("person")){
    ^
    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:57: error: ')' expected
    if else (input.equals("person")){
    ^
    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:57: error: ';' expected
    if else (input.equals("person")){
    ^
    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:66: error: reached end of file while parsing
    }
    ^
    5 errors

    Tool completed with exit code 1

    --- Update ---

    <
    String.total = "total";
    String.person = "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);
     
    }  if else (input.equals("person"))  {
     
    	System.out.printf("Your total per person is: $%3.2f\n", person);
     
    }
    >

    after I closed some of the loops with the french bracket these are the errors:

    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:59: error: '(' expected
    } if else (input.equals("person")) {
    ^
    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:59: error: illegal start of expression
    } if else (input.equals("person")) {
    ^
    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:59: error: ')' expected
    } if else (input.equals("person")) {
    ^
    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:59: error: ';' expected
    } if else (input.equals("person")) {
    ^
    4 errors

    Tool completed with exit code 1

    --- Update ---

    Okay, now everything runs up to the while statement:

    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:56: error: cannot find symbol
    }while(!String.total("total")&& !String.person("person"));
    ^
    symbol: method total(String)
    location: class String
    C:\Users\Charley\Documents\CSC 110\Programs\Un.java:56: error: cannot find symbol
    }while(!String.total("total")&& !String.person("person"));
    ^
    symbol: method person(String)
    location: class String
    2 errors

    Tool completed with exit code 1

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: User inputs string to get calculated output

    Please post the code that goes with the error messages.
    while(!String.total("total")&& !String.person("person"));
    symbol: method total(String)
    location: class String
    The String class does NOT have a method named: total or person. The compiler is objecting to the code trying to call those non-existent methods.
    What is that line of code supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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'.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: User inputs string to get calculated output

    You must use the correct syntax when writing code. The compiler says that there are problems with the syntax in the posted code.

    Look at the API doc for the String class to see what its methods are. You must use what is defined for the class. You can NOT add new methods to the String class.


    What expression will compare the String that the user entered to the String: "total"?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  2. Printing arrays from user inputs (2 dimensional)
    By kevinsauerzxc in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 13th, 2013, 07:28 AM
  3. Replies: 22
    Last Post: February 2nd, 2013, 09:16 PM
  4. Have user enter in 2 inputs instead of using -1 to end. Help appreciated!
    By neontiger in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2012, 04:26 AM
  5. "Separate Numbers"-Problem:Assigning Variables to User Inputs.
    By SaltSlasher in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 10th, 2012, 07:08 PM