I want the input to be stored in an array and print the string of that specific int in a line

int arrays
write statements to declare the array and assign the values to the corresponding positions.
parts of an array
array reference java
java arrays
initialize int array
how to set a value in an array java
array of objects in java

I want the integer to be stored in an array and then once the user enters 0 the while loop will stop and print the contents of the array in string format.

I tried things with the area but quite honestly I do not understand them.

public static void apples() {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter your integer: ");
    int number = input.nextInt();
    int[] array = {};

    while(number != 0) {
        if(number == 1){
            System.out.println("0");
        }
        else if (number == 3) {
            System.out.println("f");
        }
        else if (number == 5) {
            System.out.println("-l");
        }
        else if (number == 7) {
            System.out.println("V");
        }
        else if (number == 9) {
            System.out.println("-/");
        }
        else if (number == 11) {
            System.out.println("()");
        }
        else if (number == 13) {
            System.out.println("()");
        }
        else if (number == 15) {
            System.out.println("^");
        }
        else if (number == 17) {
            System.out.println("E");
        }
        else if (number == 19) {
            System.out.println("=");
        }
        else if (number == 21) {
            System.out.println("x");
        }
        else if (number == 23) {
            System.out.println("y");
        }
        System.out.println("int: " +  number);
    }
};

My expected results would be for the input 135 and the output be 0f-l

Take a look at this simple example of usage of Map and List.

 public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(0, "");
        map.put(1, "0");
        map.put(3, "f");
        map.put(5, "-1");
        map.put(7, "V");
        map.put(9, "-/");
        map.put(11, "()");
        map.put(13, "()");
        map.put(15, "^");
        map.put(17, "E");
        map.put(19, "=");
        map.put(21, "x");
        map.put(23, "y");
        List<Integer> listOfInputIntegers = new ArrayList<>();
        Scanner input = new Scanner(System.in);
        int integer;

        do {
            System.out.print("Input your next number:");
            integer = input.nextInt();
            listOfInputIntegers.add(integer);

        } while (integer != 0);
        for (int i : listOfInputIntegers) {
            System.out.print(map.get(i));
        }
        System.out.println();
    }

https://docs.oracle.com/javase/8/docs/api/java/util/Map.html

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

Arrays and References | Think Java, In this chapter, you'll learn how to store multiple values of the same type by using a single variable. Another algorithm would initialize 26 variables to zero, loop through the string one time For example, the following lines declare that counts is an “integer array” and values is For example, say you print an array like this: � In the above program, since each element in array contains another array, just using Arrays.toString() prints the address of the elements (nested array). To get the numbers from the inner array, we just another function Arrays.deepToString() .

Keep the integer to string mappings in a java.util.Map to easy get the corresponding string after the user types the int. If the typed int has a corresponding string add it to the list of strings to be printed at the end. If the typed int does not have a corresponding string then print it and ask for next int

After the user types in 0 stop the loop, make a string out of the list of strings accumulated until the user typed 0 by joining all the strings from the list

public class Test {

    private static Map<Integer, String> integerToStringMappings = new HashMap<>();

    static {
        integerToStringMappings.put(1, "0");
        integerToStringMappings.put(3, "f");
        integerToStringMappings.put(5, "-l");
        .... // and so on for all the integers mapped to strings
    }

    public static void main(String[] args) {

        List<String> strings = new ArrayList<>();
        Integer number = input.nextInt();

        while(number != 0) {
            System.out.println("Enter your integer: ");
            number = input.nextInt();
            String correspondingString = integerToStringMappings.get(number);

            if ( correspondingString == null ) {
                System.out.println("int: " +  number);
            } else {
                strings.add(correspondingString);
            }
        }

        System.out.println(String.join("", strings));
    }
}

