Lana
Lana

Reputation: 55

What data type to use if user is asked to input integers and letters?

I am new in programming. My instructor gave me a special project about number base.

Since the higher number base are a combination of integers and letters, I'm still confused if I should use int and/or char.

import java.util.Scanner;
public class JavaProgram {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the base you want to convert and base to be converted: ");
        int firstBase = in.nextInt();
        int secondBase = in.nextInt();

        switch(firstBase) {
            case 2:
                System.out.print("Enter value: ");
                // I get confused here already
            case 3:
            case 4:
            case 5:
            case 8:
            case 10:
            case 12:
            case 16:
            case 20:
            case 24:
            case 26:
            case 27:
            case 30:
            case 32:
            case 36:    
        }

        in.close();
    }
}

Upvotes: 0

Views: 3737

Answers (2)

user3808887
user3808887

Reputation: 319

Here is a quick overview of some types:

  • char - only a single letter or character
  • int - numbers only without decimals
  • double - numbers with decimals
  • String - both numbers and letters

So based on your requirement you have to select String.

Upvotes: 1

Cardinal System
Cardinal System

Reputation: 3420

If you want to store numbers and letters, than you can use char, since technically letters and numbers are characters. Alternatively you can use String, which can store anything from a single character, to complete paragraphs.

Upvotes: 2

Related Questions