java READ FILE急問.........

2009-11-30 8:34 am
import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;


public class ReadF{

public static void main(String[] args) {


String fileName = "test.txt";


try {

// 1. create a bufferedReader for reading files

BufferedReader reader = new BufferedReader(new FileReader

(fileName));

// 2. read out content, print them out line by line

String line;

String [] letterList;

String[][] bookingStatus = new String[10][10];


int floor = 0;

while ((line = reader.readLine()) != null) {

letterList = line.split(" ");


for (int i = 0 ; i < letterList.length ; i++) {

bookingStatus[floor][i] = letterList[i];

}

floor++;

}

System.out.println("Booking Status of Room 001 is: " +

bookingStatus[0][0]);

System.out.println("Booking Status of Room 002 is: " +

bookingStatus[0][1]);

System.out.println("Booking Status of Room 003 is: " +

bookingStatus[0][2]);

System.out.println("Booking Status of Room 101 is: " +

bookingStatus[1][0]);

System.out.println("Booking Status of Room 102 is: " +

bookingStatus[1][1]);

System.out.println("Booking Status of Room 103 is: " +

bookingStatus[1][2]);


reader.close();


// 3. error handling

}
catch (IOException e) {

e.printStackTrace();

}

}


}


想問如果我想INPUT個ROOM NO,去CHECK個ROOM既STATUS,而唔係好係依家甘,要自己寫bookingStatus[1][1], 即係bookingStatus[floor][room], eg我INPUT201,佢會CHECK bookingStatus[1][0]個STATUS,甘我要點改呀??

回答 (1)

2009-11-30 6:11 pm
✔ 最佳答案
都好簡單姐
你將個 input 用簡單數學拆成 floor 同 room #
再代入個array index 咪得囉
試下啦


收錄日期: 2021-04-13 16:57:46
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20091130000051KK00057

檢視 Wayback Machine 備份