Java storing the inputs from scanner

scanner java
how to take string array input in java using scanner
how to store user input in java
java scanner prompt user input
how to take multiple string input in java using scanner
java keyboard input
how to input a name in java
taking string input in java

When I can't use the 'list', but need to store the inputs from the scanner, how can I deal with it?

I want to make the program counting the frequencies of the inputs appearing in the inputs from scanner.

For example, if the input is

"I like apple but I like banana too"

The result is

I: 2 like: 2 apple: 1 banana: 1 but: 1 too: 1

At first I thought of making the arrays of string, and every time input comes, I put them in the array. After then, although it would require n^2 time complexity, run the for loop for each element and then check if it has the same word.

    for (String str in arr){
        for(String str_2 in arr){
             if(strr.equals(str_2)){
                    count[i]++;
             } // count is the array storing the frequencies.

But problem here is... when declaring the arr, I should know the input size. Other people told me to use "list" , but the usage of the "list" is restricted.

In this situation, what would be the way?

Can you use Java streams?

String[] array = {"i", "like", "apple", "but", "i", "like", "banana", "too"};

Or to get the input from the user, something like:

Scanner sc = new Scanner(System.in);
int numberOfEntries = sc.nextInt(); // defines how big the array should be
String[] array = new String[numberOfEntries];
for (int i = 0; i < numberOfEntries; i++) {
    System.out.println("Enter value " + (i+1));
    String word = sc.next();
    array[i] = word;
}

Arrays.stream(array).collect(Collectors.groupingBy(p -> p, Collectors.counting()))
                .entrySet().stream().forEach(key -> System.out.println(key.getKey() + ": " + key.getValue()));

Output:

banana: 1

but: 1

apple: 1

too: 1

like: 2

i: 2

Java storing the inputs from scanner, Can you use Java streams? String[] array = {"i", "like", "apple", "but", "i", "like", "​banana", "too"};. Or to get the input from the user, something like: Java User Input The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.

You can do something like this by using a HashMap and iterating over the input array in a for loop and checking if the map already contains the key or not. If it contains then just increase the count in the value. If the key is not already in the map, just add it alongwith value 1.

for (String str : inputArray) {
    if (map.containsKey(str)) {
        map.put(str, map.get(str) + 1);
    } else {
        map.put(str, 1);
    }
}

Finally, just iterate over the map and print with key and value pairs.

Taking input in Java : Scanner class, Learn Scanner class to take input from user in java. Learn about nextInt, next, nextLine, nextFloat, nextDouble methods of Java. By Doug Lowe. Until Java 1.5, getting text input from the user in a console-based Java program wasn’t easy. But with Java 1.5, a new class — called Scanner — was introduced to simplify the task of getting input from the user. Here, you use the Scanner class to get simple input values from the user.

  String input = "I like apple but I like banana too";
    String[] words = input.split(" ");
    int countfre=0;
    HashMap<String,Integer> map = new HashMap<String, Integer>();
    for(int i=0;i<words.length;i++){
        if(!map.containsKey(words[i])){
            for (int j=0;j<words.length;j++){
                if(words[i].equalsIgnoreCase(words[j])){
                    countfre++;
                }
                map.put(words[i],countfre);

            }
            countfre=0;
            System.out.println(words[i] + " = " +map.get(words[i]));

        }

    }

//Without java stream api. //Stored output in Map.

Output:

banana = 1

but = 1

apple = 1

too = 1

like = 2

i = 2

How to save user input inside an array in Java, Scanner input = new Scanner(system.in);// User input System.out.println("Enter Values :"); int array[]=new int[5]; // Intilized array of size 5 for (int i=0;i<5;i++)  The problem I'm having is that the Scanner.next() method only returns the first word before a space. So if the input is "My Birthday", the description of that instance of an Event is simply "My". I did some research and found that people used Scanner.nextLine() for this issue, but when I try this, it just skips past where the input should go.

Try something like,

public static void main(String[] args) {
        //input
        String s = "I like apple but I like banana too";
        //desired output
        //I: 2 like: 2 apple: 1 banana: 1 but: 1 too: 1

        String[] str = s.split(" ");
        String[] result = new String[str.length];
        int temp = 0;

        test:
        for (String str1 : str) {

            for (String str2 : result) {
                if(str1.equals(str2)){
                    continue test;
                }
            }
            result[temp++] = str1;
            int count = 0;
            for (String str2 : str) {
                 if(str1.equals(str2)){
                     count++;
                 }
            }
            System.out.print(str1 + ": " + count + " ");
        }

Java User Input (Scanner class), The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the  So there you need Scanner class. You have to import java.util.Scanner; and in the code you need to use . Scanner input = new Scanner(System.in); Input is a variable name.

How to Get Input with Java's Scanner Class, Until Java 1.5, getting text input from the user in a console-based Java program wasn't easy. But with Java 1.5, a new class — called Scanner — was introduced​  A scanner can read text from any object which implements the Readable interface. If an invocation of the underlying readable's Readable.read(java.nio.CharBuffer) method throws an IOException then the scanner assumes that the end of the input has been reached.

java for complete beginners - user input, One really useful class that handles input from a user is called the Scanner class This is the same code, except that java will now store whatever the user types  Scanner input = new Scanner(system.in);// User input System.out.println("Enter Values :"); int array[]=new int[5]; // Intilized array of size 5 for (int i=0;i&lt;5

Input and output | Think Java, As shown in Figure 3.1, System is defined in a file called System.java, and PrintStream is Scanner provides a method called nextLine that reads a line of input from the Keeping that in mind, here's how we can convert a measurement in  The Scanner class of the java.util package gives you methods like nextInt(), nextByte(), nextFloat() etc. to read data from keyboard. To read an element of an array uses these methods in a for loop:

Comments
  • You'd want to use a map instead or, if you need to use arrays, an array of objects which you need to replace with a bigger array (and copy the old one) when needed - much like ArrayList does it. If you must not use lists then I'd assume the purpose is to teach you something to it necessary that you understand what you should learn and to tell us what this is if we're to help in a meaningful way.