+ Post New Thread
Results 1 to 12 of 12
Coding Thread, Java problem in Coding and Web Development; I`m doing some exercises in java and i`m really stuck. The problems is with converting characters to integers. Iv pasted ...
  1. #1

    Join Date
    Nov 2008
    Location
    Derby, UK
    Posts
    169
    Thank Post
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    13

    Java problem

    I`m doing some exercises in java and i`m really stuck. The problems is with converting characters to integers. Iv pasted the problem below, I dont want the answer just pointed in the right direction.

    Problem:
    You and your Martian friend go hiking up Mount Vesuvius. While exchanging stories at the campfire, he tells you about this grocer on Mars. He says, "Ze is the best grocer in this galaxy, but you have to respect ze wishes." It seems Ze sells only two items - Zomatoes, and Zinions. But that's not all. Ze will not sell you an arbitrary number of items. The minimum order is 65 and the maximum order is 122. What's more, you cannot ask for 65 Zomatoes. You have to ask for 'A' Zomatoes. Similarly, you cannot ask for 122 Zinions. You have to ask for 'z' Zinions. Confused ? The ASCII value of (uppercase) 'A' is 65 and the ASCII value of (lowercase) 'z' is 122.
    In this exercise you have to add the ASCII values of 2 char variables.

    What we expect:
    In this exercise, Ze will ask you for the number of Zomatoes and Zinions you want to buy. You have to enter an ASCII character in the range [A-Za-z] for both. The number of Zomatoes you want to buy will be given to you in a variable called 'zomatoes', which is of type 'char'. The number of 'Zinions' you want to buy will be given to you in a variable called 'zinions', which is also of type char. You have to add the ASCII values of both the char variables (zomatoes and zinions) and populate the variable called 'items', which is of type 'int', with the sum. For example if you asked for 'a' Zomatoes and 'C' Zinions, the total number of items is 'a'(97) + 'C'(67) = 164

    import java.util.Scanner;

    public class CrazyConverter {

    public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Hello I am your friendly grocer Darth: ");
    System.out.println("How many zomatoes do you want ? (Enter a character in the range [A-Za-z]): ");
    String sZomatoes = scanner.nextLine();
    System.out.println("How many zinions do you want ? (Enter a character in the range [A-Za-z]): ");
    String sZinions = scanner.nextLine();

    char zomatoes = sZomatoes.charAt(0);
    char zinions = sZinions.charAt(0);
    int items = 0;
    ///{

    //start your coding here


    //end


    ///}

    System.out.println("Thank you ! you have asked for " + items + " items");
    }
    }

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    Err well what bit is it you're stuck with?

    To convert char to int there's some 1-liner code you can use. (Not sure how else you want help without some code )

    Steve

  3. #3

    Join Date
    Nov 2008
    Location
    Derby, UK
    Posts
    169
    Thank Post
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    13
    It asks you for 2 characters (which are ASCII) and stores them in a 2 variables it wants me to add them both together and store the answer in another variable.

    its the converting the ASCII to int and adding them together im stuck on

    iv googled but not having much luck

  4. #4

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    Quote Originally Posted by Bugs View Post
    It asks you for 2 characters (which are ASCII) and stores them in a 2 variables it wants me to add them both together and store the answer in another variable.

    iv googled but not having much luck
    I guess my question was what's the bit you're stuck on

    Code:
    char zomatoes = sZomatoes.charAt(0); 
    char zinions = sZinions.charAt(0);
    Is your two characters you've already brought in.

    You can do a 1-liner to convert char to int.

    Then just add the two ints together to make a final answer and output it.

    Example in duff code:

    Code:
    char zomatoes = sZomatoes.charAt(0); 
    char zinions = sZinions.charAt(0); 
    int items = 0; 
    
    int Number1 = ConvertToInt(zomatoes);
    int Number2 = ConvertToInt(zinions);
    
    items = Number1 + Number2
    Steve

  5. #5

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,771
    Thank Post
    1,511
    Thanked 1,294 Times in 884 Posts
    Rep Power
    813
    It just so happens [read: it was obviously the idea ] that the given values of 'A' and 'z' are the ASCII codes for those characters.

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    Therefore, to find the value of a character, just cast the character to int in Java e.g.:

    Code:
    To convert a single character to int (i.e. it's ASCII code):
    (int) ascii = (int) character;
    
    To convert an int back to a char:
    (char) character = (char) ascii;
    So, assuming the character read from the CLI is the letter 'Q' and that the letter is stored in the char variable character, after casting character to int as above the int variable ascii would contain the value 81.
    Last edited by LosOjos; 1st July 2013 at 05:35 PM.

  6. Thanks to LosOjos from:

    mac_shinobi (1st July 2013)

  7. #6

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,053
    Thank Post
    3,583
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377
    Quote Originally Posted by Steve21 View Post
    Err well what bit is it you're stuck with?

    To convert char to int there's some 1-liner code you can use. (Not sure how else you want help without some code )

    Steve
    What you're talking about @Steve21 from what I can gather is data type casting ( conversion between an alpha numeric and a numeric ) or visa versa type value so that you can manipulate the data ie to do a formula or use it for a mathmatical equation / calculation

    If Im understanding what has been explained correctly it sounds like they have a form or two prompts which requests two inputs of an alpha numeric value on both occasions ( A-Z or a-z ) for one item and the same for the 2nd prompt

    Each of the inputted characters then needs to be converted into its ascii equivalent number which then gets added together so if I entered A in the first prompt and a in the 2nd prompt

    A in Decimal would be 65
    a in decimal would be 97

    So you would add the two ascii numerical equivalents together which would be

    65 + 97 = 162

    Assuming ive got the correct ascii codes for each of the letters ( A ) and ( a )

    http://www.asciitable.com/

  8. #7

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    Quote Originally Posted by mac_shinobi View Post
    What you're talking about @Steve21 from what I can gather is data type casting ( conversion between an alpha numeric and a numeric ) or visa versa type value so that you can manipulate the data ie to do a formula or use it for a mathmatical equation / calculation

    If Im understanding what has been explained correctly it sounds like they have a form or two prompts which requests two inputs of an alpha numeric value on both occasions ( A-Z or a-z ) for one item and the same for the 2nd prompt

    Each of the inputted characters then needs to be converted into its ascii equivalent number which then gets added together so if I entered A in the first prompt and a in the 2nd prompt

    A in Decimal would be 65
    a in decimal would be 97

    So you would add the two ascii numerical equivalents together which would be

    65 + 97 = 162

    Assuming ive got the correct ascii codes for each of the letters ( A ) and ( a )

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion
    Not sure what you're saying/asking? Doing a direct will give you the ascii.

    Convert a to an int and it's the same as ascii value. Same with any of the others.

    Spolier code:
    Code:
    package test1;
    
    import java.util.Scanner;
    
    public class test {
    	
    	public static void main(String[] args)
    	{
    		
    			Scanner scanner = new Scanner(System.in); 
    			System.out.println("Hello I am your friendly grocer Darth: "); 
    			System.out.println("How many zomatoes do you want ? (Enter a character in the range [A-Za-z]): "); 
    			String sZomatoes = scanner.nextLine(); 
    			System.out.println("How many zinions do you want ? (Enter a character in the range [A-Za-z]): "); 
    			String sZinions = scanner.nextLine(); 
    
    			char zomatoes = sZomatoes.charAt(0); 
    			char zinions = sZinions.charAt(0); 
    			int items = 0; 
    			int Num1;
    			int Num2;
    			Num1 = (int) zomatoes;
    			Num2 = (int) zinions; 
    			items = Num1 + Num2;
    			System.out.println("Thank you ! you have asked for " + items + " items"); 
    	} 
    
    
    }
    Would give A + Z (65 + 90) = 155 or:

    Hello I am your friendly grocer Darth:
    How many zomatoes do you want ? (Enter a character in the range [A-Za-z]):
    A
    How many zinions do you want ? (Enter a character in the range [A-Za-z]):
    Z
    Thank you ! you have asked for 155 items
    Steve

  9. Thanks to Steve21 from:

    mac_shinobi (1st July 2013)

  10. #8

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,053
    Thank Post
    3,583
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377
    Quote Originally Posted by Steve21 View Post
    Not sure what you're saying/asking? Doing a direct will give you the ascii.

    Convert a to an int and it's the same as ascii value. Same with any of the others.

    Spolier code:
    Code:
    package test1;
    
    import java.util.Scanner;
    
    public class test {
    
    public static void main(String[] args)
    {
    
    Scanner scanner = new Scanner(System.in); 
    System.out.println("Hello I am your friendly grocer Darth: "); 
    System.out.println("How many zomatoes do you want ? (Enter a character in the range [A-Za-z]): "); 
    String sZomatoes = scanner.nextLine(); 
    System.out.println("How many zinions do you want ? (Enter a character in the range [A-Za-z]): "); 
    String sZinions = scanner.nextLine(); 
    
    char zomatoes = sZomatoes.charAt(0); 
    char zinions = sZinions.charAt(0); 
    int items = 0; 
    int Num1;
    int Num2;
    Num1 = (int) zomatoes;
    Num2 = (int) zinions; 
    items = Num1 + Num2;
    System.out.println("Thank you ! you have asked for " + items + " items"); 
    } 
    
    
    }
    Would give A + Z (65 + 90) = 155 or:



    Steve
    Not asking anything. Was trying to explain what they were after and think you've given them the answer and help they wanted

  11. #9

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,824
    Thank Post
    372
    Thanked 544 Times in 507 Posts
    Rep Power
    184
    Quote Originally Posted by mac_shinobi View Post
    Not asking anything. Was trying to explain what they were after and think you've given them the answer and help they wanted
    Ah, my bad! Thought there was a question in there that I wasn't getting Harr. Mondays are evil I tell you!

    Steve

  12. Thanks to Steve21 from:

    mac_shinobi (1st July 2013)

  13. #10

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    10,053
    Thank Post
    3,583
    Thanked 1,123 Times in 1,025 Posts
    Rep Power
    377
    Quote Originally Posted by Steve21 View Post
    Ah, my bad! Thought there was a question in there that I wasn't getting Harr. Mondays are evil I tell you!

    Steve
    Defo evil !!

  14. #11

    Join Date
    Dec 2012
    Location
    Skipton, North Yorkshire
    Posts
    30
    Thank Post
    12
    Thanked 4 Times in 4 Posts
    Rep Power
    16
    The hardest thing about this problem is deciphering the problem from the childrens' story it came with!

  15. Thanks to mikecampbell from:

    mac_shinobi (1st July 2013)

  16. #12

    Join Date
    Nov 2008
    Location
    Derby, UK
    Posts
    169
    Thank Post
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    13
    Thanks all sorted it, didnt realise it was that easy. I thought there were specialist commands for converting between variable types



SHARE:
+ Post New Thread

Similar Threads

  1. Sam Learning & Java Problem
    By Kyle in forum Educational Software
    Replies: 12
    Last Post: 23rd May 2008, 01:30 PM
  2. Java problem with Runescape server
    By Warren-Plus in forum Gaming
    Replies: 6
    Last Post: 23rd February 2008, 08:44 PM
  3. Java problems in terminal sessions
    By nutso in forum Thin Client and Virtual Machines
    Replies: 0
    Last Post: 19th November 2007, 10:00 AM
  4. Java Problems
    By Ste_Harve in forum Windows
    Replies: 2
    Last Post: 23rd March 2007, 02:22 PM
  5. Java problem - Password Needed
    By mullet_man in forum Windows
    Replies: 5
    Last Post: 5th February 2007, 07:11 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •