JAVA随笔(二)
做网易云课堂的java视频的课后题的笔记,主要是关于字符串操作的,还是挺有收获的。题目是:http://mooc.study.163.com/learn/ZJU-1000002014?tid=1000002010#/learn/ojhw?id=1000060003
第一题中主要是关于next()和nextLine()的区别:一个是以空格符读,一个是一次读一行。还有就是查看字符串结尾的方式。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
String word = null; boolean end = false; do { word = in.next(); end = word.endsWith("."); // System.out.print(end ? (word.length() == 1 ? "" : word.length() - 1): (word.length() + " ")); }while(!end);*/
第二题用到了更多关于字符串的操作,比如截取一段字符;将数字字符串转为整数;在有多个相同字符时,怎么取后面字符的位置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
String line=null,hhmmss=null; while(!(line=in.next()).equals("END")){ if(!line.contains("$GPRMC")) continue; int val = Integer.parseInt(line.substring(line.indexOf("*")+1),16); char n = line.charAt(1); for(int i = 2;i<line.length()-3;i++) { n^=line.charAt(i); } if(n == val) { int firstComma = line.indexOf(','); int secondComma = line.indexOf(',',firstComma+1); int thirdComma = line.indexOf(',',secondComma+1); if("A".equals(line.substring(secondComma+1, thirdComma))) { hhmmss=line.substring(firstComma + 1, secondComma); } if(hhmmss!=null) { int h =0,m=0,s=0; h=Integer.parseInt(hhmmss.substring(0, 1))+8; h = h>24?h-24:h; m=Integer.parseInt(hhmmss.substring(2,4)); s = Integer.parseInt(hhmmss.substring(4, 6)); System.out.print((h >= 10 ? h : "0" + h) + ":" + (m >= 10 ? m : "0" + m) + ":" + (s >= 10 ? s : "0" + s)); } } }