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 5 of 5

Thread: Using CharAt statement

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Using CharAt statement

    import java.util.Scanner;
    import java.text.DecimalFormat;
     
    public class Proj3
    {
     
    	public static void main(String[] args)
    	{
    		Scanner kb = new Scanner(System.in);
    		String strCustomerName;
    		String strPhoneNumber;
    		String strShippingCode;
    		String strTaxCode;
    		char cShippingCode;
    		char cTaxCode;
     
     
    		int iDiscountCode;
    		int iNumberOfGadgets;
    		int iNumberOfWidgets;
     
    		DecimalFormat df1 = new DecimalFormat("###-####");
    		CustomerOrder order1 = new CustomerOrder();
     
    		System.out.print("What is the name of your customer? Must be four letters long, if it is not four letters long please add X's to the end to correct this");
    		strCustomerName = kb.nextLine();
    		order1.setCustomerName(strCustomerName);
     
    		System.out.print("What is the phone number of the customer? Please input in the format ###-####:");
    		strPhoneNumber = kb.nextLine();
    		order1.setPhoneNum(strPhoneNumber);
     
    		System.out.print("How many gadgets does your customer want to order?");
    		iNumberOfGadgets = kb.nextInt();
    		order1.setNumGadgets(iNumberOfGadgets);
     
    		System.out.print("How many widgets does your customer want to order?");
    		iNumberOfWidgets = kb.nextInt();
    		order1.setNumWidgets(iNumberOfWidgets);
     
    		System.out.print("What shipping option would your customer prefer?");
    		System.out.println("For overnight shipping: O or o. For priority shipping: P or p. For standard shipping, s or S.");
    		strShippingCode = kb.nextLine();
    		cShippingCode = strShippingCode.charAt(0);
    		order1.setShippingCode(cShippingCode);
     
     
    		System.out.print("Which tax code would your customer prefer?");
    		System.out.println("For non taxable: N or n. For taxable: T or t.");
    		strTaxCode = kb.nextLine();
    		cTaxCode = strTaxCode.charAt(0);
    		order1.setTaxCode(cTaxCode);
     
    		System.out.print("Which discount code would your customer prefer?");
    		System.out.println("For no discount: 0. For loyalty discount: 1. For employee discount: 2.");
    		iDiscountCode = kb.nextInt();
    		order1.setDiscountCode(iDiscountCode);
     
     
     
     
     
     
    	}//end of main
    }//end of Proj3

    In class, we are calling private methods from a class. The shipping and tax codes have to be set to a character. I would appreciate help on how to convert a string to a character. Foe example, when asked which tax code the customer would prefer, if they choose N or n it would give non taxable, if they choose T or t, it would give taxable, otherwise it would give taxable. However, when it gets to line 44, where you have to input the shipping code, it crashes with the error "String index out of range: 0". Any help would be appreciate, thanks again.


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Using CharAt statement

    First you cannot covert a String to a char. The charAt() method takes a parameter that is the index of the character within the String and returns that
    character so if it is out of range that means the index is out of ranges of the indexes of that String. Example: can the index of n is 2. If can is the literal for a String named str if you tried str.charAt(3) you would get the same error because 3 is out of range. Also there are no line numbers you say line 44. You should include line numbers or segregate the code.

    Hope it helps

  3. #3
    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: Using CharAt statement

    However, when it gets to line 44, where you have to input the shipping code, it crashes with the error "String index out of range: 0".
    What do you input?
    Remember shipping code is of type char, that restricts it to only get character.

    Also there are no line numbers you say line 44. You should include line numbers or segregate the code.
    Hopefully (S)he is talking about his/her editor lines of code.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Using CharAt statement

    So, um, what command should I use to store the entered information into order1.setShippingCode?

  5. #5
    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: Using CharAt statement

    What do you expect a character variable to store in it?

Similar Threads

  1. not a statement?
    By frozen java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 23rd, 2011, 08:57 PM
  2. For-looping, if-else statements, charAt(), etc. Beginner programming problem
    By ayelleeeecks in forum Loops & Control Statements
    Replies: 11
    Last Post: October 3rd, 2011, 12:54 PM
  3. If statement help
    By Legion of Daughters in forum Loops & Control Statements
    Replies: 12
    Last Post: September 6th, 2011, 08:25 AM
  4. If-Else Statement help
    By SnowCrashed in forum Loops & Control Statements
    Replies: 5
    Last Post: February 9th, 2010, 07:57 PM
  5. what is charAt(0) ?
    By low1988 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 11th, 2009, 10:03 AM