Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Beginner Java Program - need lots of help - lots of errors

Posted on 2004-10-30
54
Medium Priority
?
308 Views
Last Modified: 2008-02-01
This is my first Java Program - I am used to writing in C++.  I am trying to convert my C++ program into Java and I need to do it quick... don't have a lot of time for searching web sites... My code is supposed to calculate/write up a bill for a carpet company... it asks the user the customer name, room length and width, carpet price, and customer discount... I am using the free Java version C:\JDK v1.0.2\java Do you see any noticable errors???? (or I should say what are they)...
sorry about the format - didn't cut and paste very well...

 //Java program to calculate the bill for a customer
//buying carpet.  This program asks the user for the customer name,
//the length, width, price, and customer discount.

public class Carpet{

         void getData()
         {

      // Introduction
      System.out.println();
      System.out.println("Bill creator for Billie West's Carpet Company");
      System.out.println();

      System.out.println("Press ? at any time to exit the program.");
      System.out.println();

      System.out.println("Please enter the customer name:");
      customerName = TextIO.getln(); // value read is a String
      if(name[0] == '?')
      {
               System.out.println("Exiting Program");
               exit(1);
      }
            
      System.out.println("Length of Room (feet)?");
      length=TextIO.getlnDouble(); // value read is a double
      exit(length);
      System.out.println();
      
      System.out.println("Width of Room (feet)?");
      width=TextIO.getlnDouble(); // value read is a double
      exit(width);
      System.out.println();
            
      System.out.println("Customer Discount(percent)?");
      customerDiscount=TextIO.getlnDouble(); // value read is a double
      exit2(customerDiscount);
      System.out.println();


      System.out.println("Cost per square foot(xx.xx)?");
      costPerFt=TextIO.getlnDouble(); // value read is a double
      exit(costPerFt);
      System.out.println();
      }

      void calculate()
      {
            Install = calcInstall();
            Subtotal= calcSubtotal();
            Total = calcTotal();
      }

      void printResult()
      {
             DecimalFormat z = new DecimalFormat("#,##0.00");//not  
//sure about how to format decimal points to 2 places... found this on the net               string length = z.format(length);
            

      System.out.println();
      System.out.println();
      System.out.println();
      
      System.out.println("*******************************************************************");
      System.out.println();
      FormatWriter.printLeft(" ", 15);
      FormatWriter.printLeft("Customer Name: ",17);
      FormatWriter.printLeft(customerName);
      System.out.println();


      FormatWriter.printLeft(" ", 20);
      FormatWriter.printLeft("MEASUREMENT",17);
      System.out.println();

      FormatWriter.printLeft(" ", 15);
      FormatWriter.printLeft("Length is",15);
      FormatWriter.printLeft(length,15);
      FormatWriter.printLeft("feet",15);            
      System.out.println();

      FormatWriter.printLeft(" ", 15);
      FormatWriter.printLeft("Width is",15);
      FormatWriter.printLeft(width,15);
      FormatWriter.printLeft("feet",15);            
      System.out.println();
      FormatWriter.printLeft(" ", 15);
      FormatWriter.printLeft("Area is",15);
      FormatWriter.printLeft(area,15);
      FormatWriter.printLeft("feet",15);            
      System.out.println();

      FormatWriter.printLeft(" ", 20);
      FormatWriter.printLeft("CHARGES",15);
      System.out.println();
            
      FormatWriter.printLeft("DESCRIPTION",20);                  
      FormatWriter.printLeft("COST/SQ.Ft.",20);
      FormatWriter.printLeft("CHARGE/ROOM",20);      
      System.out.println();            
      
      FormatWriter.printLeft("Carpet",20);                  
      FormatWriter.printLeft(costPerFt,20);
      FormatWriter.printLeft(chrgCarpet,20);      
      System.out.println();      

      FormatWriter.printLeft("Labor",20);            
      FormatWriter.printLeft(labor,20);
      FormatWriter.printLeft(chrgLabor,20);      
      System.out.println();      
      
                FormatWriter.printLeft("Installed Price",20);                  
      FormatWriter.printLeft(Install,20);
      System.out.println();            
            
      FormatWriter.printLeft("Discount",20);
      System.out.println(customerDiscount);
      FormatWriter.printLeft("%",15);
      FormatWriter.printLeft(totalDiscount,20);            
      System.out.println();

      FormatWriter.printLeft("SUBTOTAL",20);
      FormatWriter.printLeft(Subtotal,20);
      System.out.println();
            
      FormatWriter.printLeft("Tax",20);
      FormatWriter.printLeft(Tax,20);
      System.out.println();      

      FormatWriter.printLeft("Total",20);
      FormatWriter.printLeft(Total,20);
      System.out.println();
      System.out.println();
      System.out.println("*******************************************************************");
      System.out.println();
      }


      double calcInstall()
      {
            area = length * width;
            chrgCarpet= area*costPerFt;
            chrgLabor= labor* area;
            return(chrgCarpet + chrgLabor);
      }


      double calcSubtotal()
      {
      totalDiscount= customerDiscount/100*Install;
      return(Install - totalDiscount);
      }


      double calcTotal()
      {
      Tax = taxRate/100*Subtotal;
      return(Subtotal + Tax);
      }

