[問題] 如何把Strings裡面分成很多不同的東西
問題是, 我想把一個一串Strings加數字讓他變得不一樣
有四種條件:
The return value will be the ROT13 encoding of the input file. For our
purposes the ROT13 algorithm is defined as follows:
A) If the input is a lower case letter, the output is the lower case
letter plus 13 (with wrap-around, so 'n' becomes 'a')
B) If the input is a upper case letter, the output is the upper case
letter plus 13 (with wrap-around, so 'n' becomes 'a')
C) If the input is a number the output is the number plus 5 (with
wrap-around so '6' becomes '1')
D) If the input is neither letter nor number, the output is the same
as the input ('!' becomes '!' and ' ' becomes ' ')
也就是說:
假如我的文件, test.txt裡面的一串字是1234那就會變成5678, 然後
1a2a3a4a 就會變成 5n6n7n8n 因為a+13會變成n, 大寫跟小寫都會
我現在可以用Scanner跟Reader來讀取我的strings, 可是重點是我不會改變他們的數字或
是字母
問題1:
a+13會變成a13, 那要怎麼才能讓他跳13個字母
111我+5會變成545454...非常詭異
問題2:
因為我之前有學過C++, 我就想說把Strings當成Array of Chars可是似乎沒辦法
所以我就把他全部都寫到 char[] charArray = new char [encodedFile.length()]
然後利用.charAt(i) <== i 是用for loop
我現在所有的字母跟數字都在Array of Char裡面, 可是我要怎麼用if statement來表示
A. 如果碰到字母要跳13位
B. 如果碰到數字要+5
目前我A完全不會做, B我這樣寫, charArray[5] += 5是沒辦法這樣做的.
因為我似乎把Char當成Int來用, 所以...到底該怎麼辦
我在網上查了很多資料, 我有用Replace可是完全沒辦法用
我的Code在底下:
http://nopaste.csie.org/a4a9b
第一次寫Java, 買了Just Java2 可是沒有幫助我太多
因為實在寫不出來, 請大家麻煩指點指點
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 173.56.121.140
※ 編輯: BlackMatrix 來自: 173.56.121.140 (02/05 16:28)
※ 編輯: BlackMatrix 來自: 173.56.121.140 (02/05 16:37)
推
02/05 16:29, , 1F
02/05 16:29, 1F
→
02/05 16:29, , 2F
02/05 16:29, 2F
推
02/05 16:30, , 3F
02/05 16:30, 3F
→
02/05 16:31, , 4F
02/05 16:31, 4F
→
02/05 16:38, , 5F
02/05 16:38, 5F
喔喔, 似乎有進展了:
if (Character.isLetter(ch))
...
if (Character.isDigit(ch))
...
if (Character.isSpaceChar(ch))
※ 編輯: BlackMatrix 來自: 173.56.121.140 (02/05 16:46)
我現在是可以把數字分辨出來哩, 呼呼, 花了我好久的時間
※ 編輯: BlackMatrix 來自: 173.56.121.140 (02/05 16:47)
謝謝adrianshum大大的提示, 加快了我寫Code的速度
目前已經可以把全部的東西都做好除了wrap-around
http://nopaste.csie.org/d0aec
不過應該自己做得出來(希望)
※ 編輯: BlackMatrix 來自: 173.56.121.140 (02/05 17:09)
經過大家的幫忙, 我已經順利的把code做完哩
成品:http://nopaste.csie.org/29a17
謝謝大家, 謝謝
※ 編輯: BlackMatrix 來自: 173.56.121.140 (02/05 17:41)
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):