How to store " " (whitespace) in an `int` array in Java

how to store vegetables
how to store vegetables in kitchen
how to store fruits and vegetables chart
how to store potatoes
how to store carrots
how to store broccoli
how to store onions
how to store celery

I have an int array int a1[];

At some point in my code, if the conditions meet, I want to replace the value of a[i] to whitespace. For example, if my output is:

1 2 3 4 5 6

I want to make it:

1 2   4 5 6

How can I do that?

Edit: An integer array that contains a mixture of integers and spaces is a solution I came up with but it doesn’t seem to work. All I want is a way to get the output I’ve shown, and the purpose is to make my output more appealing. I’m making a memory game and I want to show a blank space when the guess is right so the user can see his progress.

Short answer: You can't store a space(a char) as an Integer, if is that what you're tryint to do. I would change int to the Integer type so you can add null values to the array and if you find a null value I would print a white space instead.

However, you could get the numerical representation of such char, but in this case It changes the data type.

How to Store Vegetables to Keep them Fresh, should be stored in a different part of the fridge than fruit. This will prevent them from ripening too fast. How to Store Fruits and Vegetables | Real Simple. Keep your produce as fresh as possible with these tips for storing fruits and vegetables. Keep your produce as fresh as possible with these guidelines* for storing fruits and vegetables. Keep your produce as fresh as possible with these tips for storing fruits and vegetables.


For your memory game I suggest a model class to hold the number and an indication whether it has been guessed. Something along these lines:

public class GameElement {
    final int number;
    boolean guessed = false;

    public GameElement(int number) {
        this.number = number;

    public void setGuessed() {
        guessed = true;

    public String toString() {
        if (guessed) {
            return " ";
        } else {
            return String.valueOf(number);

You will probably want to put the item to be guessed into the class too so you have everything in one place (or I misunderstood something).

Even nicer, rather than relying on a toString method, decide the presentation completely outside your model class. For this purpose, add getters getNumber and isGuessed so the presentation can access the data and decide how to present it to the user.

Original answer

As has been said in the comments, this is not possible.

You may choose a special int value to mean "space" and remember to print it as a space each time. It’s fragile since a fellow programmer may forget to print the space and just print the int value.

The gold-plated solution (hoping that you don’t need it): Design an abstract Element class with two subclasses, IntElement and SpaceElement and create an array of Element (you can probably find a better name). Have the IntElement hold an int and its toString return a string representation of the int. Have SpaceElement contain no data and its toString return a string consisting of a single space. Now you can fill your array with numbers and spaces as you like.

How to Store Potatoes to Keep Them Fresh, ventilation. Plastic bags won't allow them to breathe and will shorten their shelf life considerably, so remove them from a plastic bag if you've brought them home in one. Brussels sprouts will last between 3 to 5 days. Cauliflower will last 1 week. Carrots, parsnips, turnips, beets, and radishes can be stored in a plastic produce bag and will last 2 weeks. The same goes for lettuce and other leafy greens, which will last from 3 days up to a week depending on how delicate the leaves are.

In the comment you mention that you're making a memory game and the whitespace is there only to visualize "right guesses". With this in mind, you could keep the existing array as it is and introduce a parallel boolean[] guesses = new boolean[a1.length]; having the same size as the values array.

Then you'd update guesses[i] = true; when there is a correct guess and print your array with something like

for (int i=0; i < a1.length; i++) {
   System.out.print(guesses[i] ? " " : a1[i]);

Storing Meat in Your Refrigerator, Storing fruits and veggies is quick and easy! You can either freeze, refrigerate, or even keep them on the countertop depending on the fruit/veggie. Store the yeast in the freezer for up to 3 months. Check the date that you wrote on the package and plan to use it within 3 months of putting the yeast into the freezer. If your freezer is set to 0 °F (−18 °C) or below, you may be able to store the yeast for longer, but the yeast might not be as effective once you thaw it.

How to Store Fruits and Veggies, Having a full fridge and pantry is satisfying, but there are ways to make food last even longer. Follow these storage tips to keep groceries fresh. The best way to store bulbs during the off-season is to place them in a paper bag or cardboard box. If your container has a lid, leave it off so the bulbs can breathe. Place your container in a cool, dark, dry place, like a basement or a closet so they don’t grow in storage.

How to store food so it lasts longer, plus what not to do, If you're moving a few things into storage, you'll want to bookmark this page for later. Here is expert advice on how to store virtually anything, whether in a  How to Store Strawberries. Stash berries in the fridge, preferably in the crisper—yes, it’s time to learn the right way to use your crisper drawer. This drawer controls moisture, so berries won’t dry out. To keep them from going the other direction and getting too damp, tuck a paper towel in the container of berries to absorb excess moisture.

How to Store Anything and Everything, Keep your foods fresh and reduce the risk of harmful bacteria by correctly storing your foods in Duration: 1:28 Posted: Aug 5, 2010 How to Store Papaya (6 Easy Ways) Take a look at our in-depth guide with step by step illustrations and colorful images on how to store this tropical sweet and nutritious fruit so you can enjoy it for many days.

  • you can't. a space is not a valid int.
  • You can't do that with an int array. If you use an Integer array you can replace the value with a null. When you print the array, you can choose what to display instead of the null value.
  • Take char array instead.
  • This sounds like an XY problem ( Can you explain why you want an integer array that contains a mixture of integers and spaces?
  • @ΝίκοςΒαλτσιόγης if it's just for showing purposes, use String or char.
  • ehm ... you can store a char as an int. the problem is that storing ' ' to an int, and print it, prints a numerical value representing that char, not a space.
  • Thank you all for your answers,this however is what i was looking for and it solved my problem.Thank you.
  • Thank you for your answer.
  • seeing as it's only for visual representation, the values don't have to be numerical, it would be easier to use an array of String or char
  • Then you'd also lose the information about the original data (values to be guessed) in your memory game. This solution creates an abstraction layer between the game data and the guesses made.
  • how is that? you can compare non-int values just as easily as ints