Results 1 to 2 of 2
Like Tree2Likes
  • 2 Post By SethLasky

Thread: Java is ignoring "if" code?

  1. #1
    SethLasky is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Java is ignoring "if" code?

    Hi guys,
    I'm extremely new to Java and programming in general and am taking my first class in college (I am hooked). I wrote the following program for my class but for some reason the "newBalance" is being calculated as if the first condition wasn't met and it is a "Checking" account. The file that I am reading holds 4 different records of a bank account. I'm sorry in advance if this isn't proper protocol for this forum but I am going to put my whole code on here to see if someone can give me any advice on how I can fix this problem, or any advice on how my code could be more effective. Thank you! (And yes I know the .out file isn't being utilized, I'm just trying to test this, the final output of the program will be written onto a .out file.)

    Java Code:
    import java.io.*;
    import java.util.*;
    public class unit8_Lasky
    {
      
      public static void main(String[] args)
      {
    Scanner in = null;
    PrintWriter out = null;
    double accountNumber = 0.0;
    String firstName = null;
    String lastName = null;
    String accountTypeCharacter = null;
    String accountType = null;
    double balance = 0.0;
    double newBalance = 0.0;
    
      try
      {
        
        in= new Scanner(new FileReader("bank.dat"));
        out = new PrintWriter("bank.out");
       
    while (in.hasNext())
    {
      accountNumber = in.nextDouble();
      firstName = in.next();
      lastName = in.next();
      accountTypeCharacter = in.next();
      balance = in.nextDouble();
      
    if (accountTypeCharacter.matches("S"))
    {
      accountType = "Saving";
      if (balance > 500.00)
      {
        newBalance = (1.04 * balance);
      }
      else
        newBalance = (1.04 * (balance - 10.00));
    
    }
    else
      accountType = "Checking";
      if (balance > 25.00)
      {
        if (balance > 5025.00)
        
          newBalance = (1.03 * balance);
        
        else
          newBalance = (1.015 * balance);
      }
      else
        newBalance = (1.015 * (balance - 25.00));
      
        
    
    
      System.out.printf("%s%.0f%n%s%s%n%s%s%n%s%s%n%s%.2f%n%n", "Account Number: ", accountNumber, "First Name: ", firstName, "Last Name: ", lastName, "Account Type: ", accountType, "Balance: $", newBalance);  
       }
    
    
      } 
      catch (FileNotFoundException e) {
        System.err.println("FileNotFoundException: " + e.getMessage());
        
    
    }
      catch (IOException e) {
        System.err.println("Caught IOException: " + e.getMessage());
    }
      
      
    
      }
    }

  2. #2
    SethLasky is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: Java is ignoring "if" code?

    Sorry for wasting your time...I just got an email back from my professor telling me that I didn't include {} on the else statement. I didn't even know there needed to be such things! I love Java!
    awinston and kammce like this.

Similar Threads

  1. Replies: 5
    Last Post: 04-29-2012, 07:18 AM
  2. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 05:40 PM
  3. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •