線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1357
推到 Plurk!
推到 Facebook!

請問如何處理從其他檔案讀入的字串和數字?

尚未結案
Nicholas
一般會員


發表:1
回覆:1
積分:0
註冊:2004-04-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-10 14:54:43 IP:4.21.xxx.xxx 未訂閱
各位好,我是一個剛接觸 java的新手 我的問題,詳細情形是這個樣子的 先手動建立一個外部檔案 xxx.txt,內容中,每一列有1個人名及其年齡 ex. AAA 24 BBB 23 CCC 28 DDD 26 EEE 28 然後,寫1個 java的程式,將這個外部檔案的內容讀入 並根據讀入的內容,輸出年紀最大的那筆資料到螢幕上 如果同時有2個人以上年齡相同,則隨機列出其中一個人 例如 CCC、EEE兩個人都是最大的28歲,程式執行後隨機丟出 CCC或 EEE為結果 讀入外部檔案這部份我是會寫 不過後面的判別&輸出就不大靈光了 QQ 因為我不知道要怎麼去分開處理同一行的名字和年齡 可以請高手幫我一下忙嗎?Thanks
Nicholas
一般會員


發表:1
回覆:1
積分:0
註冊:2004-04-10

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-11 12:21:09 IP:4.21.xxx.xxx 未訂閱
目前我是這樣子寫 不過,跑出來的結果是錯誤的 沒辦法只單獨抓最大值列出來 會變成這樣子: The oldest person is AAA The oldest person is BBB The oldest person is CCC The oldest person is DDD The oldest person is EEE 現在還想不出來該怎麼改才對.... package chapt02; import java.io.*; import java.util.StringTokenizer; public class xxx { public static void main(String[] args) throws IOException { StringTokenizer tokenizer; String line, name, file="xxx.txt"; int age[] = new int[99]; try { FileReader fr = new FileReader("xxx.txt"); BufferedReader inFile = new BufferedReader(fr); line = inFile.readLine(); while(line != null) { tokenizer = new StringTokenizer(line); name = tokenizer.nextToken(); age = tokenizer.nextToken(); try { for(int i=1; i < a.length; i ) { a[i] = Integer.parseInt(age); } int max=0, maxVal = a[0]; for(int i=1; i < a.length; i ) { if (a[i] > maxVal) { max=i; maxVal=a[0]; } } System.out.println("The oldest person is " name); } catch (NumberFormatException exception) { System.out.println("Error in input. Line ignored:"); System.out.println(line); } line = inFile.readLine(); } inFile.close(); } catch (FileNotFoundException exception) { System.out.println("The file " file "was not found."); } catch (IOException exception) { System.out.println(exception); } } }
Frances3399
一般會員


發表:13
回覆:16
積分:5
註冊:2003-10-27

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-13 02:14:45 IP:163.18.xxx.xxx 未訂閱
package chapt02;
import java.io.*;
import java.util.StringTokenizer;
public class xxx
{
    public static void main(String[] args) throws IOException
    {
        StringTokenizer tokenizer;
        String line, name, file="xxx.txt";
        int age[] = new int[99];            try
        {
            FileReader fr = new FileReader("xxx.txt");
            BufferedReader inFile = new BufferedReader(fr);
            line = inFile.readLine();
            while(line != null)
            {
                tokenizer = new StringTokenizer(line);
                name = tokenizer.nextToken();
                age  = tokenizer.nextToken();                    try
                {
                    for(int i=1; i < a.length; i  )
                    {
                        a[i] = Integer.parseInt(age);
                    }
                    int max=0, maxVal = a[0];
                    for(int i=1; i < a.length; i  )
                    {
                        if (a[i] > maxVal)
                        {
                            max=i;
                            maxVal=a[0];
                        }
                    }
                    System.out.println("The oldest person is "   name);
                }
                catch (NumberFormatException exception)
                {
                    System.out.println("Error in input. Line ignored:");
                    System.out.println(line);
                }
                line = inFile.readLine();
            }
            inFile.close();
        }
        catch (FileNotFoundException exception)
        {
            System.out.println("The file "   file   "was not found.");
        }
        catch (IOException exception)
        {
            System.out.println(exception);
        }
    }
}
我只是幫你加入code....... 這好像是站規吧...貼程式碼一定要有[co de]跟[/co de] 發表人 - Frances3399 於 2004/04/13 02:16:23
ramb
一般會員


發表:1
回覆:4
積分:1
註冊:2004-04-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-04-14 18:16:09 IP:61.222.xxx.xxx 未訂閱
不太能理解你的程式結構說, 你只要一個一個讀進來, 然後一個一個跟目前最大值比不就好了. 裡面的 a[99] 有必要嘛? 把同一個age String 轉成 int 然後放到a[]陣列. 全部都是同一個值.......然後陣列全部相同的值再去跟最大值比......你只讀了一個啊  所以比一次就好囉.
import java.io.*;
import java.util.StringTokenizer;    public class Class {
    public static void main(String[] args) throws IOException {
        StringTokenizer tokenizer;
        String line,file="test.txt";
        String oldestMan="";
        int maxAge=-100;
        
        try {
            FileReader fr = new FileReader(file);
            BufferedReader inFile = new BufferedReader(fr);
            
            String name;
            int age;
            while( (line=inFile.readLine()) != null) {
                tokenizer = new StringTokenizer(line);
                name = tokenizer.nextToken();
                age = Integer.parseInt(tokenizer.nextToken());
                
                if (age > maxAge){
                    maxAge=age;
                    oldestMan=name;
                }
            }
            System.out.println("The oldest person is "   oldestMan " Age is " maxAge );
            inFile.close();
        }
        catch (FileNotFoundException exception) {
            System.out.println("The file "   file   "was not found.");
        }
        catch (NumberFormatException exception) {
            System.out.println("Error in input. Line ignored:");
        }
        catch (IOException exception) {
            System.out.println(exception);
        }
    }
}
系統時間:2024-05-09 9:48:39
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!