Develper
Develper

Reputation: 57

Accept two integers separated by a delimiter and print their sum

import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt().split(":");
        int B = sc.nextInt();
        System.out.println(A + B);
    }
}

If I'm given an input like 1:2 then the output should be 3. Likewise 54:6 then 60.

But I'm getting an error. What should I do to achieve that output?

Upvotes: 1

Views: 13989

Answers (6)

Mughil Iniyan
Mughil Iniyan

Reputation: 1

import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        sc.next().split(":");
        int B = sc.nextInt();
        System.out.println(A + B);
    }
}

Upvotes: 0

Lakshmi priya
Lakshmi priya

Reputation: 11

You can try this:

import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int A = sc.nextInt();
        sc.next().charAt(0);
        int B = sc.nextInt();
        System.out.println(A + B);
    }
}

Upvotes: 1

sai krishna
sai krishna

Reputation: 1

I think Splitting of two integers is not possible in java(it is possible in python by using the input.split() function) ,for that reason it is better take input as string and split input by using colon(:) operator and convert those input string to an integer and add the both to print the result. java code:

   import java.util.Scanner;
   Public class TwoIntegers
   {
    public static void main(String args[])
    {
     Scanner s=new Scanner(System.in());
     String[] two_numbers = s.next().split(":");
     int fir_num = Int.parseInt(two_numbers[0]);
     int sec_num = Int.parseInt(two_numbers[1]);
     int sum=fir_num+sec_num;
     System.out.println("The sum of two numbers is:"+sum);
    }
   }

Upvotes: 0

Punith kumar
Punith kumar

Reputation: 47

you will need to take input as string and then split your input by : and convert the string to integer and add them.

see example below


public class Hello {

    public static void main(String[] args) {
        String input;
        Scanner sc = new Scanner(System.in);
        input = sc.next();
        String[] parts = input.split(":");
       if(parts.length > 0) {
           int sum = Integer.parseInt(parts[0])+Integer.parseInt(parts[1]); 
           System.out.println(sum);
       } else{
             System.out.println("Enter number in format example 12:2");
       }
    }
}```

Upvotes: 1

rhino_rus
rhino_rus

Reputation: 140

At first, read a whole input line into a String variable. Then just split it into two values and cast them to integer.

Code example:

Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine();
String[] splittedValues = inputString.split(":");
int 
    A = Integer.parseInt(splittedValues[0]),
    B = Integer.parseInt(splittedValues[1]); 

System.out.println(A + B);

Upvotes: 1

Sebastian Heikamp
Sebastian Heikamp

Reputation: 976

You cannot call split on an integer, it is meant for splitting a String. Try this:

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] numbers = sc.next().split(":");
        int A = Integer.parseInt(numbers[0]);
        int B = Integer.parseInt(numbers[1]);
        System.out.println(A + B);
    }
}

Of course some validation would be nice (check if the String contains a colon, if the parts are numeric, etc.), but this should point you in the right direction.

Upvotes: 3

Related Questions