      void exit(double input)
      {
         if(input<0||input>5000)
         {
          FormatWriter.printLeft("Invalid Input: ",20);
          FormatWriter.printLeft(input,5);
          FormatWriter.printLeft("won't work...",20);
          System.out.println();
          System.out.println("Exiting Program");
          exit(2);
          }

           if(input=='?')
           {                  
            System.out.println("Exiting Program by User Request");
            exit(3);
            }
      }

      void exit2(double input)
      {
           if(input<0||input>100)
           {
            FormatWriter.printLeft("Invalid Input: ",20);
            FormatWriter.printLeft(input,5);
            FormatWriter.printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
             }
             if(input=='?')
             {
            System.out.println("Exiting Program by User Request");
            exit(3);
              }
      }

      public static int main()
      {

             string customerName;
             double length, width, area, customerDiscount, costPerFt;
             double labor=0.35, taxRate=8.25, Install;
             double chrgCarpet, chrgLabor, totalDiscount, Subtotal, Tax, Total;
                  
              getData();
              calculate();
              printResult();
              System.out.println("Exiting Program...");

              return 0;
         }
 }
0
Comment
Question by:bjw5d6
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 21
  • 17
  • 16
54 Comments
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455015
Hi bjw5d6,
What errors do you get when you compile?

Your main method should be as this:

public static void main(String args[])
{
    ...
}

Don't return anything from main() and put those parameters there.

Cheers!
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455039
bjw5d6,
Oh, actually, there are a lot of problems here. I just kind of glanced at it at first ... I may see if I can rewrite it quickly. This doesn't rely on another class does it?
0
 

Author Comment

by:bjw5d6
ID: 12455070
I don't think it should rely on another class, but I'm not sure if I should take the
functions inside the carpet class (like void calculate()) and make them into their
own separate classes... I'm not even sure what is supposed to return values and what isn't.  So, I guess I need to take the return(0) from main?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455077
Yes, I'm kind of trying to just rewrite it real quick if it doesn't take too long ... give me a few minutes.
0
 

Author Comment

by:bjw5d6
ID: 12455081
So far my error reads:


P2WestCS441.java:4: Warning: Public class Carpet must be defined in a file called "Carpet.java".
public class Carpet{
             ^
1 error
Exit code: 1
There were errors

But I know there are tons of them...  
0
 
LVL 10

Assisted Solution

by:NetworkArchitek
NetworkArchitek earned 80 total points
ID: 12455112
bjw5d6,
Oh, that one is easy. You have to call the class file name by the name of the class. Your class must be called Carpet.java if it begins with
"public class Carprt{ .. }"

But there were many other problems ... What is the "name[]" array coming from? I don't see it declared anywhere. Are you trying to make customerName populate a string array called "name" ?
0
 

Author Comment

by:bjw5d6
ID: 12455152
That array name[] was a mistake to leave in the Java program.  I had to read the
name in C++ as an array of characters.  I guess in Java it was okay to use the code:
 
customerName = TextIO.getln(); // value read is a String

so that name[] should just be customerName.  Sorry for the confusion and thanks for looking into this!
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455186
bjw5d6,
oh well darn. I rewrote the whole "name" logic but I did notice you were using to get a char. Although you can't compare a string to a char directly. Well, if I finish, I'll just tell you what I did then.
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455201
bjw5d6,
I'm sorry but what exactly are you tryingt to accomplish with the "exit(length)" and "exit(width" and so forth ..
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455226
bjw5d6,
If you wouldn't mind answering that last question it would be great. But, without me going to far in analyzing this code could you just tell me what you are trying to accomplish exactly and I will see if it would just be quicker for me to write it from scratch for you or to edit this stuff. Like give me what you want outputted and all that.
0
 

Author Comment

by:bjw5d6
ID: 12455231
I'm just trying to have a way for the user to exit the program in the middle by
entering a ? and at the same time exiting the program if the user enters a negative
number or something bigger than 100 (for the discount) or something bigger than 5000 for the length or width...

Also, I really just wanted to give an upper and lower bound for the length and width so if the user enters in a character instead of an integer, it wouldn't be in the range and
the program would exit then too...
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455253
bjw5d6,
That makes sense but I don't see where you are checking for negative values ... its just "exiting" ...
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455263
bjw5d6,
Also when you do a System.println(), do you intend to actually output "length" if it is valid or are you just trying to print a blank line? Sorry, I know its weird going from one language to another. If you want, just tell me exactly what this thing is supposed to do and I may just write it out from scratch, it doesn't look like it would take very long.
0
 

Author Comment

by:bjw5d6
ID: 12455278
The program prompt/ input is supposed to be something like this:
Enter the customer name: James West
Length of Room (feet)? 30
Width of Room (feet)? 18
Customer Discount(percent)? 9
Cost per square foot (xxx.xx)?8.99

Sample output should look something like this:
*******************************************************
                  Customer Name: James West

            MEASUREMENT

            Length xxx feet
            Width xxx feet
            Area xxx square feet

