0

How can I extract each String from A B -> carry sum without -> ? So, I need to get only A B carry sum. How can it be done?

A B -> carry sum is passed as a String into the method.

public void parseContactsLine(String line)
{
    Scanner readLine = new Scanner(line);

    while(readLine.hasNext())
    {

    }
}
Andy
  • 3,997
  • 2
  • 19
  • 39
Nikolay
  • 321
  • 3
  • 5
  • 13

2 Answers2

2

You can split the string using the split() method:

public void parseContactsLine(String line)
{
    Scanner readLine = new Scanner(line);

    while(readLine.hasNext())
    {
        String[] parts = line.split("->");
        int a = Integer.parseInt(parts[0].split("\\s+")[0]); // Split string by whitespaces
        int b = Integer.parseInt(parts[0].split("\\s+")[1]);            
        int carry = Integer.parseInt(parts[1].trim().split("\\s+")[0]);
        int sum = Integer.parseInt(parts[1].trim().split("\\s+")[1]);
        // Do whatever you want with a, b, carry and sum
    }
}
Lior Erez
  • 1,852
  • 2
  • 19
  • 24
0

You could break the string around the spaces, and ignore the -> you could use regex, use the string.split function, or even iterate over the string and do it yourself

AndrewGrant
  • 786
  • 7
  • 17
  • You know that each input will have 5 strings separated by sources, you know that the third one is the arrow. It sounds like you are doing this for a class, so implementing the function yourself is ideal – AndrewGrant Apr 12 '15 at 01:20