前言:
眼前同学们对“java中scanner用法例题”大概比较注重,各位老铁们都需要了解一些“java中scanner用法例题”的相关知识。那么小编在网络上收集了一些关于“java中scanner用法例题””的相关文章,希望兄弟们能喜欢,各位老铁们快快来学习一下吧!在昨天题目基础上做挑战问题:
在金额上增加一个$符号 和逗号。- 这个在展示函数上直接增加即可。以金额高低来做排序。重写程序,主要用csv 的解析库来实现解析效果。
挑战2两个求解思路,
方法1 : 建设2个字典,以Salary 为 key, 以First 和 Last 为value。 这样做排序的时候,先对Salary list 做排序, 然后基于这两个字典做查询即可。
方法2: 把Salary 的 list 中的元素index 单独抽出来,在做排序的时候,记录原始index的最后位置,再根据这个位置在list 里寻找数据。--这个之前挑战已经做过了,有demo。
方法3: 把这些内容存到数据库里,做一条查询即可,再做展示。
挑战3的代码如下:
python 版本:
import csvclass Parsedatafile: originallist = [] firstcolumn = [] seconcolumn = [] thirdcolumn = [] def readdatafile(this): # 从指定的文件中读取数据,并读入到一个列表中 filename = input("Please provde the name of the file from which you want open") with open(filename, "r") as f: reader = csv.reader(f) for line in reader: this.originallist.append(line) f.close() print("this is the list we get from the file {0}".format(this.originallist)) def processlist(this): # 对列表做格式化,并生成对应的三个队列 for i in this.originallist: this.firstcolumn.append(i[0]) this.seconcolumn.append(i[1]) this.thirdcolumn.append(i[2]) print("This is the firstcolumn list: {0}".format(this.firstcolumn)) print("This is the secondcolumn list: {0}".format(this.seconcolumn)) print("This is the thirdcolumn list: {0}".format(this.thirdcolumn)) def displaylist(this): # 对处理后的队列操作,逐行做展示 # 找到每个列表里面最宽的元素 col1length = 0 col2length = 0 col3length = 0 for i in this.firstcolumn: if len(i) > col1length: col1length = len(i) for i in this.seconcolumn: if len(i) > col2length: col2length = len(i) for i in this.thirdcolumn: if len(i) > col3length: col3length = len(i) space = " " line1 = "Last" + space * (col1length - 4 + 1) + "First" + space * (col2length - 5 + 1) + "Salary" + space * ( col3length - 6 + 1) line2 = len(line1) * "-" print(line1) print(line2) for i in range(0, len(this.firstcolumn)): print(this.firstcolumn[i].ljust(col1length + 1), end="") print(this.seconcolumn[i].ljust(col2length + 1), end="") print(this.thirdcolumn[i])lesson42 = Parsedatafile()lesson42.readdatafile()lesson42.processlist()lesson42.displaylist()
python csv 函数的详细用法可以参考网文,
这里主要是把每一行的数据解析成了一个队列。
看到下面运行效果是一样的。
Java 版本:
使用csvReader来解析CSV 文件,效果类似,也是解析成了一个队列,从队列里面直接读取元素即可。
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.InputStreamReader;import java.nio.Buffer;import java.util.ArrayList;import java.util.Scanner;import com.opencsv.CSVReader;public class ParseFilev2{ private ArrayList<String> originallist = new ArrayList<>(); private ArrayList<String> firstcolumn = new ArrayList<>(); private ArrayList<String> secondcolumn = new ArrayList<>(); private ArrayList<String> thirdcolumn = new ArrayList<>(); private Scanner sc = new Scanner(System.in); void readdatafile(){ System.out.println("Please provde the name of the file from which you want open"); String filename = sc.nextLine(); try{ String[] nextLine; CSVReader csvReader = new CSVReader(new FileReader(filename)); while((nextLine=csvReader.readNext())!= null) { firstcolumn.add(nextLine[0]); secondcolumn.add(nextLine[1]); thirdcolumn.add(nextLine[2]); originallist.add(nextLine[0]+","+nextLine[1]+","+nextLine[2]); } }catch(IOException e){ } System.out.println("This is the original list we get from the file"+ originallist.toString()); } //从指定的文件中读取数据,并读入到一个列表中 void displaylist(){ //对处理后的队列操作,逐行做展示 //找到每个列表里面最宽的元素 int fcmlength = 0; int scmlength = 0; int tcmlength = 0; for (int i = 0 ; i< firstcolumn.size();i++){ if (fcmlength< firstcolumn.get(i).length()) {fcmlength = firstcolumn.get(i).length();} if (scmlength< secondcolumn.get(i).length()) {scmlength = secondcolumn.get(i).length();} if (tcmlength< thirdcolumn.get(i).length()) {tcmlength = thirdcolumn.get(i).length();} } fcmlength = fcmlength + 1; scmlength = scmlength + 1; tcmlength = tcmlength + 1; System.out.printf("%-"+fcmlength+"s","Last"); System.out.printf("%-"+scmlength+"s","First"); System.out.printf("%-"+tcmlength+"s\n","Salary"); for(int j=0;j< fcmlength+scmlength+tcmlength;j++) { System.out.printf("-"); } System.out.println(); for( int k =0 ;k < firstcolumn.size();k++) { System.out.printf("%-"+fcmlength+"s", firstcolumn.get(k)); System.out.printf("%-"+scmlength+"s", secondcolumn.get(k)); System.out.printf("%-"+tcmlength+"s", thirdcolumn.get(k)); System.out.println(); } } public static void main(String[] args) { ParseFilev2 lesson42 = new ParseFilev2(); lesson42.readdatafile(); lesson42.displaylist(); }}
效果图:
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #java中scanner用法例题