Array Basics, int list[30]; // an array of 30 integers char name[20]; // an array of 20 characters double The last example illustrates a two dimensional array (which we often like to With normal variables, we could declare on one line, then initialize on the next: to store C-style strings, you can initialize a character array with a string literal� The string which is passed as an argument to the printf function is printed. Next, we will learn how to print a string that is stored in a character array. #include <stdio.h>

This can be easily accomplished by using an ArrayList since you do not know how big the array will be (unless you specifically ask the user for the number of items they will be entering).

After collecting the user's numbers, just loop through each one in the ArrayList to produce the corresponding output using your if-else statements. I highly advise using a switch statement in this case.

ArrayList<Integer> values = new ArrayList<Integer>();
int number = input.nextInt();
while(number != 0) {
    values.add(number);
    number = input.nextInt();
}

for(int i=0; i<values.size(); i++) {
    int numberCheck = values.get(i);
    //Run through the if-else statements using numberCheck
    System.out.println("int: " + numberCheck);
}

Java Arrays, For instance, an array of int is a collection of variables of the type int . A Java array variable is declared just like you would declare a The first line declares an array of String references. You can read their value, assign values to them, use the elements in calculations and pass specific elements as� Read and Print elements of an array: ----- Input 10 elements in the array : element - 0 : 2 element - 1 : 4 element - 2 : 6 element - 3 : 8 element - 4 : 10 element - 5 : 12 element - 6 : 14 element - 7 : 16 element - 8 : 18 element - 9 : 20 Elements in array are: 2 4 6 8 10 12 14 16 18 20

Arrays in C# - working with arrays in CSharp, In this part of the C# programming tutorial, we will cover arrays. Arrays are used to store data of our applications. We declare arrays to be of a certain data type. the elements within an array ( int , String , float in our case) and a pair of square brackets [] . We go through the array and print its elements. @user2316393 Sorry I hadn't noticed you had updated your answer. Well, as I've pointed out scanf is a dangerous function.Also my way does not rely on loops reducing code, possibilities for something to go wrong (i.e. bugs) and clutter :) With regards to your pointers request, when passing user_input to fgets it will decay into a pointer as arrays always decay into pointers when passed as

Day 5 -- Lists and Array Variables, Storing Lists in Array Variables; Accessing an Element of an Array Variable Retrieving the length of a list; Using array slices; Using an array to store input; Sorting a list or array Line 15 prints the total for a particular random number. ( It doesn't handle strings such as 0000 or 0.0, which you might want your program to� It's not clear to me what you're trying to do. If the entire contents of the file are "kyu", then the input definitely will stop when it reaches end of file.What I suspect (but I'm really just guessing) is that you've output the strings without a separator, and so the input doesn't know where each string ends, and reads all of the characters as a single string.

Chapter 7: Arrays, Like Strings, arrays use zero-based indexing, that is, array indexes start with 0. A second loop should print the values of the array with spaces between the values. int[ ] temps = new int[days]; // Input and store the temperatures in the array for (int i After the first line is printed, how many arrays can this program access? X — Input array array Input array. To display more than one array, you can use concatenation or the sprintf or fprintf functions as shown in the example, Display Multiple Variables on Same Line .

Comments
  • Where do you save the number?
  • This is the better way to go. This would allow you to easily check for bad input with if (!map.containsKey(i)) and report an error instead of throwing a "no such key" exception. You should always check for unexpected input values and deal with them appropriately.
  • Agree. Exception checking could be implemented in various ways with it and one of it would be (!map.containsKey(integer)). @Steve
  • Thank you so much! That makes sense I appreciate your help
  • Oops...yeah, I meant 'integer' rather than 'i'.
  • One other small suggestion. I think it cleans up your code to define your static data outside of main(). You could do this by declaring map above main() as a static variable, and then put the adding of the items in a static {} block. - nothing really wrong with the way @MS90 did it though.
  • Ha! I didn't see this until I made a similar comment on the first answer. So @ValentinCarnu demonstrates my suggestion here.
  • This also deals with the bad input case. Bravo!