2016-03-24 25 views
0

bir output.txt dosyasına kullanmayı denedim Uygulamamı bir output.txt dosyasına yazdırarak test etmeye çalışıyorum. Şimdiden dört onur öğrencisi ve en az ikisi de aynı not ortalaması 3.9 olan ve üstelik onur öğrencileri olmayan bir input.txt dosyası var. Sonuçlar çıktı.txt dosyasına gönderilmelidir. Output.txt dosyası şunları içermelidir: 1) Öğrenciler en iyi 2) En iyi öğrenci 3) Listedeki onur öğrenci sayısı 4) Öğrencileri onurlandırmak Oluşturduğum input.txt dosyası aşağıdakileri içerir: sipariş) soyadları, ad adları, kimlik, not ortalaması ve yıl.Bir input.txt dosyasını Java

TestStudents sınıfı, input.txt dosyasını yazdırır. Ancak, yukarıda belirtilen output.txt dosyasını yazdırmak için input.txt dosyasını kullanmak için ona ihtiyacım var. Çok teşekkür ederim.

Öğrenci sınıfı -

public class Student 
{ 
    String lastName, firstName, id; 
    double gpa; 
    int year; 

    public Student (String lastName, String firstName, String id, 
        double gpa, int year) 
    { 
     this.lastName = lastName; 
     this.firstName = firstName; 
     this.id = id; 
     this.gpa = gpa; 
     this.year = year; 
    } 

    public String toString() 
    { 
     return this.lastName + ", " + this.firstName + ": " + this.id + " " 
       + this.gpa + " " + this.year; 
    } 

    public double getGPA() 
    { 
     return gpa; 
    } 

    public boolean isBetter (Student s) 
    { 
     return (this.gpa > ((Student)s).getGPA()); 
    } 

    public boolean isHonors() 
    { 
     if (this.gpa >= 3.5) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 
} 

CS152 sınıfı -

import java.util.*; 
import java.io.*; 
public class CS152 
{ 
    public static final int MAXSIZE = 22; 
    private static int size = 0; 

    public static Student[] createList (Scanner scan) throws IOException 
    {  
     Student[] list = new Student [MAXSIZE]; 
     return populateList (list, scan); 
    } 

    private static Student[] populateList (Student[] list, Scanner scan) 
    { 
     Student s; 
     if (size < MAXSIZE && scan.hasNext()) 
     { 
      s = new Student (scan.next(), scan.next(), scan.next(), 
          scan.nextDouble(), scan.nextInt()); 
      list[size] = s; 
      size++; 
      System.out.println (s); 
      return populateList (list, scan); 
     } 
     else 
     { 
      return list; 
     } 
    } 

    public static int getSize() 
    { 
     return size; 
    } 

    // Returns String of all students. Variable n is actual size of the list. 
    // Assume that n is positive. Recursive code. 
    public static String toString (Student[] list, int n) 
    { 
     String s = " "; 
     if (n == 1) 
     { 
      return s += list[0]; 
     } 
     else 
     { 
      s += list[n].toString() + "\n"; 
      s += "\n"; 
     } 
     return s + toString (list, n - 1); 
    } 

    // Returns the best student. Must use method isBetter in the code. 
    // Variable n is actual size of the list. Assume that n is positive. 
    public static Student findBestStudent (Student[] list, int n) 
    { 
     if (n == 1) 
     { 
      return list[0]; 
     } 
     else if (list[n].isBetter (list[n - 1])) 
     { 
      return list[n]; 
     } 
     else 
     { 
      return findBestStudent (list, n - 1); 
     }   
    } 

    // Returns the number of honor students in the list. 
    // Must call the method isHonors(). Variable n is actual size of the list. 
    // Assume that n is positive. 
    public static int countHonors (Student[] list, int n) 
    { 
     if (n == 0) 
     { 
      return 0; 
     } 
     else if (list[n].isHonors()) 
     { 
      return 1 + countHonors (list, n - 1); 
     } 
     else 
     { 
      return countHonors (list, n - 1); 
     } 
    } 

