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.
Then the only part of your code that would change would be where you print it out :
Though you may want to change your x array to be of arbitrary length as well.