简体   繁体   English

如果我想在字符串中的特定 position 添加特定字符,我将如何处理 go 呢? [爪哇]

[英]If I wanted to add a specific character at a specific position in a string, how would I go about that? [Java]

I'm working on a school assignment for a hotline, and the output has to be a 1-800- number.我正在为热线电话分配学校作业, 1-800-必须是 1-800 号码。 I've worked out everything, but in the end, the requirement is that after 1-800- , I need to add another hyphen - in front of the third number after 1-800- .我已经解决了所有问题,但最后,要求是在1-800-之后,我需要在1-800-之后的第三个数字前面添加另一个连字符- How would I go about that?我怎么会 go 呢? I've worked out that the hyphen would come at the 9th index if that helps.我已经计算出,如果有帮助的话,连字符会出现在第 9 个索引处。 ASCII determines the number output after the user inputs a string.用户输入字符串后,ASCII 确定数字 output。

An example output would be:例如 output 将是:

Enter a string: help now
1-800-4357669

Process finished with exit code 0

In this case, the hyphen would come after the number 5 , which is the 9th index (since the number 5 is at the 8th index).在这种情况下,连字符将出现在数字5之后,即第 9 个索引(因为数字5在第 8 个索引处)。

I'm new to java, so I haven't quite figured it out yet.我是 java 的新手,所以我还没有完全弄清楚。 Any help is appreciated.任何帮助表示赞赏。

I figured it out.我想到了。 I just split the string using the .substring() function and then joined the two splits with a hyphen.我只是使用.substring() function 拆分字符串,然后用连字符连接两个拆分。

String string1 = final_number_temp.substring(0,9);
String string2 = final_number_temp.substring(9);
String final_number = string1 + "-" + string2;

System.out.println(final_number);

you had to elaborate on many thing like you would insert the hyphen in the 3rd index of the entered number or the 9th index, like whether the user would enter 1-800-4357669 or just 4357669 , also you didn't post your code, so there is so many missing details, anyways,this is my solution for your problem:您必须详细说明许多事情,例如您将在输入数字的第三个索引或第 9 个索引中插入连字符,例如用户是输入1-800-4357669还是仅输入4357669 ,您也没有发布您的代码,所以有很多缺失的细节,无论如何,这是我对你的问题的解决方案:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        String basicNum = "1-800";
        String dummyString;

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a string:");
        dummyString = scanner.nextLine();

        System.out.println("Enter a number");
        dummyString = scanner.nextLine();

        try
        {
            int enteredNum = Integer.parseInt(dummyString); // -> throws NumberFormatException if entered value isn't integer
            int enteredNumLen = String.valueOf(enteredNum).length();
            int firstPart = (int) (enteredNum / Math.pow(10 , (enteredNumLen - 3)));
            int secondPart = (int) (enteredNum % Math.pow(10 , (enteredNumLen - 3)));
            basicNum = String.format("%s-%d-%d", basicNum, firstPart, secondPart);
            System.out.println(basicNum);
        }catch (NumberFormatException numberFormatException){
             // do whatever here to handle if the entered value isn't integer
            System.out.println("not valid number");
        }
    }
}

this code is supposed to work with a number entered by the user and here is image of the result when I run the code:此代码应该与用户输入的数字一起使用,这是我运行代码时的结果图像: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何 go 关于删除 ArrayList 中的特定字符? - How would I go about removing a specific character in an ArrayList? Java:我将如何使用正则表达式过滤字符串以查找特定字符集? - Java: How would I go about filtering a String with regex to find a specific charset? 我将如何从单链列表中的特定节点返回数据? - How would I go about returning the data from a specific node in a singly linked list? 我将如何使用jsoup从此HTML解析特定的整数值? - How would I go about using jsoup to parse a specific integer value from this HTML? 我将如何将MongoDb集合附加到Java视图 - How would i go about to attache a MongoDb Collection to a Java View 我将如何使用整数定界符? (Java) - How would I go about using an integer delimiter? (Java) 我将如何跟踪此Java数组代码段 - How would I go about tracing this java arrays code snippet 如何将布尔字符串更改为JTable中的JCheckBox? - How would I go about changing the boolean string to a JCheckBox in JTable? 我将如何实现这个 Java 接口? - How would I go about implementing this Java interface? 我将如何在JAVA和PHP之间传输数据? - How would I go about Transfering data between JAVA and PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM