Seperating String with comma
Hello,
thanks in advance for the help.
Anyways, this program prints out a list of items form an array and allows the user to select from the list and store the item. It also saves the total of each item selected and the total the total items selected. At the end the program will print the list of items the user selected. This all works fine. The issue an "and" to the last item. For instance the user selects a few items. My program prints: (1) hot dog, (1) Ice Cream, (3) Sodas. I need it to print: (1) hot dog,
(1) Ice Cream, and (3) Sodas. (along with the period). I am not allowed to use the "delim()" or append() methods because we have not covered that in class. Any help or ideas so I can make this work would be great. I just can not seem to get the logic down. Thanks again.
Code :
import java.util.Scanner;
import java.util.*;
import java.text.*;
public class Regist {
public static void main (String [] args) {
Scanner input = new Scanner(System.in);
DecimalFormat format = new DecimalFormat("$0.00");
String[] myMenu = {"1. Hot Dog", "2. Hamburger", "3. Soda", "4. Chips", "5. Ice Cream", "6. Shave Ice", "7. Cookie", "8. Plate Lunch", "9. French Fries", "10. Shake", "11. Order Complete"};
String[] pluralMenu = {"Hot Dogs", "Hamburgers", "Sodas", "Chips", "Ice Creams", "Shave Ice", "Cookies", "Plate Lunches", "French Fries", "Shakes"};
String[] selectedItems = new String[10];
double[] price = {2.50, 3.00, 1.25, 1.50, 2.50, 2.00, 1.00, 5.00, 2.50, 3.00};
int[] itemNumber = new int[10];
int totalItems = 0;
double prices = 0;
int decision;
String choice = "";
do {
for (String a: myMenu) {
System.out.println(a);
}
System.out.print("Please select your choice: ");
decision = input.nextInt() - 1;
if (decision != 10) {
if (itemNumber[decision] < 1 ) {
totalItems++;
}
itemNumber[decision] = itemNumber[decision] + 1;
prices = prices + price[decision];
if( itemNumber[decision] == 1) {
selectedItems[decision] = myMenu[decision].substring(2);
} else if (itemNumber [decision] > 1) {
selectedItems[decision] = pluralMenu[decision];
}
}
} while (decision != 10);
for (int i = 0; i != 10; i++) {
if (selectedItems[i] != null) {
choice = choice + " (" + itemNumber[i] + ")" + selectedItems[i];
}
}
System.out.println ("Your order contains: " + choice);
System.out.println ("Your order price is: " + format.format(prices));
}
Re: Seperating String with comma
Get the length of the data structure where you have store the values. When you are one less than the total length, print a comma and "and" and then print the value.