Results 1 to 3 of 3
  1. #1
    popeyito18's Avatar
    popeyito18 is offline Member
    Join Date
    Jun 2011
    Location
    USA
    Posts
    17
    Rep Power
    0

    Default passing arguments to other methods

    I'm supposed to call several methods from my main method.
    I am not sure what should be my argument and what should go inside the parenthesis.

    This is the code:
    The main method is a loop that will stop only if users chooses option 4
    Java Code:
     import java.util.Scanner;
     import java.io.*;
     import javax.swing.JOptionPane; //Needed for JOptionPane 
     
    
     public class project2
     {
     public static void main(String[] args) throws IOException
        {
       int option1;
       String CUSTOMER;
       
          Scanner keyboard = new Scanner(System.in);
          System.out.println("Welcome to THE STORE \n"); //greeting the user
          System.out.println("\n Please Select One of Following");  //Main menu, to be used throughout the program
          System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit"); //displaying the 4 different options to user
          option1 = keyboard.nextInt();
           keyboard.nextLine();
          {
            //if customers picks option one
            while (true) //The variable "option1" will be passed as an argument to the other methods
            { while(option1 ==1)
              { customerinformation(option1); } //method to get customer information
            while (option1 ==2)
              { pricelookup(option1); } //method to check the price of the products
            while (option1 ==3)
            { totalbill(option1); } //method to print tpo screen the purchase total
            while (option1 ==4) //if user selects option 4 he/she will be brought here
               { System.exit(0); }
            }
          }
        } /********************************************************************************************************/
           //First method: Customer Information
           public static void customerinformation(int option1)
          {
            String cname, caddress, cemail;
             while (option1 ==1) //using the if statement to pick one of the choices. In this case, option 1
             {  // getting all the customer information
        cname = JOptionPane.showInputDialog(null, "What is the customer's name?"); //asking for customer's name
        PrintWriter outputFile = new PrintWriter("cInfo.txt"); //creating new text file
        outputFile.println(cname); //writing name to the files
        caddress = JOptionPane.showInputDialog(null, "What is the custormer's address?"); //asking for custormer's address
        outputFile.println(caddress); //exporting address to the text file 
        cemail = JOptionPane.showInputDialog(null, "What is the customer's e-mail address?");
        outputFile.println(cemail); //exporting email to file
        outputFile.close(); //closing the text file 
            
             }
           }  
           /********************************************************************************************************/
           //Second Method: PriceLookUp
           
             public static void pricelookup(int option1);
               String menu2;
               
             { while (option1 == 2) //if users picks option 2 the following series of codes will be executed
              {
              Scanner keyboard2 = new Scanner(System.in); //user input
              System.out.println("\n Choose one of the following products");
              System.out.println(" 1. Shoes \n 2. T-shirts \n 3. Shorts \n 4. Caps \n 5. Jackets"); //offering customer the available options
              menu2 = keyboard.nextInt(); //obtaining customer's option
             
              if (menu2 == 1)
              {System.out.println("The price for Shoes is $50.00 \n"); //displaying results for option 1
              }if (menu2 == 2)
              {System.out.println("The price for T-Shirts is $30.00 \n"); //displaying results for option 2
              }if (menu2 == 3)
              {System.out.println("The price for Shorts is $75.00 \n"); //displaying results for option 3
              }if (menu2 == 4)
              {System.out.println("The price for Caps is $15.00 \n"); //displaying results for option 4
              }if (menu2 == 5)
              }
             }
             /********************************************************************************************************/
             //Third Method: TotalBill
             //method for option 3  = Total Bill
    
                   public static void getTotal(int option1);
                   {while (option ==3)
                     {String empty; System.out.print(" "); //Program did not run so I created an empty variable
                     empty = keyboard.nextLine(); // for reasons I don't know it worked this way
                     String name2;
                     //creating scanner for input
                     System.out.println("\n Enter customer's name ");
                     name2 = keyboard.nextLine(); //name2 contains the name you want to match with the
                                             // one on the text file
                     //obtaining the desired product drom customer   
                     System.out.println("\n what product are you buying? \n1.Shoes \n2.T-Shirts \n3.Shorts \n4.Caps \n5.Jackets");
                     product = keyboard.nextInt();  //obtaining the customer's selection
                     keyboard.nextLine(); //to swallow the end of line token  
                     String producto = "not a product"; //holds the product name
                     int price = 1;
                     switch (product) {
                       case 1:
                         price = 50; 
                         producto = "Shoes"; break; //options depending of the customer's selection
                       case 2:
                         price = 30; producto = "T-Shirts"; break;
                       case 3:
                         price = 75; producto = "Shorts"; break;
                       case 4:
                         price = 15; producto = "Caps"; break;
                       case 5:
                         price = 100; producto = "Jacket"; break;
                       default:
                         System.out.println("Wrong choice");
                
                
                     System.out.println("How many do you want?");
                     quantity = keyboard.nextInt(); //quantity purchased
                     keyboard.nextLine(); //to swallow the end of line token
                
                     double tax, total, TOTAL; //defining variables
                
                     tax = .0825; //8% of tax
                     total = (price*quantity*tax);
                     TOTAL = total + price;                      
               
                
                     String cinfo = ("cInfo.txt"); //assigning the name of the file to the variable cinfo
               
                     File file = new File(cinfo); //file is ready to display
                     Scanner inputFile = new Scanner(file);
                     String name3 = inputFile.nextLine(); // name3 contains the name in the fil
                
                     //compare name2 & name3
                     //if names are the same display the content of the file, otherwise error message
                     //use if-statements for this step
            
                     System.out.println("\n Total Bill: ");
            
                     if (name2.equals(name3))
                     {
                       System.out.println(name3);
                       File file2 = new File(cinfo); //opening file
                       Scanner inputFile2 = new Scanner(file2);
                       while (inputFile.hasNext()) //loop to display all the lines in the text file
                       {
                         String alltext = inputFile.nextLine();
                         // displaying the content of the file
                         System.out.println(alltext);
                       }
                     }
                     else 
                     { System.out.println("Could not find name in the system \n ");
                       
                     } //now to set the results to only 2 decimal places
                
                
                     DecimalFormat formatter = new DecimalFormat("#0.00");
                
                     System.out.println("\nProduct: \n\n" + producto + "\t" + quantity + "\t $" + price*quantity);
                     System.out.println("\nTax: (@8%)\t\t" + "$" + (formatter.format(tax*price*quantity)));
                     System.out.println("\nTotal:\t\t $" + (formatter.format(TOTAL)) + "\n");
                      }
        }
              }
    When I try to compile I get several errors and I have hours trying to figure it out but I don't have enough Java knowledge to do it.

    this is the log:




    I doubled checked the braces and couldn't find the error (if that's the cause)

    p.s I have 3 weeks doing Java. It is tricky to self-teach with the pressure of timelines from school

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    • You're coding style is wrong as you're creating a big program all at once and then only compiling when done. Even the best Java programmer if coding this way, would end up with what you have: a mess of errors.
    • Instead when coding compile early and compile often, and don't add any new code until all compile errors have been corrected.
    • When you receive an illegal start of expression error, it's often best to look at the line of code above the error. In your first example you have an if statement with no code block after it, and that's obviously not what an if block should look like (check your text to see what it should look like).

    Best of luck.

  3. #3
    popeyito18's Avatar
    popeyito18 is offline Member
    Join Date
    Jun 2011
    Location
    USA
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Problem passing arrayed arguments to a method
    By vcl in forum New To Java
    Replies: 1
    Last Post: 06-27-2011, 07:04 PM
  2. Passing arguments to jar file with a pipe
    By clover in forum New To Java
    Replies: 7
    Last Post: 06-27-2011, 04:36 PM
  3. Help: Methods and Arguments
    By whateverme in forum New To Java
    Replies: 5
    Last Post: 12-12-2010, 12:14 AM
  4. Passing array arguments into main
    By JohnDas in forum New To Java
    Replies: 12
    Last Post: 11-10-2010, 03:00 PM
  5. passing arguments
    By mac in forum New To Java
    Replies: 3
    Last Post: 04-07-2010, 11:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •