2

I am writing a program and I need to input a value for index, but the index should be composite, e.g 44GH. My question is, how to make the program to do not crash when I ask the user to input it and then I want to display it?

I have been looking for answer in the site, but they are only for integer or string type. If anyone can help, would be really appreciated.

         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);
}
}
5
  • If you parse a string containing letters as an integer, your code will crash. There is not way around this. Can you tell us more about what you plan to do with the input 44GH ? Commented Nov 17, 2016 at 14:55
  • It is for a program that ask the user to input a name of a product, and a code and the code should be something like 44GH. Then the program should ask the user to input a code from the keyboard and the program should delete it. Commented Nov 17, 2016 at 15:01
  • it's not really clear what you're trying to do. Do you mean you just want to ditch the non-numeric part? Commented Nov 17, 2016 at 15:13
  • I want the non-numeric and the numeric part together. And I need to delete this "index", which contains both non-numeric and numeric part Commented Nov 17, 2016 at 15:15
  • I updated the code Commented Nov 17, 2016 at 15:29

4 Answers 4

4

Remove all non digits char before casting to integer:

String numbersOnly= aString.replaceAll("[^0-9]", "");
Integer result = Integer.parseInt(numbersOnly);
Sign up to request clarification or add additional context in comments.

Comments

0

The best way to do it is to create some RegEx that could solve this problem, and you test if your input matches your RegExp. Here's a good website to test RegExp : Debuggex

Then, when you know how to extract the Integer part, you parse it.

Comments

0

I think the OP wants to print out a string just but correct me if I am wrong. So,

    Scanner input = new Scanner(System.in);
    String aString = input.nextLine(); // FFR55 or something is expected
    System.out.println(aString);

Then obviously you can use:

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

To modify the output but from what I gather, the output is expected to be something like FFR55.

Comments

0

Try making the code split the two parts:

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;

This code makes a variable numbers, holding the index of the number part of the input string, as well as char0Index and char1Index, holding the value of the two characters from 0-25.

You can add the two characters, or use the characters for rows and numbers for columns, or whatever you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.