Since I'm rather new to Java, I am not familiar with all the ways of handling this type of an assignment. I studied C and C# throughout my high school but I just started Java few days ago.
The task goes like this:
Write a program that reads 10 single-digit integers and displays a string consisting of 10 characters using the coding scheme: Digit Corresponding Character 0-a 1-b 2-c … … 9-j For example, if input consists of the 10 digits 1 8 6 1 0 3 1 8 5 5, the application responds with "bigbadbiff."
Since I moved from C and C# at first I had trouble allowing user to input the integers of the array, but I got that figured out. And this is what I got so far.
import java.util.*;
public class UnsualCoding
{
public static void main (String[] args)
{
Scanner util = new Scanner(System.in);
int[] x = new int[5];
int counter = 0;
for(int i = 0; i < x.length && util.hasNextInt(); i++)
{
x[i] = util.nextInt();
counter++;
}
System.out.println("\n\n");
System.out.println("Our array looks like");
for(int i = 0; i < x.length; i++)
{
System.out.println(x[i]);
}
System.out.println("\n\n");
}
}
Should I create a list, copy the content from an array to it and do the change or should I approach this problem differently? Can I convert the array to string? Also how can I bind/assign the characters to the given numbers?
Thank you.
Since there are only 10 possibilities, you could put the characters that you want to encode into an array, and use the int inputs as the index to that array.
ReplyDeletechar[] y = new char[]{'a','b','c','d','e','f','g','h','i','j'};
Then the only part of your code that would change would be where you print it out :
System.out.println(y[x[i]]);
Though you may want to change your x array to be of arbitrary length as well.
You can do it like this:
ReplyDeleteScanner input = new Scanner(System.in);
int count = 10;
StringBuilder result = new StringBuilder(count);
for(int i = 0; i < count; i++) result.append((char)(input.nextInt() + 'a'));
System.out.println(result);
INPUT:
1 8 6 1 0 3 1 8 5 5
OUTPUT:
bigbadbiff
You can Map each numeric value to a letter. Then you pull from the Map for each number found in the input string. You will need a String to keep track of all the retrieved characters. After that you can return the String that was created.
ReplyDeleteFirst of all - you need some kind of mapping between digits and characters representing them. When you need mapping best to have is a Map!
ReplyDeleteMap <Integer, Character> digitToCharacter = new HashMap<Integer, Character>();
char baseCharacter = 'a';
for (int i = 0; i< 10 ; i++) {
digitToCharacter.put(i, baseCharacter++);
}
System.out.println("--" + digitToCharacter.get(1));
during your printing of content:
for(int i = 0; i < x.length; i++)
{
System.out.println(x[i]);
}
you would simply need to get value from map:
for(int i = 0; i < x.length; i++)
{
System.out.println( digitToCharacter.get(i)));
}
but because you need a String, you don't have to write values to output - you need to append them to single string.
String result = null;
for(int i = 0; i < x.length; i++)
{
result += digitToCharacter.get(i)); // that isn't performace optimal, but it's enough and good for begginer
}
Should I create a list, copy the content from an array to it and do the change or should I approach this problem differently?
ReplyDeleteYou could use a list to accumulate the result, or you could print the result as you accumulate it (you can use System.out.print(ch) to print an individual character).
Also how can I bind/assign the characters to the given numbers?
You could declare:
char[] symbols = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
Then, symbols[d] is the symbol for digit d.
Or you might use that in unicode, lowercase letters occupy a contiguous range of character codes in alphabetical order, and use arithmetic to compute the codepoint of the next character: char symbol = (char) ('a' + digit);