            CHARGES
DESCRIPTION            COST/SQ.FT.            CHARGE/ROOM
Carpet                  xxx.xx                                      $xxxx.xx
Labor                  0.35                   xxxx.xx
INSTALLED PRICE                              $xxxx.xx
Discount                            xx.x%                  xxxx.xx
SUBTOTAL                                    $xxxx.xx
Tax                                     xxx.xx
TOTAL                                    $xxxx.xx
                  
*********************************************************



0
 

Author Comment

by:bjw5d6
ID: 12455290
The above comment didn't print out the format very well, I'd like it to be lined up.
Yes, when I use the system.println() I'm just trying to print a blank line.
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455344
bjw5d6,
ok, well give me a minute, I will see if it won't take too long to write out. I'll format it a little but you can go back and make it pretty if I do submit something to you. Give me a few minutes.
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455429
bjw5d6,
I am working on it and nearly done ... just to let you know.
0
 

Author Comment

by:bjw5d6
ID: 12455432
Thanks, I really appreciate this...
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455437
Oh, I thought you were using System.exit() .... I didn't realize you had a static method called exit() .. er give me a little more time .. lol
0
 

Author Comment

by:bjw5d6
ID: 12455452
Whatever works for you... If you think one exit would be better than another that is okay.  I don't know anything about Java, and I will be learning "best practices" from
what you think, so do what suites you so I can learn the best way...
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455469
Ok, I'm rewriting the error handling a little ... keep you posted.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455474
Your code should be some thing like this

import java.io.*;
import java.util.*;
public class Carpet{
   
    void getData() {
       
        // Introduction
        System.out.println();
        System.out.println("Bill creator for Billie West's Carpet Company");
        System.out.println();
       
        System.out.println("Press ? at any time to exit the program.");
        System.out.println();
       
        System.out.println("Please enter the customer name:");
        customerName = readLine();
        if(customerName.charAt(0) == '?') {
            System.out.println("Exiting Program");
            exit(1);
        }
       
        System.out.println("Length of Room (feet)?");
        length=getDouble(); // value read is a double
        exit(length);
        System.out.println();
       
        System.out.println("Width of Room (feet)?");
        width=getDouble(); // value read is a double
        exit(width);
        System.out.println();
       
        System.out.println("Customer Discount(percent)?");
        customerDiscount=getDouble(); // value read is a double
        exit2(customerDiscount);
        System.out.println();
       
       
        System.out.println("Cost per square foot(xx.xx)?");
        costPerFt=getDouble(); // value read is a double
        exit(costPerFt);
        System.out.println();
    }
   
    void calculate() {
        Install = calcInstall();
        Subtotal= calcSubtotal();
        Total = calcTotal();
    }
   
    void printResult() {
       
       
       
       
        System.out.println();
        System.out.println();
        System.out.println();
       
        System.out.println("*******************************************************************");
        System.out.println();
        printLeft(" ", 15);
        printLeft("Customer Name: ",17);
        printLeft(""+customerName);
        System.out.println();
       
       
        printLeft(" ", 20);
        printLeft("MEASUREMENT",17);
        System.out.println();
       
        printLeft(" ", 15);
        printLeft("Length is",15);
        printLeft(""+length,15);
        printLeft("feet",15);
        System.out.println();
       
        printLeft(" ", 15);
        printLeft("Width is",15);
        printLeft(""+width,15);
        printLeft("feet",15);
        System.out.println();
        printLeft(" ", 15);
        printLeft("Area is",15);
        printLeft(""+area,15);
        printLeft("feet",15);
        System.out.println();
       
        printLeft(" ", 20);
        printLeft("CHARGES",15);
        System.out.println();
       
        printLeft("DESCRIPTION",20);
        printLeft("COST/SQ.Ft.",20);
        printLeft("CHARGE/ROOM",20);
        System.out.println();
       
        printLeft("Carpet",20);
        printLeft(""+costPerFt,20);
        printLeft(""+chrgCarpet,20);
        System.out.println();
       
        printLeft("Labor",20);
        printLeft(""+labor,20);
        printLeft(""+chrgLabor,20);
        System.out.println();
       
        printLeft("Installed Price",20);
        printLeft(""+Install,20);
        System.out.println();
       
        printLeft("Discount",20);
        System.out.println(customerDiscount);
        printLeft("%",15);
        printLeft(""+totalDiscount,20);
        System.out.println();
       
        printLeft("SUBTOTAL",20);
        printLeft(""+Subtotal,20);
        System.out.println();
       
        printLeft("Tax",20);
        printLeft(""+Tax,20);
        System.out.println();
       
        printLeft("Total",20);
        printLeft(""+Total,20);
        System.out.println();
        System.out.println();
        System.out.println("*******************************************************************");
        System.out.println();
    }
   
   
    double calcInstall() {
        area = length * width;
        chrgCarpet= area*costPerFt;
        chrgLabor= labor* area;
        return(chrgCarpet + chrgLabor);
    }
   
   
    double calcSubtotal() {
        totalDiscount= customerDiscount/100*Install;
        return(Install - totalDiscount);
    }
   
   
    double calcTotal() {
        Tax = taxRate/100*Subtotal;
        return(Subtotal + Tax);
    }
   
    void exit(double input) {
        if(input<0||input>5000) {
            printLeft("Invalid Input: ",20);
            printLeft(""+input,5);
            printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
            exit(2);
        }
       
        if(input=='?') {
            System.out.println("Exiting Program by User Request");
            exit(3);
        }
    }
   
    void exit2(double input) {
        if(input<0||input>100) {
            printLeft("Invalid Input: ",20);
            printLeft(""+input,5);
            printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
        }
        if(input=='?') {
            System.out.println("Exiting Program by User Request");
            exit(3);
        }
    }
   
    public static void main(String s[]) {
        Carpet c=new Carpet();
        c.getData();
        c.calculate();
        c.printResult();
        System.out.println("Exiting Program...");
       
       
    }
   
    public double getDouble() {
        return Double.parseDouble(readLine());
    }
   
