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 3 of 3

Thread: Var Might have been initialize

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Var Might have been initialize

    Hello, Soo my java coding has this 2 ways of paymentterm that is cash and credit.
    the problem is the category of the File that is being read would be on the Payment Term.
    and the payment term would have cash and credit. So what i'm trying to say is that I want the cash and credit to be written in a different output because one of them has a discount.

    so I use IF Statement.. which is If ( PaymentTerm == CREDIT )

    but apparently the error said Variable Credit might not have been initialize
    What should I actually should put?
    ps. Im using BlueJ
    import java.util.*;
    import java.io.*;
     
    public class Assignment2
    {
    public static void main(String[]args ) throws FileNotFoundException
    {
        String[] A= {"Aaron Technology Sdn Bhd", "Bagunan Aziz dan Anak-anak"//0 1
                    , "Blok'A', Tingkat 1, No.4","Jalan Adam, Simpang 234",// 2 3
                    "AST-Beyond, Kuala Hawa", "Brunie CH134", "Receipt No",// 4 5 6
                      "Customer Name :", "Payment Term :", "Item No. : ",// 7 8 9
                 "-----------------------------"," ","Total Include GST of 5% :", //10 11 12
                "Total Amount", // 13
            "------------------------------------Cut Here ---------------------------------", // 14
            "Discount Rate : 10%"}; //15
         String PaymentTerm , PaymentTerm2, CustomerName, ItemNo , ItemName , CREDIT, CASH  ;
     
        double x=0.00,Quantity,Price = 1 , Tax =0.05 , ReceiptNo ;            
     
       Scanner readFile = new Scanner(new FileReader("C:\\Users\\user\\Documents\\DIT MKJB\\Programming Java\\customer.txt"));
     
       PrintWriter writeFile = new PrintWriter("C:\\Users\\user\\Desktop\\receipts.txt");
     
    while(readFile.hasNext())
        {
    //Categories being read in Customer.txt file
        ReceiptNo = readFile.nextDouble();
        PaymentTerm = readFile.next();
        CustomerName = readFile.next();
        ItemNo = readFile.next();
        ItemName = readFile.next();
        Quantity = readFile.nextDouble();
        Price = readFile.nextDouble();
     
     
          writeFile.printf("%53s %n%54s %n%53s %n%52s %n%52s %n%46s%n",A[0],A[1],A[2],A[3],A[4],A[5]);
     
          if( PaymentTerm == CREDIT  )
         {
           writeFile.println(); // RECEIPT NO
           writeFile.printf("%43s %s %n%55s %n%41s %s %n%40s %s %n%37s %n%37s %s %n%55s %n"// Discount Displayed
                           ,A[6],ReceiptNo,A[10],A[7],CustomerName,A[8],PaymentTerm,A[15],A[9],ItemNo,A[10]);
                           x = Quantity * Price;
     
           writeFile.printf("%26s%.0f x %.2f %10s $%.2f %n%55s %n%38s "
                           ,A[11],Quantity,Price,A[11],x,A[10],A[13]);
                           x = x + (x * Tax);
           writeFile.printf("%n%51s $%.2f %n%n%s",A[12],x,A[14]);                  
           writeFile.println();
        }
           if ( PaymentTerm == CASH )
          { writeFile.println(); // RECEIPT NO
            writeFile.printf("%43s %s %n%55s %n%41s %s %n%40s %s %n%37s %s %n%55s %n"// Do not have discount displayed
                           ,A[6],ReceiptNo,A[10],A[7],CustomerName,A[8],PaymentTerm,A[9],ItemNo,A[10]);
                           x = Quantity * Price;
     
            writeFile.printf("%26s%.0f x %.2f %10s $%.2f %n%55s %n%38s "
                           ,A[11],Quantity,Price,A[11],x,A[10],A[13]);
                           x = x + (x * Tax);
            writeFile.printf("%n%51s $%.2f %n%n%s",A[12],x,A[14]);              
            writeFile.println();
        }
       }


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Var Might have been initialize

    Hello Evilreaper!

    Quote Originally Posted by Evilreaper View Post
    so I use IF Statement.. which is If ( PaymentTerm == CREDIT )
    but apparently the error said Variable Credit might not have been initialize
    What should I actually should put?
    The error message is pretty clear. You should initialize that variable. And another think I noticed:
    if( PaymentTerm == CREDIT  )
    You should use the String's equals method instead of the == operator when you want to compare the contents of Strings.

    Hope this helps.

  3. The Following User Says Thank You to andreas90 For This Useful Post:

    Evilreaper (December 15th, 2012)

  4. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Var Might have been initialize

    Thanks it helped even though i wasn't taught on how to use it~ decided to lurk around on what u meant.

Similar Threads

  1. [SOLVED] using the return of a method to initialize an ArrayList
    By mia_tech in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 06:05 PM
  2. Initialize Map size
    By mano123 in forum Collections and Generics
    Replies: 2
    Last Post: May 29th, 2012, 01:05 AM
  3. What is sql:query var refers to and what's wrong with the below code ?
    By tangara in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 8th, 2011, 06:24 AM
  4. Re-Initialize array without loosing contents..
    By Mr.777 in forum Java Theory & Questions
    Replies: 7
    Last Post: June 17th, 2011, 06:47 AM
  5. I'm not sure how to initialize GraphicsDevice and Window
    By DotChris in forum AWT / Java Swing
    Replies: 3
    Last Post: July 15th, 2009, 10:00 AM