In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.