Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the inputs and outputs of common data types in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the input and output of Java common data types. It is very detailed and has certain reference value. Friends who are interested must finish reading it.

1. Chart type

The char type here refers to the case where only one character is entered.

1.1 input format:

Import java.io.IOException;// Import package

Public class Main {

Public static void main (String [] args) throws IOException {

Char ch = (char) System.in.read (); /

System.out.println ((int) ch)

}

}

Description: it needs to be used with IOException exceptions. The System.in is input from the standard input stream (most commonly the keyboard), and the rand () method reads the input from this stream. The input result is int and needs to be forced to char.

1.2 give an example

2. Int 1.1 simple int format input:

This refers to a situation where there is only one int input per line. For example, enter only one integer per line.

Import java.util.Scanner

Public class Main {

Public static void main (String [] args) {

Scanner scan = new Scanner (System.in)

Int num = scan.nextInt ()

System.out.println (num)

}

}

1.2 give an example

Note: Long num is required, otherwise the num is very large, the result is not accurate.

2.1 input in int format with spaces:

A format similar to 2334. There is a space between the two numbers, at this time, using int input alone will not solve the problem, of course, you can use two consecutive scan.nextInt () for input. However, we can at this time, we need to change the angle. We treat 23-34 as a whole as a string, and then split it in the space into 23 and 34 strings, which can be converted into integers. Please refer to the official help manual for the method of split ().

Import java.util.Scanner

Public class Main {

Public static void main (String [] args) {

Scanner scan = new Scanner (System.in)

String [] str = scan.nextLine (). Split ("[]"); / / divided into several blocks, there are several string arrays, here are two blocks

Int a = Integer.parseInt (str [0])

Int b = Integer.parseInt (str [1]); / / etc.

System.out.println (a + "" + b)

}

}

2.2 give an example

3.1 input of complex int format

The method is the same as that described in 2. 1, similar to typing await 3 and 2. 1. Let's go straight to the example here.

3.2 give an example

The input of the long type is similar to that of the int type and is not repeated.

3. Simple type

In Java, you should use the float type rather than the float type.

The floating-point type is mainly its formatted output, such as preserving the format of two decimal places. In Java, there are printf methods similar to those in C, and we can also use the format () method in String to implement them.

1.1 double preserves output in two-bit format

Import java.util.Scanner

Public class Main {public static void main (String [] args) {Scanner scan = new Scanner (System.in); double num = scan.nextDouble (); String a = String.format ("% .2f", num); System.out.println (a);}}

/ / output in printf format:

/ / System.out.printf ("2f", num)

1.2 give an example

Import java.util.Scanner;public class Main {public static void main (String [] args) {Scanner scan = new Scanner (System.in); String str = scan.nextLine (); String [] num = str.split ("[;,]"); String a = String.format ("% .2f", Double.parseDouble ((num1]) String b = String.format ("% .2f", Double.parseDouble ((num2])); String c = String.format ("% .2f", Double.parseDouble ((num3); System.out.println ("The each subject score of No. "+ num [0] +" is "+ a +", "+ b +", "+ c +". ");}} 4, enter 1.1 input format multiple times

In C, there are two relatively simple ways to loop multiple inputs:

While (scanf (% d, & n)! = EOF)

While (~ scanf ("d", & n))

In Java, there are also simple ways:

While (scan.hasNext ())

1.2 give an example

It is important to note that when you enter a single character in multiple groups, you need to enter it in a string format to convert it to a character type.

5. Array

The input is similar to that in C language. However, it should be noted that input such as string, in C language, it is a pointer type, in Java, there is its own string type, can not be like C language, without learning the pointer, loop input each character to form a string.

1.1 Array input format:

Import java.util.Scanner

Public class Main {public static void main (String [] args) {Scanner scan = new Scanner (System.in); int [] arr = new int [3]; / / enter 3 for (int I = 0; I < arr.length; I +) {arr [I] = scan.nextInt ();} for (int I = 0; I < arr.length) ) {System.out.print (ARR [I] + ");} 2.1 array converted to a string

You can use the toString () method in Arrays.

Import java.util.Scanner

Import java.util.Arrays

Public class Main {public static void main (String [] args) {Scanner scan = new Scanner (System.in); int [] arr = new int [3]; / / enter 3 for (int I = 0; I < arr.length; iTunes +) {arr [I] = scan.nextInt ();} System.out.println (Arrays.toString (arr));}}

Type 1, 2, 2, 3 and you will see [1, 2, 2, 3]. Sometimes the format of the OJ question is 1 / 2 / 3. [1] this format can also be passed.

6. String

Because most of the inputs are converted to string types. Therefore, for strings, there are many operations that need to be converted, such as converting segmented strings to integers, floating-point types, arrays and so on.

1.1 string conversion to integer, floating-point type (take integer as an example)

Int a = Integer.parseInt (str [0]); / / assume that str [0] is a character'1' after segmentation

1.2 Integer, floating-point conversion to string

Int num = 10

/ / method 1

String str1 = num + ""; / / "" indicates an empty string, which is different from null in Java

/ / method 2

String str2 = String.valueOf (num)

2.1 convert strings to character arrays

Import java.util.Scanner

Import java.util.Arrays

Public class Main {

Public static void main (String [] args) {

Scanner scan = new Scanner (System.in)

String str = scan.nextLine ()

Char [] arr = str.toCharArray ()

For (int I = 0; I < arr.length; iTunes +) {

System.out.print (ARR [I] + "")

}

}

}

2.2 character array converted to string

/ / method 1

New String (arr)

/ / method 2

String.copyValueOf (arr)

3 give examples to illustrate

Description: write a function and enter a string to realize the inversion of the string. The code is as follows:

Import java.util.Scanner;public class Main {public static String my_reverse (String str) {int left = 0; int right = str.length ()-1; char [] arr = str.toCharArray (); while (left < right) {char tmp = 0; tmp = arr [left]; arr [left] = arr [right]; arr [right] = tmp Left++; right--;} return new String (arr);} public static void main (String [] args) {Scanner scan = new Scanner (System.in); String str = scan.next (); String ret = my_reverse (str); System.out.println (ret);}}

The results are as follows:

7. Fast input

It is slow to input with Scanner, so a new input and output function is introduced here. Compared with them, the advantage is that they are faster, but the disadvantage may be that they are too long and hard to type.

Import java.io.*;// omits Main public static void main (String [] args) throws IOException {BufferedReader bf = new BufferedReader (new InputStreamReader (System.in)); int a = Integer.parseInt (bf.readLine ()); System.out.println (a); double b = Double.parseDouble (bf.readLine ()); System.out.println (b) Char c = bf.readLine (). CharAt (0); System.out.println (c); char d = (char) bf.read (); / / is fine, but this cannot be used with multiple sets of inputs because it retains line breaks. System.out.println (d); System.out.println ("-"); String str = null; / / multiple input while ((str = bf.readLine ())! = null) {char ch = str.charAt (0); / / other similar System.out.println (ch);}}

The above is all the contents of the article "what are the inputs and outputs of Java common data types". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report