    static ArrayList<Student> studentsList = new ArrayList<Student>(); 
    public static ArrayList <Student> honorsStuds (Student[] list, int n) 
    { 
     if (n == 0) 
     { 
      return studentsList; 
     } 
     else 
     { 
      boolean currentIsHonors = list[n - 1].isHonors(); 
      if (currentIsHonors) 
      { 
       studentsList.add(list[n - 1]); 
       return honorsStuds (list, n - 1); 
      } 
      else 
      { 
       return honorsStuds (list, n - 1); 
      } 
     } 
    } 
} 

TestStudents sınıfı -

import java.io.*; 
import java.util.*; 
public class TestStudents 
{ 
    public static void main (String[] args) throws IOException 
    { 
     File input = new File ("input.txt"); 
     Scanner scan = new Scanner (input); 
     Student[] studentArray = CS152.createList (scan); 
    } 
} 

I TestStudents sınıfına FileWriter dahil. Tüm öğrencilerin bir listesi şimdi görüntülenir. FindBestStudent, countHonors ve honorsStuds yöntemlerini çağırmaya ve bunları TestStudents uygulamasına çağırmaya çalışırken hala zorlanıyorum. İşte revize TestStudents sınıftır:

TestStudents sınıfı -

import java.io.*; 
import java.util.*; 
public class TestStudents 
{ 
    public static void main (String[] args) throws IOException 
    { 
     File input = new File ("input.txt"); 
     Scanner scan = new Scanner (input); 
     System.out.println ("All students: "); 
     Student[] studentArray = CS152.createList (scan); 


     File output = new File ("output.txt"); 
     FileWriter fWriter = new FileWriter (output); 
     PrintWriter pWriter = new PrintWriter (fWriter); 
     pWriter.println (input); 
     pWriter.close();  
    } 
} 
+0

'statik Öğrenci [] populateList (Öğrenci [] listesi, Tarayıcı taraması) 'tekrarlı olarak adlandırılıyor - her öğrenci için bir seviye - kesinlikle hiçbir sebep yok. Eğer istemiyorsanız (/ * yapılmadı * /) {/ * ... */populateList()} ', sadece bir süre (/ * bitmedi * /) {...}' loop . – AJNeufeld

+0

En iyi öğrenciyi bulmak için kodu zaten yazdınız.Sadece işlevleri çağırmanız ve sonucu yazdırmanız gerekir. – AJNeufeld

+0

Bu benim bir sorunum var. Tüm öğrencileri bastım. Şimdi findBestStudent, countHonors ve honorsStuds yöntemlerini yazdırmak zorundayım. Ne yazık ki, bunları çıktı.txt dosyasında yazdıramam. – Voraptustenellus

cevap

0

bir dosyaya yazmak için, bir FileWriter gerekir. Bir FileWriter ve Try-with-resources kullanma

, kullanım şöyle görünecektir: Bir deneyin-with-kaynaklarını kullanmak yoksa

try(FileWriter w = new FileWriter(new File("output.txt"))) { 
    w.append("Some string"); 
} catch (IOException ex) { 
    Logger.getLogger(Output.class.getName()).log(Level.SEVERE, null, ex); 
} 

, emin kaynaklar kaçak yok yapmak için close()Writer emin olun . Aslında, Scanner'u da kapatmamanız gerekir, çünkü kapalı olarak bıraktığınızda kaynak sızıntısı olacaktır.

Gelecekte, ask one question per post.

Öğrencilerinize erişmek için, bunları yalnızca diziden okumalısınız.

System.out.println(studentArray[0].getGPA()); // prints the GPA of the first student 
for (int i=0; i<CS152.getSize(); i++) { 
    System.out.println(studentArray[i]); // prints every Student 
} 

[[doldurulur kaç söylüyorum CS152.class ile sonunda null elemanları ile uzun dizi sahip bu tasarım kötü tasarım olduğunu unutmayın. Okuma prosedürünü, kendi uzunluğunu yöneten bir List<Student> döndürürüm. Sınıfın adı verilen CS152 ise, bu muhtemelen size CS-152 öğretmeni tarafından verilmiş veya daha önce yapılmış, bu yüzden sahip olduğunuzla çalışacağım.]]

+0

FileWriter'i TestStudents sınıfına dahil ettim. Tüm öğrencilerin bir listesi şimdi görüntülenir. FindBestStudent, countHonors ve honorsStuds yöntemlerini çağırmaya ve bunları TestStudents uygulamasına çağırmaya çalışırken hala zorlanıyorum. – Voraptustenellus

+0

Evet, halihazırda verilen ve doldurulan genel statik Öğrenci [] createList (Tarayıcı taraması) ve özel statik Öğrenci [] populateList (Öğrenci [] listesi, Tarayıcı taraması) yöntemleri. Gönderdiğiniz kodları öğrencilerden okuyan kod çalışır. Listeyi input.txt'den zaten almıştım. Göremediğim tek bölüm, daha önce bahsettiğim, bunları output.txt içine dahil eden yöntemlerdir. Önceki bir programda input.txt kullandım. Bununla birlikte, metotları bana yeni olarak output.txt içine aktarıyorum. – Voraptustenellus

İlgili konular