Home > Java > How to store string tokens into array

How to store string tokens into array

王林
Release: 2024-02-09 13:30:15
forward
1343 people have browsed it

php editor Strawberry will introduce how to store string tags into an array. During programming, we often need to process strings and split them into multiple tokens. Storing these tags in an array makes it easier for us to process and operate on them. This article will explain in detail how to use functions and methods in PHP to implement this function to help readers better understand and use it. Whether you are a beginner or an experienced developer, you can gain useful knowledge and practical tips from this article. Let’s get started!

Question content

I refer to some examples to successfully extract each part of user input. But it can only be withdrawn once. There should be 2 loops for extracting multiple inputs and saving the markers in an array. I'm stuck on an array, what should I do?

question:
write a program that accepts string tokens in the format of token1:token2:token3:token4 , where : is used as delimiter to separate tokens. there should be two functions, ingest and appearance.

ingest takes a string, and stores it in the collection.

appearance takes a string as input . it returns a normalized value
between 0 to 1,  where the value represents the percentage of
appearances of stored tokens which have input as the prefix.

state the space and time complexity of your solution.


Copy after login

expected outcome:

ingest('mcdonal:uk:employeea')
ingest('mcdonal:hk:employeea')
ingest('mcdonal:hk:employeeb')
ingest('mcdonal:hk:employeec')
ingest('fastfood')

appearance('mcdonal')
# > 0.8
appearance('mcdonal:hk')
# > 0.6
Copy after login

My code:

String input;
   
        
 // For user input
 Scanner sentense = new Scanner(System.in);
 input = sentense.nextLine();   
 String[] ingestWords = {};
       
             
 // Use ':' to seperate input
 StringTokenizer st = new StringTokenizer(input, ":");
 while(st.hasMoreTokens()) { 
   System.out.println(st.nextToken());
 }
Copy after login

Solution

I suggest you solve your problem in two steps.

First, the ingestion part: you need to accept words entered by the user and store each word in an arraylist<string>, not a fixed size array, since you don't know in advance what you are going to get How many inputs.

The sample code is as follows.

public static void main(string[] args) {
    string input;
    list<string> ingestwords = new arraylist<>();

    scanner sentence = new scanner(system.in);
        
    while (sentence.hasnext()) {
        input = sentence.next();
            
        if (input.equals("exit")) { // to stop receiving input
            break;
        }
            
        ingestwords.add(input);         
    }
    sentence.close();
}
Copy after login

Second, the appearance part: given a string, you need to iterate the list of words from the ingestion part and check which words start with the given string.

For example, you can create a helper function like this.

void hasPrefix(String word, String str) {
    return word.startsWith(str);
}
Copy after login

Applying this function to each word in ingestwords will give you the number of words prefixed with str. And you can figure out the percentage of occurrences from that.

The above is the detailed content of How to store string tokens into array. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template