简体   繁体   中英

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?

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.

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);

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");
       }
    }
}```
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
sc.next();
int B = sc.nextInt();
System.out.println(A + B);

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);
}

}

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);
    }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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