Java Scanner 用法總結

2021-08-07 21:57:25 字數 659 閱讀 2548

######讀入資料流#########

scanner sc = new  scanner(systrm.io) //從控制台輸入

scanner sc = new scanner(paths.get("filename")) //獲取檔案path

scanner sc = new scanner(file f) //讀入檔案

scanner sc = new scanner(string str) //讀入給定字串

#####寫入檔案#######

printwriter out  = new printwriter(「filename」) //構造乙個寫入檔案的printerwriter ,之後out就可以像system.out一樣使用了。

######scanner常用方法#######

sc.nextline() //讀取下一行

sc.next() //下乙個單詞 (空格為分割符)

sc.nextint() //return int

sc.nextdouble() //reutrn double //輸入必須為double

sc.hasnext() //boolean

sc.hasnextint() //boolean

sc.hasnextdouble //boolean

java scanner相關問題

前段時間做資料結構課設,在控制台中進行一些輸入,其中用到了不少scanner的方法,之前沒怎麼注意,導致出現了好多問題,現簡單總結以下 一 nextint,nextline,next三者區別 nextint it only reads the int value,nextint places the...

static用法總結

static static關鍵字是c,c 中都存在的關鍵字,它主要有三種使用方式,其中前兩種只指在c語言中使用,第三種在c 中使用 c,c 中具體細微操作不盡相同,本文以c 為準 1 區域性靜態變數 2 外部靜態變數 函式 3 靜態資料成員 成員函式 下面就這三種使用方式及注意事項分別說明 一 區域...

sizeof用法總結

在vc中,sizeof有著許多的用法,而且很容易引起一些錯誤。下面根據sizeof後面的 引數對sizeof的用法做個總結。a 引數為資料型別或者為一般變數。例如sizeof int sizeof long 等等。這種情 況要注意的是不同系統系統或者不同編譯器得到的結果可能是不同的。例如int型別在...