Get the App
SLTechnology News&Howtos  ›  Development  › 

Mutual conversion among String, Char and Int in Java

Shulou Source: shulou.com Published: 2022-06-03 12:49:56 09月25日 Update

This article mainly explains "the conversion between String, Char and Int in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the conversion between String, Char and Int in Java".

1. The mode of input from the terminal

How to use the Scanner class:

Scanner scanner = new Scanner (System.in)

Get the input stream from the terminal, and when the input stream is passed into the Scanner initialization object, it is passed as a parameter.

Several important methods of the Scanner class:

Next method (reads a character)

1. Be sure to read valid characters before you can end the input (if nothing is entered, the program will not end)

2. The next method automatically removes the white space encountered before the input valid characters. The white space that follows a valid character is used as a delimiter or Terminator only after a valid character is entered.

The next method complements to get a string with spaces.

Public static void main (String [] args) {Scanner sc = new Scanner (System.in); while (sc.hasNext ()) {String next = sc.next (); System.out.println (next);}}

NextLine method (reads a line of strings)

1. Take Enter as the Terminator, that is, the nextLine () method returns all the characters before entering enter.

2. You can get spaces.

Public static void main (String [] args) {Scanner sc = new Scanner (System.in); while (sc.hasNextLine ()) {String next = sc.nextLine (); System.out.println (next);}}

NextInt reads an integer. Sometimes you can get an integer directly from the terminal without the need to convert String to int. This reduces the running time of the program.

2.String and Char

String to Char []: using toCharArray ()

Scanner sc = new Scanner (System.in); String line = sc.nextLine (); char [] chars = line.toCharArray ()

String to single insert character: using charAt ()

Scanner sc = new Scanner (System.in); String line = sc.nextLine (); char charAt = line.charAt (2)

3.String and int

Convert int to String: using valueOf ()

Int n = 10 * string s = String.valueOf (10); System.out.println (s)

String to int: using Integer.parseInt (s)

Scanner sc = new Scanner (System.in); String line = sc.nextLine (); int I = Integer.parseInt (line); System.out.println (I)

4. Format of input

For example:

5,15 2,10

Import java.util.Scanner;/*** @ author zhoujian123@hotmail.com 2018-8-24 19:27*/public class ScannerTest {public static void main (String [] args) {Scanner sc = new Scanner (System.in); String line = sc.nextLine (); String [] split = line.split (""); for (int I = 0; I < split.length; ilevels +) {String s = split [I]; int i1 = Integer.parseInt (s.split (",") [0]) Int i2 = Integer.parseInt (s.split (",") [1]); System.out.println (i1 + "" + i2);}

Thank you for your reading, the above is the content of "the conversion between String, Char and Int in Java". After the study of this article, I believe you have a deeper understanding of the conversion between String, Char and Int in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Input character method between valid terminal learning content string integer program space white space Terminator important one line that is use method delimiter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn NVidia Redmi Linux Apple