I'm trying to create a single array that contains the numbers 1-9 and the characters A-F. The array should look like: 1 2 3 4 5 6 7 8 9 A B C D E F. I don't know how to set the array up and would appreciate any advice.
Asked
Active
Viewed 5,390 times
-1
-
How will this array be worked with? – Evan Knowles Jul 24 '14 at 04:54
-
3The numbers 1-9, or the characters 1-9? – user253751 Jul 24 '14 at 04:56
-
1An array can contain only a single type of value – MadProgrammer Jul 24 '14 at 04:58
-
It will use the numbers 1-9 as the program will try to use the array and help convert decimal numbers into a different base number, EX:base 16 – Lightningzr Jul 24 '14 at 06:01
1 Answers
1
Try this following piece of code,This will work for you,However you must keep in mind that An array can contain only a single type of value
as said by @MadProgrammer in comments
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char[] arr = new char[16];
/*taking input from the keyboard*/
for(int i = 0; i < 16; i++)
{
arr[i] = sc.next().charAt(0);
}
/*displaying the contents of the array*/
for(int i = 0; i < 16; i++)
{
System.out.println(arr[i] + ",");
}
}
}
In this code the numbers 0-9,which you are taking as input are still inserted as characters.

nobalG
- 4,544
- 3
- 34
- 72