Results 1 to 15 of 15
  1. #1
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Need Help organizing the code!!

    Hello All, I need some help with the organizing the code. I have some questions in comments. Please help. Thank you.

    Java Code:
     import java.io.*;
    -
    
     public class BarkingLotReportJOLENE //Begin BarkingLot class
     {
     public static void main(String args[]) throws Exception //Begin main method
     {
    
     /**Variable Declarations ********************/
     String dogName;
    
    
    
    
     // variables to Read in Input Strings
     String name, weightString, weeksString;
    
     // variables to convert String input to ints so we can use in calculations
     int weeks, weight;
     weight = Integer.parseInt(weightString); // you cant convert weightString before it is initialized. What does this mean?
     weeks = Integer.parseInt(weeksString); // you cant convert weeksString before it is initialized. What does this mean?
    
    
     // results of calculations
     int bill, custTotal = 0, yearEndTotal = 0, numCustomers = 1;
    
     // declare control break variable
     String oldname = "";
    
     //File I/O open and I/O buffers (refer to Fig. 7-1 Java)
     FileReader fr = new FileReader("BarkingLotInput.txt");
     BufferedReader br = new BufferedReader(fr);
     // Need your Output file and buffer delcared too
    
     /****** end of variable Declarations *******/
    
     // **** start our logic ***********//
    
     // Print out Report Header to File (refer Fig. 7-5 Java)
     final String TITLE = "\n\nBarking Lot 2015 Year-End Report by Doggie Customer\n\n"; // need to output too
    
    
     //While not EOF 
     while ((name = br.readLine())!= null )
     {
     // initialize our control-break variable first time in loop
     if (oldname.equals(""))
     oldname = name;
    
     //Read in data from Input File: weight and stay in weeks
     name = br.readLine(); // already read in the name above in the while loop, so this is reading it in AGAIN in will
     // make your line reads "off-line" or not correct
     weightString = br.readLine();
     weeksString = br.readLine();
    
     // convert weightString and weeksString to int
     weight = Integer.parseInt(weightString);
     weeks = Integer.parseInt(weeksString);
    
     // if same Dog
     if (name.equals(oldname))
     {
     //CALL calculateStay
    
     //Set dogs running total
    
     }
    
     else // we are on a new dog customer, so output previous customers totals
     {
     // output dog total summary for year (refer to Fig. 7-1 Java)
    
     // reset variarbles and control-break variable 'oldname'
     oldname = name;
     custTotal = 0;
     numCustomers= numCustomers + 1;
    
     //CALL calculateStay for new dog
     calculateStay(weight, weeks);
    
     //Start summing new dogs running total
    
     }
    
     // Add to total sales running total outside of our if-else structure
     // another way of writing "yearEndTotal = yearEndTotal + bill;"
     yearEndTotal += bill; 
    
    
    
     } //End of Loop
    
     //Output last dog total summary for year
    
    
    
     //Output Year End totals
    
    
     //close input file and close output file
    
    
     System.exit(0); //Ending program
    
    
     } //End main method
    
     //Starting calculateStay method 
     public static int calculateStay(int w, int s)
     {
     int rate;
     // DO NOT need, we pass this information in as a method parameter w
     // DO NOT need, we pass this information in as a method aprameter s
     if( w < 15 ) // should be ( w < 15 )
     {
     rate = 55;
     return rate * s;
     }
    
     else if( w <= 30 ) // should be ( w <= 30 )
     {
     rate = 75;
     return rate * s;
    
     }
    
     else if( w <= 80 )// Should be (w <=80 )
     {
     rate = 105;
    
     }
    
     else 
     {
     rate = 125;
    
     }
    
     // need to return rate * w
     } 
    
    -
    }//End BarkingLot class
    Last edited by DarrylBurke; 07-01-2015 at 01:37 PM. Reason: Restored original content

  2. #2
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    Well, i did figure some stuff out. Here's my re-evaluated code.

    Java Code:
    -
    import java.io.*;
    
     public class BarkingLotReportASHAN //Begin BarkingLot class
     {
     public static void main(String args[]) throws Exception //Begin main method
     {
    
     /**Variable Declarations ********************/
     // variables to Read in Input Strings
     String name, weightString, weeksString;
    
     // variables to convert String input to ints so we can use in calculations
     int weeks, weight;
    
     // results of calculations
     int bill, custTotal = 0, yearEndTotal = 0, numCustomers = 1;
    
     // declare control break variable
     String oldname = "";
    
     //File I/O open and I/O buffers (refer to Fig. 7-1 Java)
     FileReader fr = new FileReader("BarkingLotInput.txt");
     BufferedReader br = new BufferedReader(fr);
     FileWriter fw = new FileWriter("BarkingLotYearEndrpt.txt");
     PrintWriter pw = new PrintWriter(fw);
    
     weightString = br.readLine();
     weight = Integer.parseInt(weightString);
     weeksString = br.readLine();
     weeks = Integer.parseInt(weeksString);
    
     /****** end of variable Declarations *******/
    
     // **** start our logic ***********//
    
     // Print out Report Header to File (refer Fig. 7-5 Java)
     final String TITLE = "\n\nBarking Lot 2015 Year-End Report by Doggie Customer\n\n";
    
     //While not EOF 
     while ((name = br.readLine())!= null )
     {
     // initialize our control-break variable first time in loop
     if (oldname.equals(""))
     oldname = name;
    
     //Read in data from Input File: weight and stay in weeks
    
    
    
     // convert weightString and weeksString to int
     weight = Integer.parseInt(weightString);
     weeks = Integer.parseInt(weeksString);
    
     // if same Dog
     if (name.equals(oldname))
     {
     //CALL calculateStay
     calculateStay();
    
     //Set dogs running total
    
     }
    
     else // we are on a new dog customer, so output previous customers totals
     {
     // output dog total summary for year (refer to Fig. 7-1 Java)
    
     // reset variarbles and control-break variable 'oldname'
     oldname = name;
     custTotal = 0;
     numCustomers= numCustomers + 1;
    
     //CALL calculateStay for new dog
     calculateStay(weight, weeks);
    
     //Start summing new dogs running total
    
     }
    
     // Add to total sales running total outside of our if-else structure
     // another way of writing "yearEndTotal = yearEndTotal + bill;"
     yearEndTotal += bill; 
    
    
    
     } //End of Loop
    
     //Output last dog total summary for year
    
    
    
     //Output Year End totals
    
    
     //close input file and close output file
    
    
     System.exit(0); //Ending program
    
    
     } //End main method
    
     //Starting calculateStay method 
     public static int calculateStay(int w, int s)
     {
     int rate;
    
     if( w < 15 )
     {
     rate = 55;
     return rate * s;
     }
    
     else if( w <=30 )
     {
     rate = 75;
     return rate * s;
    
     }
    
     else if( w <= 80 )
     {
     rate = 105;
    
     }
    
     else 
     {
     rate = 125;
    
     }
    
    
     } 
    
    
     }//End BarkingLot class
    Last edited by DarrylBurke; 07-01-2015 at 01:38 PM. Reason: Restored original content

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Need Help organizing the code!!

    You said you wanted help organizing your code. But you never stated what you thought was wrong with it or what you were seeking.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    Quote Originally Posted by jim829 View Post
    You said you wanted help organizing your code. But you never stated what you thought was wrong with it or what you were seeking.

    Regards,
    Jim
    Hello Jim, Yes, first of all can you check if my first part of the code is error free? (see below first part)
    -

    Java Code:
     import java.io.*;
    -
    public class BarkingLotReportASHAN //Begin BarkingLot class
     {
     public static void main(String args[]) throws Exception //Begin main method
     {
    
     /**Variable Declarations ********************/
     // variables to Read in Input Strings
     String name, weightString, weeksString;
    
     // variables to convert String input to ints so we can use in calculations
     int weeks, weight;
    -
     // results of calculations
     int bill, custTotal = 0, yearEndTotal = 0, numCustomers = 1;
    
     // declare control break variable
     String oldname = "";
    
     //File I/O open and I/O buffers (refer to Fig. 7-1 Java)
     FileReader fr = new FileReader("BarkingLotInput.txt");
     BufferedReader br = new BufferedReader(fr);
     FileWriter fw = new FileWriter("BarkingLotYearEndrpt.txt");
     PrintWriter pw = new PrintWriter(fw);
    
     weightString = br.readLine();
     weight = Integer.parseInt(weightString);
     weeksString = br.readLine();
     weeks = Integer.parseInt(weeksString);
    
     /****** end of variable Declarations *******/
    Last edited by DarrylBurke; 07-01-2015 at 01:40 PM. Reason: Restored original content

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Need Help organizing the code!!

    Only you can determine if you code is error free since someone else can't be expected to know what you want to do. Does it compile okay? If so, then syntax is okay. Does it provide the results you are expecting? If so then it must be okay for the provided input. However, your parseInt routines will throw exceptions if the strings contain invalid characters. So you need to try different inputs and use some error detection capability. I suggest you write your code and run it. Then if you get compile-time or runtime errors someone on this forum will probably be able to help. But you need to provide more detail as to where you are seeing problems.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    If you're using a program like eclipse to write your code, it should tell you if there's an error in there.
    or are you perhaps getting a different result than you expect and you don't know why?

  7. #7
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    Hello All,

    Ok, here is my code and the errors that im getting. Please let me know how to correct.

    Java Code:
    -
    import java.io.*;
    
     public class BarkingLotReportASHAN //Begin BarkingLot class
     {
     public static void main(String args[]) throws Exception //Begin main method
     {
    
     /**Variable Declarations ********************/
     // variables to Read in Input Strings
     String name, weightString, weeksString;
     final String TITLE = "\n\nBarking Lot 2015 Year-End Report by Doggie Customer\n\n";
    
     // variables to convert String input to ints so we can use in calculations
     int weeks, weight;
    
     // results of calculations
     int bill, custTotal = 0, yearEndTotal = 0, numCustomers = 1;
     int rate=0;
    
     // declare control break variable
     String oldname = "";
    
     //File I/O open and I/O buffers (refer to Fig. 7-1 Java)
     FileReader fr = new FileReader("BarkingLotInput.txt");
     BufferedReader br = new BufferedReader(fr);
     FileWriter fw = new FileWriter("BarkingLotYearEndrpt.txt");
     PrintWriter pw = new PrintWriter(fw);
    
     weightString = br.readLine();
     weight = Integer.parseInt(weightString);
     weeksString = br.readLine();
     weeks = Integer.parseInt(weeksString);
    
     /****** end of variable Declarations *******/
    
     // **** start our logic ***********//
    
     // Print out Report Header to File (refer Fig. 7-5 Java)
     System.out.println(TITLE);
    
     //While not EOF 
     while ((name = br.readLine())!= null )
     {
     // initialize our control-break variable first time in loop
     if (oldname.equals(""))
     oldname = name;
    
     //Read in data from Input File: weight and stay in weeks
     weightString = br.readLine();
     weeksString = br.readLine();
    
    
     // convert weightString and weeksString to int
     weight = Integer.parseInt(weightString);
     weeks = Integer.parseInt(weeksString);
    
     // if same Dog
     if (name.equals(oldname))
     {
     //CALL calculateStay
     calculateStay(weight, weeks);
    
     //Set dogs running total
     bill = rate * weeks;
    
     }
    
     else // we are on a new dog customer, so output previous customers totals
     {
     // output dog total summary for year (refer to Fig. 7-1 Java)
    
     // reset variarbles and control-break variable 'oldname'
     oldname = name;
     custTotal = 0;
     numCustomers= numCustomers + 1;
    
     //CALL calculateStay for new dog
     calculateStay(weight, weeks);
    
     //Start summing new dogs running total
    
     }
    
     // Add to total sales running total outside of our if-else structure
     // another way of writing "yearEndTotal = yearEndTotal + bill;"
     // yearEndTotal += bill
     yearEndTotal = yearEndTotal + bill; 
    
    
    
     } //End of Loop
    
     //Output last dog total summary for year
    
    
    
     //Output Year End totals
    
    
     //close input file and close output file
    
    
     //Ending program
    
    
     } //End main method
    
     //Starting calculateStay method 
     public static int calculateStay(int w, int s)
     {
     int rate;
    -
     if( w < 15 )
     {
     rate = 55;
    
     }
    
     else if( w <=30 )
     {
     rate = 75;
    
    
     }
    
     else if( w <= 80 )
     {
     rate = 105;
    
     }
    
     else 
     {
     rate = 125;
    
     }
    
    
     } <-------------------------------- It's this bracket that is getting me the error.
    
    
     }//End BarkingLot class
    Errors
    ----jGRASP exec: javac -g BarkingLotReportASHAN.java
    BarkingLotReportASHAN.java:146: error: missing return statement
    }
    ^
    BarkingLotReportASHAN.java:94: error: variable bill might not have been initialized
    yearEndTotal = yearEndTotal + bill;
    ^
    2 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    Last edited by DarrylBurke; 07-01-2015 at 01:41 PM. Reason: Restored original content

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need Help organizing the code!!

    BarkingLotReportASHAN.java:146: error: missing return statement
    Add a return statement for the method that ends at line 146.
    Or define the method as void to say it doesn't return a value.

    BarkingLotReportASHAN.java:94: error: variable bill might not have been initialized
    Assign the variable: bill a value before it is used.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    Quote Originally Posted by Norm View Post
    Add a return statement for the method that ends at line 146.
    Or define the method as void to say it doesn't return a value.


    Assign the variable: bill a value before it is used.
    line 146?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need Help organizing the code!!

    BarkingLotReportASHAN.java:146: error: missing return statement
    The error message shows the line number.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    Quote Originally Posted by Norm View Post
    The error message shows the line number.
    Hello,

    I'm kind of lost. What kind of a return statement is suitable?

  12. #12
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    Quote Originally Posted by anpk05 View Post
    Hello,

    I'm kind of lost. What kind of a return statement is suitable?
    I got it. Thanks

  13. #13
    anpk05 is offline Member
    Join Date
    Jun 2015
    Posts
    16
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    Well i compiled the code with out errors. :)

    Now i get the following error after running it. Any idea?

    ----jGRASP exec: java BarkingLotReportASHAN

    Exception in thread "main" java.lang.NumberFormatException: For input string: "Charlie"
    at java.lang.NumberFormatException.forInputString(Num berFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at BarkingLotReportASHAN.main(BarkingLotReportASHAN.j ava:37)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  14. #14
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Need Help organizing the code!!

    It would have been really helpful if the OP hadn't edited all their posts to delete the code content. Quite unnecessary. It makes this thread kind of meaningless and is no longer helpful to future visitors of the forum.

    What a shame.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Need Help organizing the code!!

    Quote Originally Posted by trcooke View Post
    It would have been really helpful if the OP hadn't edited all their posts to delete the code content. Quite unnecessary. It makes this thread kind of meaningless and is no longer helpful to future visitors of the forum.
    I agree. I've restored the content and I'm closing this thread.

    Any repeat of this and anpk05 iwll be banned for a week (or more if I'm in a bad mood).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Organizing a list of integers from greatest to least
    By natdizzle in forum New To Java
    Replies: 3
    Last Post: 08-17-2012, 11:55 PM
  2. Organizing (GUI-)Code
    By jDennis79 in forum New To Java
    Replies: 9
    Last Post: 08-13-2010, 10:58 PM
  3. Organizing Java code
    By PeterTaps in forum New To Java
    Replies: 2
    Last Post: 06-11-2010, 09:39 PM
  4. organizing larger application code / modules
    By zenner in forum Advanced Java
    Replies: 5
    Last Post: 07-28-2008, 09:18 AM

Posting Permissions

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