    public String readLine() {
        String str = "";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
           
            while (str != null) {
               
                str = in.readLine();
               
               
            }
        } catch (IOException e) {
        }
        return str;
       
    }
    public void printLeft(String str,int n) {
        for(int i=0;i<n;i++) {
            System.out.print(" ");
        }
        System.out.print(str);
    }
   
   
    String customerName;
    double length, width, area, customerDiscount, costPerFt;
    double labor=0.35, taxRate=8.25, Install;
    double chrgCarpet, chrgLabor, totalDiscount, Subtotal, Tax, Total;
   
}

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455481
import java.io.*;
import java.util.*;
public class Carpet{
   
    void getData() {
       
        // Introduction
        System.out.println();
        System.out.println("Bill creator for Billie West's Carpet Company");
        System.out.println();
       
        System.out.println("Press ? at any time to exit the program.");
        System.out.println();
       
        System.out.println("Please enter the customer name:");
        customerName = readLine();
        if(customerName.charAt(0) == '?') {
            System.out.println("Exiting Program");
            exit(1);
        }
       
        System.out.println("Length of Room (feet)?");
        length=getDouble(); // value read is a double
        exit(length);
        System.out.println();
       
        System.out.println("Width of Room (feet)?");
        width=getDouble(); // value read is a double
        exit(width);
        System.out.println();
       
        System.out.println("Customer Discount(percent)?");
        customerDiscount=getDouble(); // value read is a double
        exit2(customerDiscount);
        System.out.println();
       
       
        System.out.println("Cost per square foot(xx.xx)?");
        costPerFt=getDouble(); // value read is a double
        exit(costPerFt);
        System.out.println();
    }
   
    void calculate() {
        Install = calcInstall();
        Subtotal= calcSubtotal();
        Total = calcTotal();
    }
   
    void printResult() {
       
       
       
       
        System.out.println();
        System.out.println();
        System.out.println();
       
        System.out.println("*******************************************************************");
        System.out.println();
        printLeft(" ", 15);
        printLeft("Customer Name: ",17);
        printLeft(""+customerName);
        System.out.println();
       
       
        printLeft(" ", 20);
        printLeft("MEASUREMENT",17);
        System.out.println();
       
        printLeft(" ", 15);
        printLeft("Length is",15);
        printLeft(""+length,15);
        printLeft("feet",15);
        System.out.println();
       
        printLeft(" ", 15);
        printLeft("Width is",15);
        printLeft(""+width,15);
        printLeft("feet",15);
        System.out.println();
        printLeft(" ", 15);
        printLeft("Area is",15);
        printLeft(""+area,15);
        printLeft("feet",15);
        System.out.println();
       
        printLeft(" ", 20);
        printLeft("CHARGES",15);
        System.out.println();
       
        printLeft("DESCRIPTION",20);
        printLeft("COST/SQ.Ft.",20);
        printLeft("CHARGE/ROOM",20);
        System.out.println();
       
        printLeft("Carpet",20);
        printLeft(""+costPerFt,20);
        printLeft(""+chrgCarpet,20);
        System.out.println();
       
        printLeft("Labor",20);
        printLeft(""+labor,20);
        printLeft(""+chrgLabor,20);
        System.out.println();
       
        printLeft("Installed Price",20);
        printLeft(""+Install,20);
        System.out.println();
       
        printLeft("Discount",20);
        System.out.println(customerDiscount);
        printLeft("%",15);
        printLeft(""+totalDiscount,20);
        System.out.println();
       
        printLeft("SUBTOTAL",20);
        printLeft(""+Subtotal,20);
        System.out.println();
       
        printLeft("Tax",20);
        printLeft(""+Tax,20);
        System.out.println();
       
        printLeft("Total",20);
        printLeft(""+Total,20);
        System.out.println();
        System.out.println();
        System.out.println("*******************************************************************");
        System.out.println();
    }
   
   
    double calcInstall() {
        area = length * width;
        chrgCarpet= area*costPerFt;
        chrgLabor= labor* area;
        return(chrgCarpet + chrgLabor);
    }
   
   
    double calcSubtotal() {
        totalDiscount= customerDiscount/100*Install;
        return(Install - totalDiscount);
    }
   
   
    double calcTotal() {
        Tax = taxRate/100*Subtotal;
        return(Subtotal + Tax);
    }
   
    void exit(double input) {
        if(input<0||input>5000) {
            printLeft("Invalid Input: ",20);
            printLeft(""+input,5);
            printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
            exit(2);
        }
       
        if(input=='?') {
            System.out.println("Exiting Program by User Request");
            exit(3);
        }
    }
   
    void exit2(double input) {
        if(input<0||input>100) {
            printLeft("Invalid Input: ",20);
            printLeft(""+input,5);
            printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
        }
        if(input=='?') {
            System.out.println("Exiting Program by User Request");
            exit(3);
        }
    }
   
    public static void main(String s[]) {
        Carpet c=new Carpet();
        c.getData();
        c.calculate();
        c.printResult();
        System.out.println("Exiting Program...");
       
       
    }
   
    public double getDouble() {
        return Double.parseDouble(readLine());
    }
   
