-1

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.

Lightningzr
  • 73
  • 1
  • 2
  • 5

1 Answers1

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,798
  • 3
  • 29
  • 63