Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize string concatenation, integer addition and floating point addition by Java

Shulou Source: shulou.com Published: 2022-06-01 12:43:48 09月29日 Update

Today, Xiaobian to share with you how to achieve string concatenation, integer addition and floating point addition related knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Write a Java application program, read two strings from the keyboard, and overload three functions to realize the concatenation of the two strings, integer addition and floating point addition, and output the results. To handle the exception, prompt the user to package zyy.wxt.demo the input string that does not meet the requirements

Import java.util.Scanner;public class overload {public static void main (String [] args) {String S1 = null, S2 = null; Scanner sc = new Scanner (System.in); try {System.out.println ("Please enter the first string:"); S1 = sc.next (); System.out.println ("Please enter the second string:") S2 = sc.next ();} catch (Exception e) {System.out.println (e.getMessage ());} int i1 = Integer.parseInt (S1); int i2 = Integer.parseInt (S2); float F1 = Float.parseFloat (S1); float f2 = Float.parseFloat (S2); System.out.println (F1) System.out.println ("splicing result:" + strAdd (S1, S2)); System.out.println ("Integer result:" + strAdd (i1, i2)); System.out.println ("floating point result:" + strAdd (F1, f2));} public static String strAdd (String str1, String str2) {return str1 + str2;} public static int strAdd (int a, int b) {return a + b } public static String strAdd (float f11, float f22) {return String.valueOf (f11 + f22);}}

Running result

Please enter the first string:

one hundred and thirty two

Please enter the second string:

three hundred and twenty one

132.0

Stitching result: 132321

Integer result: 453

Floating point result: 453.0

These are all the contents of this article entitled "how Java implements string concatenation, integer addition and floating point addition". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Characters strings results input knowledge articles integers points two content floating points users different large functions most that is applications more knowledge points Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft Shulou Tech Info MariaDB Shulou Technology