    public String readLine() {
        String str = "";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
           
            while (str != null) {
               
                str = in.readLine();
               
               
            }
        } catch (IOException e) {
        }
        return str;
       
    }
    public void printLeft(String str,int n) {
        for(int i=0;i<n;i++) {
            System.out.print(" ");
        }
        System.out.print(str);
    }
    public void printLeft(String str) {
        printLeft(str,0);
       
    }
   
   
   
    String customerName;
    double length, width, area, customerDiscount, costPerFt;
    double labor=0.35, taxRate=8.25, Install;
    double chrgCarpet, chrgLabor, totalDiscount, Subtotal, Tax, Total;
   
}

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455493
a small change in readLine method
You should replace exitsting readLine method of mine to this one



public String readLine() {
        String str = "";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
           
           
           
            str = in.readLine();
           
           
           
        } catch (IOException e) {
        }
        return str;
       
    }
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455502
I have changed following things in your program.

FormatWritter.printLeft to printLeft

TextIO.readLn to readLine
TextIo.readDouble to getDouble

and nothing beyond this

0
 

Author Comment

by:bjw5d6
ID: 12455584
Ok, I'm checking this out... a few minutes...
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455588
good ... pay no attention to the conversation .. especially if it doesn't compile anyway .. hm
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455615



So final program should be like this. Let me know if you have any doubts.

import java.io.*;
import java.util.*;
public class Carpet{
   
    void getData() {
       
        // Introduction
        System.out.println();
        System.out.println("Bill creator for Billie West's Carpet Company");
        System.out.println();
       
        System.out.println("Press ? at any time to exit the program.");
        System.out.println();
       
        System.out.println("Please enter the customer name:");
        customerName = readLine();
        if(customerName.charAt(0) == '?') {
            System.out.println("Exiting Program");
            exit(1);
        }
       
        System.out.println("Length of Room (feet)?");
        length=getDouble(); // value read is a double
        exit(length);
        System.out.println();
       
        System.out.println("Width of Room (feet)?");
        width=getDouble(); // value read is a double
        exit(width);
        System.out.println();
       
        System.out.println("Customer Discount(percent)?");
        customerDiscount=getDouble(); // value read is a double
        exit2(customerDiscount);
        System.out.println();
       
       
        System.out.println("Cost per square foot(xx.xx)?");
        costPerFt=getDouble(); // value read is a double
        exit(costPerFt);
        System.out.println();
    }
   
    void calculate() {
        Install = calcInstall();
        Subtotal= calcSubtotal();
        Total = calcTotal();
    }
   
    void printResult() {
       
       
       
       
        System.out.println();
        System.out.println();
        System.out.println();
       
        System.out.println("*******************************************************************");
        System.out.println();
        printLeft(" ", 15);
        printLeft("Customer Name: ",17);
        printLeft(""+customerName);
        System.out.println();
       
       
        printLeft(" ", 20);
        printLeft("MEASUREMENT",17);
        System.out.println();
       
        printLeft(" ", 15);
        printLeft("Length is",15);
        printLeft(""+length,15);
        printLeft("feet",15);
        System.out.println();
       
        printLeft(" ", 15);
        printLeft("Width is",15);
        printLeft(""+width,15);
        printLeft("feet",15);
        System.out.println();
        printLeft(" ", 15);
        printLeft("Area is",15);
        printLeft(""+area,15);
        printLeft("feet",15);
        System.out.println();
       
        printLeft(" ", 20);
        printLeft("CHARGES",15);
        System.out.println();
       
        printLeft("DESCRIPTION",20);
        printLeft("COST/SQ.Ft.",20);
        printLeft("CHARGE/ROOM",20);
        System.out.println();
       
        printLeft("Carpet",20);
        printLeft(""+costPerFt,20);
        printLeft(""+chrgCarpet,20);
        System.out.println();
       
        printLeft("Labor",20);
        printLeft(""+labor,20);
        printLeft(""+chrgLabor,20);
        System.out.println();
       
        printLeft("Installed Price",20);
        printLeft(""+Install,20);
        System.out.println();
       
        printLeft("Discount",20);
        System.out.println(customerDiscount);
        printLeft("%",15);
        printLeft(""+totalDiscount,20);
        System.out.println();
       
        printLeft("SUBTOTAL",20);
        printLeft(""+Subtotal,20);
        System.out.println();
       
        printLeft("Tax",20);
        printLeft(""+Tax,20);
        System.out.println();
       
        printLeft("Total",20);
        printLeft(""+Total,20);
        System.out.println();
        System.out.println();
        System.out.println("*******************************************************************");
        System.out.println();
    }
   
   
    double calcInstall() {
        area = length * width;
        chrgCarpet= area*costPerFt;
        chrgLabor= labor* area;
        return(chrgCarpet + chrgLabor);
    }
   
   
    double calcSubtotal() {
        totalDiscount= customerDiscount/100*Install;
        return(Install - totalDiscount);
    }
   
   
    double calcTotal() {
        Tax = taxRate/100*Subtotal;
        return(Subtotal + Tax);
    }
   
    void exit(double input) {
        if(input<0||input>5000) {
            printLeft("Invalid Input: ",20);
            printLeft(""+input,5);
            printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
            exit(2);
        }
       
        if(input=='?') {
            System.out.println("Exiting Program by User Request");
            exit(3);
        }
    }
   
    void exit2(double input) {
        if(input<0||input>100) {
            printLeft("Invalid Input: ",20);
            printLeft(""+input,5);
            printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
        }
        if(input=='?') {
            System.out.println("Exiting Program by User Request");
            exit(3);
        }
    }
   
    public static void main(String s[]) {
        Carpet c=new Carpet();
        c.getData();
        c.calculate();
        c.printResult();
        System.out.println("Exiting Program...");
       
       
    }
   
