ch.toString()和new String(数组)

toString() 方法返回此对象本身(它已经是一个字符串).
Object 类的 toString( ) 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成.

new String(数组) 把数组转为字符串

package test;
import util.DBUtil;
import entity.Entity;
public class test1 {
	public static void main(String[] args) {
		char[] data = {'a', 'b', 'c'};
		String str1  =  data.toString();
		String str2 = new String(data);
		String str3 = str2.toString();
		System.out.println("str1 = "+str1);
		System.out.println("str2 = "+str2);
		System.out.println("str3 = "+str3);
	}
}
str1 = [C@139a55
str2 = abc
str3 = abc
public static GPA PersonalReader()throws IOException{ // Read credit information from a JSON file String file = "C:\\Users\\isabe\\Desktop\\code\\code\\src\\STUDENT\\1001.json"; try{ FileReader fileReader = new FileReader(file); Reader reader = new InputStreamReader(new FileInputStream(file),"Utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } reader.close(); fileReader.close(); File file1 = new File("C:\\Users\\isabe\\Desktop\\code\\code\\src\\credit.json"); FileReader fileReader1 = new FileReader(file1); Reader reader1 = new InputStreamReader(new FileInputStream(file1), "Utf-8"); int ch1 = 0; StringBuffer sb2 = new StringBuffer(); while ((ch1 = reader1.read()) != -1) { sb2.append((char) ch1); } fileReader1.close(); reader1.close(); String creditStr = sb2.toString(); // Parse JSON strings using FastJSON library and calculate weighted GPA JSONObject jsonObject2 = JSON.parseObject(creditStr); double weighteGpa = 0.0; double Cre = 0.0; String jsonStr = sb.toString(); JSONObject jsonObject = JSON.parseObject(jsonStr); JSONArray subjectsList = jsonObject.getJSONArray("subjectsList"); Double avgGrade = 0.0; for (int i = 0; i < subjectsList.size(); i++) { JSONObject subject = subjectsList.getJSONObject(i); String subjectName = subject.getString("subject"); JSONObject jsonObject1 = jsonObject2.getJSONObject(subjectName); Object o = jsonObject1.getDouble("credit"); Double grade = subject.getDouble("grade"); weighteGpa += Double.parseDouble(o.toString())*grade; Cre += Double.parseDouble(o.toString()); avgGrade += grade; } avgGrade = avgGrade / subjectsList.size(); System.out.println(avgGrade); // Return GPA object return new GPA(avgGrade+"",(weighteGpa/(10*Cre)-5)+""); } catch (IOException e) { e.printStackTrace(); return null; } }使用tdd修改这段代码
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值