Table of Contents
Question content
Solution
Home Java How to store string tokens into array

How to store string tokens into array

Feb 09, 2024 pm 01:30 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)