-3

I cant figure out where to go with my code, also in my if loop it doesn't let me enter more company names after the first loop.

Assign4:

import java.util.Scanner;

public class Assign4 {

    private static int invoice;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        OurDate date = new OurDate();
        Invoice inv = new Invoice();
        Ledger led = new Ledger();

        System.out.print("Enter the amount of monthly invoices: ");
        invoice = scan.nextInt();

        for(int i = 0; i < invoice; i++){

            System.out.println("\nEnter info for invoice number " + i);
            inv.setCompanyNameFromUser();
            inv.toString();
            inv.setBillAmountFromUser();
            inv.setDateFromUser();
            date.setDayFromUser();
            date.setMonthFromUser();
            date.setYearFromUser(); 
        }
    }
}

OurDate:

import java.util.Scanner;

public class OurDate {

    private int day;
    private int month;
    private int year;
    Scanner scan = new Scanner(System.in);

    public OurDate(){
        day = 0;
        month = 0;
        year = 1900;
    }
    public void setDayFromUser(){
        System.out.print("Enter day : ");
        day = scan.nextInt();
    }
    public void setMonthFromUser(){
        System.out.print("Enter month : ");
        month = scan.nextInt();
    }
    public void setYearFromUser(){
        System.out.print("Enter year : ");
        year = scan.nextInt();
    }
    public String toString(){
        return null;

    }
}

Invoice:

import java.util.Scanner;

public class Invoice {

    private double billAmount;
    private String name;
    private OurDate dueDate;
    Scanner scan = new Scanner(System.in);

    public Invoice(){
        billAmount = 0;
    }
    public void setDateFromUser(){
        System.out.print("Enter invoice due date\n");
        //dueDate = scan.nextInt();
    }
    public void setCompanyNameFromUser(){
        System.out.print("Enter the company name : ");
        name = scan.nextLine();
    }
    public void setBillAmountFromUser(){
        System.out.print("Enter bill amount : ");
        billAmount = scan.nextDouble();
    }
    public String toString(){
        return name;

    }

}

Ledger:

import java.util.Scanner;

public class Ledger {

    private Invoice [] invoices;
    private int numInvoices;
    Scanner scan = new Scanner(System.in);

    public Ledger(){

    }
    /*public Ledger(int){

    }*/
    public void getInvoiceInfo(){
        System.out.println("Enter info for the invoice number 0\n");
    }
    public void printInvoiceInfo(){

    }
    public double calculateMonthBills(){
        return numInvoices;

    }   
}
Gary
  • 11,083
  • 14
  • 43
  • 68
  • 4
    What is an "if loop" ? – Manos Nikolaidis Dec 03 '15 at 16:34
  • Maybe a case of http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods –  Dec 03 '15 at 16:38
  • 1
    You should explain which specific step your code is failing on. Then you should describe in detail how you tried to fix that problem. You should then describe what didn't work about that solution. You should then ask a specific and answerable question. – takendarkk Dec 03 '15 at 16:45

1 Answers1

1

As Manos Nikolaidis mentioned link indicated, you need to call #nextLine() to skip the \n after the #readInt() on the Scanner object contained in the Invoice object.

The modified code below should fix your issue.

import java.util.Scanner;

public class Assign4 {

    private static int invoice;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        OurDate date = new OurDate();
        Invoice inv = new Invoice();
        Ledger led = new Ledger();

        System.out.print("Enter the amount of monthly invoices: ");
        invoice = scan.nextInt();

        for(int i = 0; i < invoice; i++) {

            System.out.println("\nEnter info for invoice number " + i);
            inv.setCompanyNameFromUser();
            inv.toString();
            inv.setBillAmountFromUser();
            inv.setDateFromUser();
            date.setDayFromUser();
            date.setMonthFromUser();
            date.setYearFromUser();
            inv.scan.nextLine(); // Need this line so \n is skipped
        }
    }
}
ARau
  • 489
  • 2
  • 9