简体   繁体   English

Java-使用indexOf方法作为输入

[英]Java - Using method indexOf for inputs

So, I am trying to find a specific character within an input given by the user. 因此,我试图在用户给定的输入中查找特定字符。 In this problem, the user inputs a British telephone number using the format 在此问题中,用户使用以下格式输入英国电话号码

(XXXX) XXX XXXX or (XXXXX) XXX XXX (XXXX)XXX XXXX或(XXXXX)XXX XXX

where the area code is formed by the numbers within brackets, and the personal phone number is formed by the numbers after that. 其中区号由括号内的数字组成,而个人电话号码由其后的数字组成。

Heretofore, I have tried - unsuccessfully - to use the indexOf(String) method to find the closing bracket within the user's input, with this being my code: 到目前为止,我已经尝试-未成功-使用indexOf(String)方法在用户输入中找到右括号,这是我的代码:

import javax.swing.JOptionPane;

public class L2ExerciseE {
    public static void main(String[] args) {
        String number = JOptionPane.showInputDialog("Please input thy number: ");

        String areaCode = number.substring(1, indexOf(")"));
        String telephoneNumber = number.substring((indexOf(")") + 1), 15);
        System.out.println();

        System.out.println("The area code is: " + areaCode);
        System.out.println("The telephone number: " + telephoneNumber);
    }
}

I guess that this problem is occurring because java uses a compiler, while Python , my previous language, used an interpreter, and, even though I've tried reading other answers, I'm having serious trouble understanding them. 我猜是因为Java使用了编译器,而我以前的语言Python使用了解释器,所以发生了这个问题,尽管我尝试阅读其他答案,但在理解它们时却遇到了严重的麻烦。

What can I do to make my code work without over complicationg things? 我该怎么办才能使我的代码正常工作而又不至于太复杂? For the moment, we have only seen some I/O commands, arithmetic commands, basic concatenation, and some few other commands like .substring() , .indexOf() , and .trim() . 目前,我们仅看到一些I / O命令,算术命令,基本串联以及一些其他命令,例如.substring() .indexOf().trim()

Thank you very much in advance! 提前非常感谢您!

indexOf() is not a stand-alone function (nothing in Java is a stand-alone function), it is a method on a String . indexOf()不是独立函数(Java中的任何东西都不是独立函数),它是String上的方法。 You need to use number.indexOf(")") - you're looking for ")" in number . 您需要使用number.indexOf(")") -您要在number寻找")"

Try below. 请尝试以下。 Documentation of string class https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int) 字符串类https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)的文档

String areaCode = name.substring(name.indexOf("(")+1,name.indexOf(")"));
        String personalNo = name.substring(name.indexOf(")")+1);

Use a regex pattern: 使用正则表达式模式:

String number = "(1234) 123 1234";
String pattern = "\\((\\d{4})\\) (\\d{3} \\d{4})|\\((\\d{5})\\) (\\d{3} \\d{3})";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(number);
if (m.find()) { 
    System.out.println("Found area code: " + m.group(1));
    System.out.println("Found personal number: " + m.group(2));
}

The reason that a regex based solution might be the way to go here is that your exact number pattern may have some variety to it. 基于正则表达式的解决方案可能是解决问题的方法,原因是您的确切数字模式可能有所不同。 For example, a number with more than one space might still be considered valid, and this is easy to expresss in a regex. 例如,具有多个空格的数字可能仍被认为是有效的,并且这很容易用正则表达式表示。

Demo 演示

There is one mistake in your code. 您的代码中有一个错误。 Try this: String areaCode = number.substring(1, number.indexOf(")")); 试试看:字符串areaCode = number.substring(1,number.indexOf(“)”)); String telephoneNumber = number.substring((number.indexOf(")") + 1), 15); 字符串TelephoneNumber = number.substring(((number.indexOf(“)”)+ 1),15);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM