Reputation: 1
In my case 1, I am trying to read enter when user decided to stop storing anymore item. Upon user pressing enter, I would like the program to go back to the menu section. Also I would like to return to the menu each time after a case ends. How can I do it?
import java.text.*;
import java.util.*;
public class MonthlyExpenditure {
static Scanner input= new Scanner(System.in).useDelimiter("\r\n");
static DecimalFormat fmt=new DecimalFormat("0.00");
public static void main(String[] args )
{
int choice, month=0,i=0,j=month ;
String b;
double[] totalAmount= new double[12];
String[][] item= new String [12][11]; //12= column(month), 11=row(item)
double[][] amount= new double[12][11]; //12=column(month), 11=row(amount)
System.out.println("************* Expenditure ***************");
System.out.println("1> Enter monthly expenses");
System.out.println("2> Display detailed expenditure by month");
System.out.println("3> Quick glance at monthly expenses");
System.out.println("4> Exit");
System.out.print("Please select your choice <1-4>");
choice=input.nextInt();
System.out.println("*****************************************");
switch(choice)
{
case 1 :
System.out.print("Enter month <1 for Jan - 12 for Dec>: ");
month=input.nextInt();
b=getMonth(month);
System.out.println(b+" expenditure <max 10 items>");
while(i<10){ //repeat asking user to enter up to 10 item
j=month;
System.out.print("Enter item "+(i+1)+" <Press ENTER to exit> : "); //"while" to only prompt enter item and amount
item[j][i]=input.next();
if(item[j][i]==" ")
System.out.println("meunu");
System.out.print("Enter amount : ");
amount[j][i]=input.nextDouble();
i++;
}
break;
case 2 :
System.out.print("Enter month <1 for Jan - 12 for Dec>: ");
month=input.nextInt();
b=getMonth(month);
j=month;
System.out.println("Expenditure for "+b);
i=0;
while(item[j][i]!=null)
{ System.out.println(item[j][i]+"\t\t\t"+amount[j][i]);
i++;
}
break;
case 3 :
System.out.println("Monthly expenditure :");
System.out.println(" ");
{
b=getMonth(month);
j=month;
totalAmount[j]=amount[j][0]+amount[j][1]+amount[j][2]+amount[j][3]+amount[j][4]+amount[j][5]+amount[j][6]+amount[j][7]+amount[j][8]+amount[j][9];
System.out.print(b+"\t\t$"+totalAmount[j]);
if(totalAmount[j]>2500)
System.out.println("\t\t\t over spent!!");
else
System.out.println(" ");
}
break;
case 4 :
System.out.println("Thank you for using this System");
System.exit(0);
break;
default :
System.out.println("Error. Enter only 1-4");
break;
}while(choice<1||choice>4);
}
public static String getMonth(int b)
{
String month=" ";
switch (b) {
case 1:month="Jan";
break;
case 2:month="Feb";
break;
case 3:month="Mar";
break;
case 4:month="Apr";
break;
case 5:month="May";
break;
case 6:month="Jun";
break;
case 7:month="Jul";
break;
case 8:month="Aug";
break;
case 9:month="Sep";
break;
case 10:month="Oct";
break;
case 11:month="Nov";
break;
case 12:month="Dec";
break;
default: System.out.println("Invalid number. Only 1-12 is recognisable.");
}//end switch
return month;}
Upvotes: 0
Views: 110
Reputation: 4060
A simple solution would just be to wrap everything in a while(true) loop. You can move your menu creation Strings to a static method and just call that method whenever you need to show the menu again.
Something like this should do for the createMenu method, you may want to handle cases in which the user does not input an integer.
private static int createMenu() {
System.out.println("************* Expenditure ***************");
System.out.println("1> Enter monthly expenses");
System.out.println("2> Display detailed expenditure by month");
System.out.println("3> Quick glance at monthly expenses");
System.out.println("4> Exit");
System.out.print("Please select your choice <1-4>: ");
int choice = Integer.parseInt(input.nextLine());
System.out.println("*****************************************");
return choice;
}
Your main method could start out like this:
public static void main(String[] args) {
int choice, month = 0, i = 0, j = month;
String b;
double[] totalAmount = new double[12];
String[][] item = new String[12][11]; //12= column(month), 11=row(item)
double[][] amount = new double[12][11]; //12=column(month), 11=row(amount)
while (true) {
choice = createMenu();
switch (choice) {
// etc
When the user hits the return key, you could just break out of the inner while loop:
if (item[j][i].isEmpty()) {
break;
}
Upvotes: 0
Reputation: 1615
If you want to allow the user to cancel at any point inside the operations in your switch statement you'll need to first read in their input using Scanner#nextLine()
and then test if that line is equal to an empty-string. If it is, break out.
String line = input.nextLine();
if (line.equals(""))
{
break; // Go to next-line after inner-most loop/switch structure
}
// Code for basic course of events
And to return the user to the menu again you could wrap the relevant parts your code (ie: from where you print your menu up to the end of your switch-statement) in a loop of some sort like so.
do {
// Your code to print out the menu and perform operations here
} while (choice != 4); // Replace 4 with whatever value associated with an exit command
Upvotes: 0
Reputation: 1332
Don't use nextInt
in such situation, but nextLine()
, and then parse using Integer.parseInt()
.
Upvotes: 1