开发者

How to display for my code?

开发者 https://www.devze.com 2023-02-10 19:03 出处:网络
My question is how do I display the 2D array which I stored. Specifically, my program needs to display the elements (in somewhat a table) which I stored in them for each month.

My question is how do I display the 2D array which I stored.

  1. Specifically, my program needs to display the elements (in somewhat a table) which I stored in them for each month.
  2. Next, it should sum up all the values stored(do I use another array for that?), and display them in a line, e.g. (System.out.println("Total amount spent for the month of January is.....")

All help is appreciated!

import java.util.*;
import java.text.*;

public class test {
    static Scanner input = new Scanner(System.in).useDelimiter("\r\n");
    static DecimalFormat fmt = new DecimalFormat("0.00");
    static String months[] = { null, "Jan", "Feb", "Mar", "Apr", "May", "Jun",
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; // to store month data
    static String monthItems[][] = new String[13][13];
    static double amount[][] = new double[13][13];
    static int m = 1;

    public static void main(String[] args) {
        try {
            System.out.print("Enter month <1 for Jan - 12 for Dec>:  ");
            m = input.nextInt();
            if (m == 0) {
                System.out.println("----------------------------------------");
                System.out.println("Invalid month!");
             开发者_如何学C   System.out.println();
                return;
            } else {
            }
            System.out.println("----------------------------------------");
            System.out.println(months[m] + " Expenditure <max 10 items>");
            // to store data if January is selected
            MonthData();
        } catch (Exception e) {
            // error message to inform user of wrong input
            System.out.println("Invalid month!");
        }

        try {
            System.out.print("Enter month <1 for Jan - 12 for Dec> :  ");
            m = input.nextInt();
            EntryMonth();
            System.out.println("----------------------------------------");
            System.out.println("Expenditure for " + months[m] + ":");
            // to display monthly data
            DisplayMonthData();
        } catch (Exception e) {
            // when user enter's wrong input
            System.out.println("Invalid month!");
        }
    }

    static void EntryMonth()// to allow user to enter month data
    {
        try {
            if (m < 1 || m > 12) {
                return;
            } else {
            }
        } catch (Exception e) {
            System.out.println();
        }
    }

    static void MonthData() {
        try {
            for (int i = 0; i < 10; i++) {
                System.out.print("Enter item " + (i + 1)
                        + " <Press ENTER to exit> : ");
                monthItems[m][i] = input.next();
                if (monthItems[m][i].length() == 0) {
                    return;
                } else {
                    System.out.print("Enter amount : $");
                    amount[m][i] = input.nextDouble();
                    System.out.println("");
                }
            }
        } catch (Exception e) {
            System.out.println("");
        }
    }

    static void DisplayMonthData() {
        for (int row = 0; row < amount.length; row++) {
            for (int column = 0; column < amount[row].length; column++) {
                System.out.println(monthItems[row][column]);
            }
        }
    }
}


I'm not allowed to comment Joeri Hendrickx' answer, but he is correct. I just tried your code, entered 10 (enter) foo (enter) 5 (enter) bar (enter) 7 (enter) (enter) 10 (enter) and it displayed everything just fine. Of course all but two items are null and all but two amounts are 0.0, but that is just as expected.


There are a lot of ways, but here's something which should already help you.

  static void DisplayMonthData()
  {
    for(int row=0;row<amount.length;row++){
        for(int column=0;column<amount[row].length;column++){
            String monthItem = monthItems[row][column];
            if (monthItem==null)
                System.out.print("\t")
            else
                System.out.print(monthItems[row][column]+"\t");
        }
        System.out.print("\n");
    }
  }

If you want the totals next to each line, refactor out the part that prints one line, and make it sum the values in that line. Then add the end, add a print(total).

Edited to skip null values.

0

精彩评论

暂无评论...
验证码 换一张
取 消