Java输入/输出混乱

Java输入/输出混乱,java,parseint,Java,Parseint,我正在写一个程序,我需要为索引输入一个值,但是索引应该是复合的,例如44GH。 我的问题是,当我要求用户输入程序,然后我想显示它时,如何使程序不崩溃 我一直在寻找答案在网站上,但他们只为整数或字符串类型。 如果有人能帮忙,我将不胜感激 Scanner s input = new Scanner(System.in); private ArrayList<Product> productList; System.out.println("Enter

我正在写一个程序,我需要为索引输入一个值,但是索引应该是复合的,例如44GH。 我的问题是,当我要求用户输入程序,然后我想显示它时,如何使程序不崩溃

我一直在寻找答案在网站上,但他们只为整数或字符串类型。 如果有人能帮忙,我将不胜感激

         Scanner s input = new Scanner(System.in);
    private ArrayList<Product> productList;

    System.out.println("Enter the product");
    String product = s.nextLine();

    System.out.println("Input code for your product e.g F7PP");
    String code = s.nextLine();
}
public void deleteProduct(){
   System.out.println("Enter the code of your product that you want to delete ");
    String removed = input.nextLine();
            if (productList.isEmpty()) {
            System.out.println("There are no products for removing");
       }  else {
    String aString = input.next(); 
    productList.remove(aString);
}
}
Scanner s input=新扫描仪(System.in);
私有ArrayList产品列表;
System.out.println(“输入产品”);
字符串product=s.nextLine();
System.out.println(“产品的输入代码,例如F7PP”);
字符串代码=s.nextLine();
}
公共产品(){
System.out.println(“输入要删除的产品代码”);
String removed=input.nextLine();
if(productList.isEmpty()){
System.out.println(“没有可移除的产品”);
}否则{
字符串aString=input.next();
产品列表。移除(覆盖);
}
}

最好的方法是创建一些可以解决此问题的正则表达式,然后测试输入是否与正则表达式匹配。下面是一个测试RegExp的好网站:


然后,当您知道如何提取整数部分时,可以对其进行解析。

在转换为整数之前删除所有非数字字符:

String numbersOnly= aString.replaceAll("[^0-9]", "");
Integer result = Integer.parseInt(numbersOnly);

我想OP只是想打印一个字符串,但如果我错了,请纠正我。所以

    Scanner input = new Scanner(System.in);
    String aString = input.nextLine(); // FFR55 or something is expected
    System.out.println(aString);
显然,你可以使用:

    aString.replaceAll();
    Integer.parseInt();

要修改输出,但根据我收集的信息,输出应该类似于FFR55。

尝试将代码分成两部分:

int numbers = Integer.parseInt(string.replaceAll("[^0-9]", ""));
String chars = string.replaceAll("[0-9]", "").toUpperCase();
int char0Index = ((int) chars.charAt(0)) - 65;
int char1Index = ((int) chars.charAt(1)) - 65;
此代码生成一个变量
numbers
,保存输入字符串中数字部分的索引,以及
char0Index
char1Index
,保存0-25之间两个字符的值


您可以添加这两个字符,或将这些字符用于行,将数字用于列,或根据需要使用任何字符。

如果将包含字母的字符串解析为整数,代码将崩溃。这是没有办法的。您能告诉我们更多关于您计划如何使用输入
44GH
?它适用于要求用户输入产品名称的程序,代码和代码应该类似于44GH。然后,程序应该要求用户从键盘输入一个代码,然后程序应该删除它。现在还不清楚你想做什么。你是说你只想放弃非数字部分吗?我想把非数字部分和数字部分放在一起。我需要删除这个“索引”,它包含非数字和数字部分,我更新了代码