    public double getDouble() {
        return Double.parseDouble(readLine());
    }
   
    public String readLine() {
        String str = "";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
           
           
           
            str = in.readLine();
           
           
           
        } catch (IOException e) {
        }
        return str;
       
    }
    public void printLeft(String str,int n) {
        for(int i=0;i<n;i++) {
            System.out.print(" ");
        }
        System.out.print(str);
    }
    public void printLeft(String str) {
        printLeft(str,0);
       
    }
   
   
   
    String customerName;
    double length, width, area, customerDiscount, costPerFt;
    double labor=0.35, taxRate=8.25, Install;
    double chrgCarpet, chrgLabor, totalDiscount, Subtotal, Tax, Total;
   
}

0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455680
No that's not right. You didn't handle the exceptions. Yes, I could have just thrown that together by just haphazardly translating what he had to java and then using a BufferedStream reader even though he already has TextIO but ... oh well nevermind. I don't know why I post on EE... sheesh.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455692
for your information


1. The main method should be some thing like this
public static void main(String s[])

2. As there is no printLeft method available with Java
I have introduced printLeft Method  as overloaded. i.e 2 methods you have

3. for reading trom console device we need to use java.lang.System calls.

So I write readLine method for that.

4. for double reading from console, I call readLine method and parse it double value.

5. Finally I just replaced your calls with my calls. Nothing beyond that and no change in code logic

Regards
Sudhakar



0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455702
sudhakar_koundinya,
I told him about main() in the first post. There is a very popular FormatWriter.java which he is using, just like TextIO.java. He was just using classes already written ... That's not really where the problems were ... But ok.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455707
>> No that's not right. You didn't handle the exceptions

I didn't get you. Could you please tell where the program will not work??

Regards
Sudhakar
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455717
>>There is a very popular FormatWriter.java which he is using, just like TextIO.java
I don't know abt them. I never used ;-)

And If that is the case then he should do small changes.

His main method should be like this
public static void main(String s[]) {
        Carpet c=new Carpet();
        c.getData();
        c.calculate();
        c.printResult();
        System.out.println("Exiting Program...");
       
       
    }

and variables should be declared at class level.

