Bugs Posted July 1, 2013 Posted July 1, 2013 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"); } }
Steve21 Posted July 1, 2013 Posted July 1, 2013 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
Bugs Posted July 1, 2013 Author Posted July 1, 2013 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
Steve21 Posted July 1, 2013 Posted July 1, 2013 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 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: 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
LosOjos Posted July 1, 2013 Posted July 1, 2013 (edited) 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.: 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. Edited July 1, 2013 by LosOjos 1
mac_shinobi Posted July 1, 2013 Posted July 1, 2013 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/
Steve21 Posted July 1, 2013 Posted July 1, 2013 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: 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 1
mac_shinobi Posted July 1, 2013 Posted July 1, 2013 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: 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
Steve21 Posted July 1, 2013 Posted July 1, 2013 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 1
mac_shinobi Posted July 1, 2013 Posted July 1, 2013 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 !!
mikecampbell Posted July 1, 2013 Posted July 1, 2013 The hardest thing about this problem is deciphering the problem from the childrens' story it came with! 1
Bugs Posted July 2, 2013 Author Posted July 2, 2013 Thanks all sorted it, didnt realise it was that easy. I thought there were specialist commands for converting between variable types
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now