好吧,基本上,我想寫一個簡單的程序。其目的是讀取一個文件(該文件是一組401個401表格),然後將數據寫入數組並將數組打印到控制檯。這將不再用於查找特定的數字,所以就像我將編寫代碼來查找數組中的最高和最低數字一樣。我現在還不知道如何,但是我還沒有完成該程序的工作。每次我運行這個程序,我得到的確切錯誤如下。異常在線程「主要」java.lang.NumberFormatException:對於輸入字符串
異常在線程 「主」 java.lang.NumberFormatException:對於輸入字符串:「363.0 360.0 354.0 349.0 343.0 339.0 333.0 319.0 324.0 325.0 324.0 317.0 317.0 322.0 322.0 320.0 315.0 314.0 313.0 311.0 309.0 308.0 315.0 320.0 326.0 332.0 336.0 341.0 346.0 350.0 353.0 353.0 352.0 351.0 352.0 353.0 351.0 350.0 346.0 339.0 338.0 349.0 354.0 357.0 360.0 366.0 371.0 377.0 383.0 387.0 393.0 398.0 402.0 409.0 414.0 419.0 425.0 432.0 439.0 442.0 444.0 446.0 448.0 450.0 453.0 457.0 460.0 460.0 462.0 464.0 465.0 466.0 467.0 467.0 469.0 470.0 471.0 470.0 470.0 470.0 470.0 477.0 479.0 480.0 486.0 490.0 492.0 495.0 497.0 496.0 499.0 499.0 500.0 500.0 499.0 499.0 499.0 500.0 500.0 497.0 494.0 491.0 488.0 487.0 485.0 490.0 490.0 488.0 484.0 482.0 481.0 478.0 474.0 471.0 466.0 461.0 454.0 449.0 443.0 436.0 432.0 422.0 410.0 397.0 389.0 381.0 375.0 365.0 340.0 318.0 289.0 283.0 276.0 2 66.0 255.0 241.0 235.0 233.0 234.0 235.0 235.0 239.0 257.0 281.0 302.0 321.0 330.0 337.0 354.0 373.0 383.0 389.0 401.0 410.0 413.0 403.0 389.0 381.0 373.0 363.0 351.0 343.0 335.0 328.0 322.0 314.0 306.0 298.0 293.0 285.0 275.0 270.0 276.0 283.0 286.0 286.0 285.0 283.0 283.0 283.0 279.0 274.0 270.0 264.0 255.0 245.0 239.0 238.0 238.0 239.0 240.0 249.0 256.0 262.0 268.0 272.0 275.0 276.0 275.0 273.0 271.0 269.0 269.0 271.0 274.0 276.0 279.0 283.0 289.0 293.0 294.0 295.0 295.0 297.0 307.0 312.0 314.0 314.0 314.0 313.0 312.0 310.0 307.0 305.0 304.0 303.0 304.0 299.0 291.0 308.0 325.0 335.0 344.0 351.0 358.0 361.0 366.0 370.0 379.0 382.0 386.0 389.0 391.0 391.0 390.0 390.0 393.0 395.0 397.0 402.0 408.0 412.0 413.0 414.0 415.0 415.0 416.0 416.0 416.0 415.0 415.0 414.0 413.0 411.0 410.0 408.0 407.0 407.0 406.0 404.0 402.0 400.0 394.0 375.0 379.0 390.0 394.0 397.0 397.0 397.0 397.0 402.0 405.0 407.0 407.0 412.0 417.0 423.0 428.0 441.0 448.0 451.0 453.0 457.0 458.0 459.0 460.0 460.0 460.0 458.0 456.0 455.0 454.0 455.0 455.0 455.0 454.0 452.0 452.0 451.0 450.0 450.0 450.0 450.0 447.0 442.0 440.0 437.0 436.0 436.0 437.0 437.0 437.0 436.0 434.0 429.0 427.0 425.0 422.0 412.0 410.0 415.0 415.0 414.0 413.0 413.0 414.0 411.0 410.0 409.0 408.0 406.0 404.0 402.0 400.0 398.0 395.0 393.0 392.0 389.0 387.0 385.0 384.0 380.0 377.0 376.0 375.0 374.0 372.0 370.0 369.0 367.0 365.0 362.0 355.0 346.0 340.0 334.0 331.0 331.0 330.0 327.0 323.0 322.0 319.0 316.0 315.0 310.0 297.0 289.0 280.0 274.0 274.0 282.0 301.0 320.0 328.0 344.0 352.0 351.0 350.0 346.0 341.0 338.0 333.0 329.0 323.0 318.0 312.0 308.0 304.0" 在sun.misc.FloatingDecimal.readJavaFormatString(來源不明) 在java.lang.Double.parseDouble(來源不明) 在MultiArray.main(MultiArray.java:19)
現在,這裏是合德爲我的節目。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException; //importing tools
public class MultiArray {
public static void main(String[] args)throws IOException {
//variables
int rows = 401;
int columns = 401;
String file = "dmt.asc";
double dmtData[][] = new double[rows][columns]; //array
BufferedReader Reader = new BufferedReader(new FileReader(file)); //read the file
//split the numbers and write to array
for(int i = 0; i < rows; i++){
String rowArray [] = Reader.readLine().split("\t");
for(int j = 0; j < columns; j++){
dmtData[i][j] = Double.parseDouble(rowArray[j]);
}
}
Reader.close(); //close the reader and the file
//print out the array
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.println(dmtData[i][j]);
}
}
//code here to find highest number
System.out.println("The highest peak in this area is: ");
//code here to find lowest number
System.out.println("The lowest dip in this area is: ");
}
}
感謝您的幫助,我保證一旦我瞭解java我會盡我所能回答就到這裏:)
該文件中的數字只有1空格分隔,所以在拆分,我改變了代碼(「\ t)只是爲了(」「),它的工作非常感謝,真的很感謝它! – Daniel
好吧,所以在這個工作之後,我開始尋找數據數組中的最高和最低值。然而,現在我無法得到這個工作xD我添加了導入java.util.Arrays,我也嘗試添加java.util。*。之後,我嘗試使用的代碼如下: – Daniel
Arrays.sort(dmtData); System.out.println(「該區域中的最高峯是:」+ dmtData [dmtData.length-1]); System.out.println(「該區域的最低下標爲:」+ dmtData [0]); – Daniel