導航:首頁 > 編程語言 > java控制台讀字元串

java控制台讀字元串

發布時間:2023-02-10 14:26:36

java:一個java程序中,先在控制台輸出一個字元串,再通過控制台獲取該字元串

publicclassTest3{ publicStringget(Stringo){ returno; } publicstaticvoidmain(String[]args){ Test3t=newTest3(); Strings="abc"; System.out.println(s); t.get(s); System.out.print(t); }}

⑵ 如何從java控制台輸入帶空格的字元串

  1. /**按行讀取*/import java.io.*;public class SystemInTest {
    public static void main(String[] args) {BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));String str;try {str = stdin.readLine();System.out.println(str);} catch (IOException e) {e.printStackTrace();}}}

  2. Java控制台輸入帶空格的字元串,讀取的時候只要按照行來讀取,就可以獲取到輸入的空格,下面是示例:/**按行讀取 */import java.io.*;public class SystemInTest {public static void main(String[] args) { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));//建立從控制台輸入的類 String str; try { str = stdin.readLine();//讀取一行 System.out.println(str); } catch (IOException e) { e.printStackTrace(); } }}

  3. importjava.util.Scanner;
  4. publicclassencode2013
  5. {
  6. publicstaticvoidmain(String[] args){
  7. String inString;
  8. String delimiter;
  9. Scanner scan =newScanner(System.in);
  10. inString=scan.nextLine();
  11. delimiter=scan.next();
  12. inString= inString.replace(" ",delimiter);
  13. System.out.println(inString);
  14. }

⑶ java怎麼獲取控制台輸入的字元串

1.比較傳統的方法,得到字元串後要另行判斷、轉換
package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainRun {
/**
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
System.out.print("請輸入一個字元串:");
String str = strin.readLine();
System.out.println("第一個:"+str);
System.out.println("請輸入第二個字元串:");
String str2 = strin.readLine();
System.out.println("第2個:"+str2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.JDK5以後引入了 Java.util.Scanner;來處理控制台輸入並得到響應的類型數據
package com.test;
import java.util.Scanner;
public class MainRun {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("輸入第一個boolean值(true/false):");
if(sc.nextBoolean()){
System.out.println("輸入布爾:真的");
}else{
System.out.println("輸入布爾:假的");
}
System.out.println("輸入第一個數字:");
System.out.println("輸入數字:"+sc.nextInt());
System.out.println("輸入一個字元串:");
System.out.println("輸入字元串:"+sc.next());
System.out.println("輸入一個長整型:");
System.out.println("輸入長整型:"+sc.nextLong());
}
}

⑷ java :從控制台輸入一個字元串,驗證是不是郵箱

  1. 自定了一個合法郵箱規則,希望能幫助理解。代碼如下:import java.util.Scanner;public class Test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入一個郵箱地址:");String mail = sc.nextLine();/* 設定郵箱地址的合法規則,合法郵箱地址要求如下: (1)字元必須是英文或數字開始 (2)必須包含一個@ (3)@符號在. 符號前面 (4)以英文或數字結尾 */ //設置一個正則表達式 String reg = "[\w]+@[\w]+.[\w]+"; //告知此字元串是否匹配給定的正則表達式。if(mail.matches(reg)) {System.out.println("郵箱地址合法!");}else {System.out.println("郵箱地址不合法!");}}}這里主要是採用正則表達式的方式。

  2. 關於正則表達式,查看Pattern類和Matcher類。樓主可以可以到網上查看下相關資料。解釋下上面的正則表達式String reg = "[\w]+@[\w]+.[\w]+";w 表示單詞字元:[a-zA-Z_0-9],上面是兩個反斜桿是因為反斜桿是轉義字元 +號表示:出現一次或多次 ,所以[\w]+意思就是一到多個單詞字元(英文或數字)@ :直接表示@字元.:表示點字元綜上所述。String reg = "[\w]+@[\w]+.[\w]+";的意思就是 :一到多個字元 + @ + 一到多個字元 + 點 + 一到多個字元。正則表達式使用的好。

  3. 合法E-mail地址: 1. 必須包含一個並且只有一個符號「@」 2. 第一個字元不得是「@」或者「.」 3. 不允許出現「@.」或者.@ 4. 結尾不得是字元「@」或者「.」 5. 允許「@」前的字元中出現「+」 6. 不允許「+」在最前面,或者「+@」

⑸ java控制台讀取所有行後,如何將得到的字元串數值相加啊 每行開頭是整數,後面全是空白

StringBuilderstringBuilder=newStringBuilder();
stringBuilder.append(element);

將讀取的數據賦值回append();最後答stringBuilder.toString()

⑹ java中DataInputStream如何讀取我們控制台的字元串

java中DataInputStream使用構造參數,使用bufferreader,輸入參數system.in就可以讀取控制台的字元串,如下代碼:

importjava.io.BufferedInputStream;
importjava.io.DataInputStream;
importjava.io.IOException;

publicclassceshi{
publicstaticvoidmain(String[]args){

DataInputStreamin=newDataInputStream(newBufferedInputStream(
System.in));//新建DateInputSteam,接收控制台的信息
Strings;
try{
while((s=in.readLine()).length()!=0)
System.out.println(s);//讀取控制台信息,並列印出來
//
}catch(IOExceptione){
e.printStackTrace();
}

}

}

運行結果如下:

⑺ java在控制台上顯示字元串的語句是什麼

顯示字元串的語句是:
public
class
Test{
String
str
=readString("請輸入字元串:");
System.out.println("輸入的是:"+str);
}
private
static
String
readString(String
p){
BufferedReader
br
=
new
BufferedReader(new
InputStreamReader(System.in));
String
str
=null;
try{
System.out.print(p);
str
=br.readLine();
}catch(IOException
e){}
return
str;
}

⑻ java中怎麼在控制台輸入字元串

工具:復

eclipse

方法如制下:

輸入代碼如下:

importjava.util.Scanner;

publicclassHelloWorld{

publicstaticvoidmain(String[]args){
System.out.println("控制台輸入字元串開始");
Scannerinput=newScanner(System.in);
Stringinstr=input.nextLine();
System.out.println("控制台輸入字元串結束");
}

}

效果圖

⑼ java中如何從控制台輸入一個字元串並進行排序

BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String str = read.readLine();//這兩句用於讀取從控制台輸入的值,讀取的為字元串,排序前需將字元串轉換為數字類型,排序就簡單了,自己先思考思考

閱讀全文

與java控制台讀字元串相關的資料

熱點內容
如何發視頻到騰訊視頻文件夾里 瀏覽:510
微信打開cad外部文件找不到 瀏覽:882
平板可以打開各種文件後綴的軟體 瀏覽:531
蘋果微信文件怎麼全選 瀏覽:749
手機里之前打開過的文件在哪裡找 瀏覽:685
cad能看word文件嗎 瀏覽:719
12306火車票系統後台資料庫 瀏覽:570
js翻譯德語 瀏覽:33
從哪裡可以下載a股的數據 瀏覽:437
邏輯文件名和物理文件名關系 瀏覽:66
怎麼查一個網站的外鏈 瀏覽:675
linux下db2安裝時版本選擇 瀏覽:738
匯編編程用哪個軟體 瀏覽:486
仙樂下的歌在文件管理哪裡 瀏覽:477
115網盤解析工具2014 瀏覽:371
內圓弧銑刀怎麼編程 瀏覽:410
記事本文件轉word格式對齊 瀏覽:300
excel刪除恢復文件 瀏覽:290
三星s4怎麼切換3g網路 瀏覽:994
什麼是網站維護 瀏覽:314

友情鏈接