Could anyone tell where is the error in the program? There is a compile-time error... When I hit compile it shows error in the 40th line saying that 'r' might not have been initialised.. I can't get that.. Pease could anybody help??

import java.util.Scanner;
public class Customer_Account
{
  public static void main(String args[])
  {
      Scanner in= new Scanner(System.in);
      int dep, n, money=0;
      int r;
      System.out.println("Enter the amount deposited");
      dep= in.nextInt();
      System.out.println("Enter the number of years");
      n= in.nextInt();
      if(dep<2000 && n>=2)
      {
          r=5;
          money=(dep*r*n)/100;
      }
      if(dep>=2000 && dep<6000 && n>=2)
      {
          r=7;
          money=(dep*r*n)/100;
      }
      if(dep>6000 && n>=1)
      {
          r=8;
          money=(dep*r*n)/100;
      }
      if(n>=5)
      {
          r=10;
          money=(dep*r*n)/100;
      }
      if((dep<2000 && n>=2)&&(dep>=2000)&&(dep<6000 && n>=2)&&(dep>6000 && n>=1)&&(n>=5))
      {
          r=3;
          money=(dep*r*n)/100;
      }
      System.out.println("Money deposited= Rs."+ dep);
      System.out.println("Time Period="+" " +n+"years");
      System.out.println("Rate Of Interest="+ r + "%");
      System.out.println("Amount In Account= Rs."+ money);
    }

It would be great if I get the help before monday the 26th coz my computer exam is on that day..So obviously I need it before that!!!
Thanks, Ordella.

Dani AI

Generated

The immediate compile-time complaint was handled in earlier replies from and . Below are practical corrections and improvements that go beyond the one-line fix, so the program behaves correctly for all inputs and is easier to test before your exam.

Main problems to fix

  • Several standalone if blocks let more than one branch run and overwrite the rate. Use an if / else if / else chain so exactly one rate is chosen.
  • The long combined condition in the last if is contradictory (it requires mutually exclusive deposit ranges) and is effectively unreachable. If you meant a default case use a plain else.
  • Use a numeric type that can represent fractional interest (e.g., double), compute interest separately, and show the final balance (deposit + interest). Validate inputs (nonnegative deposit, positive years) and decide which rule has precedence (for example, years >= 5 usually overrides deposit-based rules).

Corrected structure (example)

import java.util.Scanner;

public class CustomerAccount {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    double deposit = in.nextDouble();
    int years = in.nextInt();
    double rate;

    if (years >= 5) {
      rate = 10.0;
    } else if (deposit > 6000 && years >= 1) {
      rate = 8.0;
    } else if (deposit >= 2000 && years >= 2) {
      rate = 7.0;
    } else if (deposit < 2000 && years >= 2) {
      rate = 5.0;
    } else {
      rate = 3.0; // default or no-interest case
    }

    double interest = deposit * rate * years / 100.0;
    double balance = deposit + interest;
    System.out.printf("Deposit: Rs %.2f%nYears: %d%nRate: %.1f%%%nInterest: Rs %.2f%nBalance: Rs %.2f%n",
                      deposit, years, rate, interest, balance);
    in.close();
  }
}

Quick test ideas and tips

  • Try edge cases: deposit = 2000, deposit = 6000, years = 1, years = 2, years = 5 to confirm the intended precedence.
  • Add input checks and clear prompts before the scanner reads.
  • Use constants for rates and unit tests (small main/test harness) to verify behavior.
    For practice problems before the exam, follow ’s suggestion to solve small, well-scoped exercises and write tests for each case.

Recommended Answers

All 7 Replies

So change "int r;" to:
int r = 0;

Assign r a value and make the compiler happy. The compiler wants you to assign initial values to local variables (inside methods)

Thank you so so so so much for pointing out such a silly mistake of mine!!!!
And finally NormR1 my compiler is extremely happy now.. Thank You so much!!!
Regards, Ordella :)

As I mentioned in an earlier post of mine that I have my computer exam on th 26th March 2012.. So I was just wondering if anyone could provide me with some programming questions for my school level which I could practice on my own.
If anyone is posting me practice questions then I request do not forget that am just in class Tenth.. Please do not give something whish is sbove my level.
Thanks, Ordella

Well, there are pleny of open questions on DaniWeb.
If you attempt to answer them, you could practice your "chops".
It really depends on the scope of the questions, too.

You might find some easy enough problems in Rosetta code You can do your own solution and see the previous Java answer, and compare them.

Thank You Sir pyTony. I will make it a point to try these.
Regards, Ordella

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.