Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Seperating String with comma

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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.

    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));
    	}
    Last edited by javafriend; March 12th, 2012 at 03:58 AM. Reason: fixed part of issue.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  3. The Following User Says Thank You to Mr.777 For This Useful Post:

    javafriend (March 12th, 2012)

Similar Threads

  1. Import Comma Delimited File to 2D Array
    By kigroy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 14th, 2012, 08:54 PM
  2. [SOLVED] Seperating Strings
    By beer-in-box in forum What's Wrong With My Code?
    Replies: 15
    Last Post: September 14th, 2011, 02:37 PM
  3. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  4. How to remove comma before an String?
    By bhba73 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 28th, 2011, 08:51 PM
  5. How to remove the last comma
    By fride360 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 07:20 AM