public class Carpet
{
    String customerName;
    double length, width, area, customerDiscount, costPerFt;
    double labor=0.35, taxRate=8.25, Install;
    double chrgCarpet, chrgLabor, totalDiscount, Subtotal, Tax, Total;



And nothing to change beyond that. Isn't it.
0
 

Author Comment

by:bjw5d6
ID: 12455720
Ok, sorry I'm taking so long - I'm starting to go cross-eyed...
I'm getting the following errors:

MyApp.java:169: 'try' without 'catch' or 'finally'.
            } catch (IOException e) {
            ^
MyApp.java:169: Type expected.
            } catch (IOException e) {
              ^
MyApp.java:3: Warning: Public class Carpet must be defined in a file called "Carpet.java".
public class Carpet{
             ^
3 errors
Exit code: 1
There were errors

I renamed my file Carpet.java - do I need to rename my project this too?
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455723
sudhakar_koundinya,
Run the program and do not use ideal input. It blows up. Isn't it? That's why I was re-writing it ... But ok.
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455725
Yes it all has to be Carpet.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455741
MyApp.java should be  Carpet.java
0
 

Author Comment

by:bjw5d6
ID: 12455750
I finally got MyApp.java to be Carpet.java, then I got an unholy error.  
I started again, and now I'm getting more errors... I know this is my fault - I'll keep working on it...

MyApp.java:2: Package java.awt.event not found in import.
import java.awt.event.*;
^
MyApp.java:8: ')' expected.
            this.addWindowListener      (new WindowAdapter(){
                                                            ^
MyApp.java:8: '}' expected.
            this.addWindowListener      (new WindowAdapter(){
                                                             ^
MyApp.java:8: '}' expected.
            this.addWindowListener      (new WindowAdapter(){
                                                             ^
MyApp.java:9: Statement expected.
                  public void windowClosing(WindowEvent e){
                  ^
MyApp.java:13: Class or interface declaration expected.
            });
             ^
6 errors
Exit code: 1
There were errors
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455752
DId u copy the my last post and compile the program??

The code starts after this line in one of my previous comments
"So final program should be like this. Let me know if you have any doubts."

0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 1920 total points
ID: 12455754
I suggest you to copy below code in Carpet.java and compile it

Regards
Sudhakar




import java.io.*;
import java.util.*;
public class Carpet{
   
    void getData() {
       
        // Introduction
        System.out.println();
        System.out.println("Bill creator for Billie West's Carpet Company");
        System.out.println();
       
        System.out.println("Press ? at any time to exit the program.");
        System.out.println();
       
        System.out.println("Please enter the customer name:");
        customerName = readLine();
        if(customerName.charAt(0) == '?') {
            System.out.println("Exiting Program");
            exit(1);
        }
       
        System.out.println("Length of Room (feet)?");
        length=getDouble(); // value read is a double
        exit(length);
        System.out.println();
       
        System.out.println("Width of Room (feet)?");
        width=getDouble(); // value read is a double
        exit(width);
        System.out.println();
       
        System.out.println("Customer Discount(percent)?");
        customerDiscount=getDouble(); // value read is a double
        exit2(customerDiscount);
        System.out.println();
       
       
        System.out.println("Cost per square foot(xx.xx)?");
        costPerFt=getDouble(); // value read is a double
        exit(costPerFt);
        System.out.println();
    }
   
    void calculate() {
        Install = calcInstall();
        Subtotal= calcSubtotal();
        Total = calcTotal();
    }
   
    void printResult() {
       
       
       
       
        System.out.println();
        System.out.println();
        System.out.println();
       
        System.out.println("*******************************************************************");
        System.out.println();
        printLeft(" ", 15);
        printLeft("Customer Name: ",17);
        printLeft(""+customerName);
        System.out.println();
       
       
        printLeft(" ", 20);
        printLeft("MEASUREMENT",17);
        System.out.println();
       
        printLeft(" ", 15);
        printLeft("Length is",15);
        printLeft(""+length,15);
        printLeft("feet",15);
        System.out.println();
       
        printLeft(" ", 15);
        printLeft("Width is",15);
        printLeft(""+width,15);
        printLeft("feet",15);
        System.out.println();
        printLeft(" ", 15);
        printLeft("Area is",15);
        printLeft(""+area,15);
        printLeft("feet",15);
        System.out.println();
       
        printLeft(" ", 20);
        printLeft("CHARGES",15);
        System.out.println();
       
        printLeft("DESCRIPTION",20);
        printLeft("COST/SQ.Ft.",20);
        printLeft("CHARGE/ROOM",20);
        System.out.println();
       
        printLeft("Carpet",20);
        printLeft(""+costPerFt,20);
        printLeft(""+chrgCarpet,20);
        System.out.println();
       
        printLeft("Labor",20);
        printLeft(""+labor,20);
        printLeft(""+chrgLabor,20);
        System.out.println();
       
        printLeft("Installed Price",20);
        printLeft(""+Install,20);
        System.out.println();
       
        printLeft("Discount",20);
        System.out.println(customerDiscount);
        printLeft("%",15);
        printLeft(""+totalDiscount,20);
        System.out.println();
       
        printLeft("SUBTOTAL",20);
        printLeft(""+Subtotal,20);
        System.out.println();
       
        printLeft("Tax",20);
        printLeft(""+Tax,20);
        System.out.println();
       
        printLeft("Total",20);
        printLeft(""+Total,20);
        System.out.println();
        System.out.println();
        System.out.println("*******************************************************************");
        System.out.println();
    }
   
   
    double calcInstall() {
        area = length * width;
        chrgCarpet= area*costPerFt;
        chrgLabor= labor* area;
        return(chrgCarpet + chrgLabor);
    }
   
   
    double calcSubtotal() {
        totalDiscount= customerDiscount/100*Install;
        return(Install - totalDiscount);
    }
   
   
    double calcTotal() {
        Tax = taxRate/100*Subtotal;
        return(Subtotal + Tax);
    }
   
    void exit(double input) {
        if(input<0||input>5000) {
            printLeft("Invalid Input: ",20);
            printLeft(""+input,5);
            printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
            exit(2);
        }
       
        if(input=='?') {
            System.out.println("Exiting Program by User Request");
            exit(3);
        }
    }
   
    void exit2(double input) {
        if(input<0||input>100) {
            printLeft("Invalid Input: ",20);
            printLeft(""+input,5);
            printLeft("won't work...",20);
            System.out.println();
            System.out.println("Exiting Program");
        }
        if(input=='?') {
            System.out.println("Exiting Program by User Request");
            exit(3);
        }
    }
   
    public static void main(String s[]) {
        Carpet c=new Carpet();
        c.getData();
        c.calculate();
        c.printResult();
        System.out.println("Exiting Program...");
       
       
    }
   
    public double getDouble() {
        return Double.parseDouble(readLine());
    }
   
    public String readLine() {
        String str = "";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
           
           
           
            str = in.readLine();
           
           
           
        } catch (IOException e) {
        }
        return str;
       
    }
    public void printLeft(String str,int n) {
        for(int i=0;i<n;i++) {
            System.out.print(" ");
        }
        System.out.print(str);
    }
    public void printLeft(String str) {
        printLeft(str,0);
       
    }
   
   
   
    String customerName;
    double length, width, area, customerDiscount, costPerFt;
    double labor=0.35, taxRate=8.25, Install;
    double chrgCarpet, chrgLabor, totalDiscount, Subtotal, Tax, Total;
   
}

0
 

Author Comment

by:bjw5d6
ID: 12455756
Okay, I've got it figured out...

Here are my 2 errors now:

Carpet.java:169: 'try' without 'catch' or 'finally'.
            } catch (IOException e) {
            ^
Carpet.java:169: Type expected.
            } catch (IOException e) {
              ^
2 errors
Exit code: 1
There were errors
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455767
Can u post ur code again??
0
 

Author Comment

by:bjw5d6
ID: 12455784
Here is the part of the code where I'm getting the errors:

public double getDouble() {
      return Double.parseDouble(readLine());
}
      
public String readLine() {
      String str = "";
      try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                              str = in.readLine();
                  } catch (IOException e) {
            }
            return str;
      }
      
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455788
that code is ok

can u post full code??
0
 

Author Comment

by:bjw5d6
ID: 12455793
import java.io.*;
import java.util.*;
public class Carpet{

void getData() {

// Introduction
System.out.println();
System.out.println("Bill creator for Billie West's Carpet Company");
System.out.println();

System.out.println("Press ? at any time to exit the program.");
System.out.println();

System.out.println("Please enter the customer name:");
customerName = readLine();
if(customerName.charAt(0) == '?') {
System.out.println("Exiting Program");
exit(1);
}

System.out.println("Length of Room (feet)?");
length=getDouble(); // value read is a double
exit(length);
System.out.println();

System.out.println("Width of Room (feet)?");
width=getDouble(); // value read is a double
exit(width);
System.out.println();

System.out.println("Customer Discount(percent)?");
customerDiscount=getDouble(); // value read is a double
exit2(customerDiscount);
System.out.println();


System.out.println("Cost per square foot(xx.xx)?");
costPerFt=getDouble(); // value read is a double
exit(costPerFt);
System.out.println();
}

void calculate() {
Install = calcInstall();
Subtotal= calcSubtotal();
Total = calcTotal();
}

void printResult() {




System.out.println();
System.out.println();
System.out.println();

System.out.println("*******************************************************************");
System.out.println();
printLeft(" ", 15);
printLeft("Customer Name: ",17);
printLeft(""+customerName);
System.out.println();


printLeft(" ", 20);
printLeft("MEASUREMENT",17);
System.out.println();

printLeft(" ", 15);
printLeft("Length is",15);
printLeft(""+length,15);
printLeft("feet",15);
System.out.println();

printLeft(" ", 15);
printLeft("Width is",15);
printLeft(""+width,15);
printLeft("feet",15);
System.out.println();
printLeft(" ", 15);
printLeft("Area is",15);
printLeft(""+area,15);
printLeft("feet",15);
System.out.println();

printLeft(" ", 20);
printLeft("CHARGES",15);
System.out.println();

printLeft("DESCRIPTION",20);
printLeft("COST/SQ.Ft.",20);
printLeft("CHARGE/ROOM",20);
System.out.println();

printLeft("Carpet",20);
printLeft(""+costPerFt,20);
printLeft(""+chrgCarpet,20);
System.out.println();

printLeft("Labor",20);
printLeft(""+labor,20);
printLeft(""+chrgLabor,20);
System.out.println();

printLeft("Installed Price",20);
printLeft(""+Install,20);
System.out.println();

printLeft("Discount",20);
System.out.println(customerDiscount);
printLeft("%",15);
printLeft(""+totalDiscount,20);
System.out.println();

printLeft("SUBTOTAL",20);
printLeft(""+Subtotal,20);
System.out.println();

printLeft("Tax",20);
printLeft(""+Tax,20);
System.out.println();

printLeft("Total",20);
printLeft(""+Total,20);
System.out.println();
System.out.println();
System.out.println("*******************************************************************");
System.out.println();
}


double calcInstall() {
area = length * width;
chrgCarpet= area*costPerFt;
chrgLabor= labor* area;
return(chrgCarpet + chrgLabor);
}


double calcSubtotal() {
totalDiscount= customerDiscount/100*Install;
return(Install - totalDiscount);
}


double calcTotal() {
Tax = taxRate/100*Subtotal;
return(Subtotal + Tax);
}

void exit(double input) {
if(input<0||input>5000) {
printLeft("Invalid Input: ",20);
printLeft(""+input,5);
printLeft("won't work...",20);
System.out.println();
System.out.println("Exiting Program");
exit(2);
}

if(input=='?') {
System.out.println("Exiting Program by User Request");
exit(3);
}
}

void exit2(double input) {
if(input<0||input>100) {
printLeft("Invalid Input: ",20);
printLeft(""+input,5);
printLeft("won't work...",20);
System.out.println();
System.out.println("Exiting Program");
}
if(input=='?') {
System.out.println("Exiting Program by User Request");
exit(3);
}
}

public static void main(String s[]) {
Carpet c=new Carpet();
c.getData();
c.calculate();
c.printResult();
System.out.println("Exiting Program...");


}

public double getDouble() {
return Double.parseDouble(readLine());
}

public String readLine() {
String str = "";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));



str = in.readLine();



} catch (IOException e) {
}
return str;

}
public void printLeft(String str,int n) {
for(int i=0;i<n;i++) {
System.out.print(" ");
}
System.out.print(str);
}
public void printLeft(String str) {
printLeft(str,0);

}



String customerName;
double length, width, area, customerDiscount, costPerFt;
double labor=0.35, taxRate=8.25, Install;
double chrgCarpet, chrgLabor, totalDiscount, Subtotal, Tax, Total;

}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455795
That code is working perfectly at my end
0
 

Author Comment

by:bjw5d6
ID: 12455797
Could I be getting these errors because I'm using the FREE version of Java,
Java JDK v1.0.2?
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455807
bjw5d6,
Well, Java is free. You should be using at least 1.4.2. His program will compile. But the fun begins there, heh.
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455809
bjw5d6,
I'll go ahead and make a prediction that someone will repeat that you should be using at least 1.4.2, heh.
0
 

Author Comment

by:bjw5d6
ID: 12455812
Alright... I guess I'll start downloading another Java...I didn't know all Java was free... thank goodness it is...
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455817
Oh Ok

I never noticed that you are using JDK1.0.2. No doubt you will face problems with that Version. And that is too older and have lots of bugs in that version

Download latest from

http://java.sun.com/j2se/1.4.2/download.html and it is free


0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12455821
0
 

Author Comment

by:bjw5d6
ID: 12455833
I believe you that the code works... I'll figure it out later.  Thanks for your help!
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 12455857
My prediction came true! I don't know what fully tested means. In my world it means that the program has been tested with arbitrary input to ensure it will not blow up ... That's great though!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question