VagosM Δημοσ. 8 Ιανουαρίου 2012 Δημοσ. 8 Ιανουαρίου 2012 Για σας θέλω να κάνω μια άσκηση να διαβάζω txt και να το κάνω σε συγκεκριμένο φορματ σε αλλο αρχείο. Ο κώδικας που έγραψα το πρόβλημα είναι ότι μου βγάζει ένα eror και οτι δεν γράφει στο output. import java.io.*; public class Askisi1 { public static void main (String[] args){ Writer out = null; try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("Java1.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); String[] string =strLine.split(" "); String name=string[0]; String[] surename=string[1].split("-"); // at Askisi1.main(Askisi1.java:32) ??? String s =surename[0]; int am= Integer.parseInt(surename[1]); try { File file = new File("output.txt"); out = new BufferedWriter(new FileWriter(file)); out.write(name); // wtrite to out put format name - surename - code out.write(" "); out.write("-"); out.write(" "); out.write(s); out.write(" "); out.write("-"); out.write(" "); out.write(am); out.newLine(); //// grami } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } //Close the input stream and out put in.close(); out.close(); }catch(IOException e) { System.out.println("Couldn't read the cardList out"); e.printStackTrace(); } } }
xlomo Δημοσ. 8 Ιανουαρίου 2012 Δημοσ. 8 Ιανουαρίου 2012 Δεν δίνεις και το Java1.txt μπας και βγάλουμε άκρη ?
GKNSB Δημοσ. 8 Ιανουαρίου 2012 Δημοσ. 8 Ιανουαρίου 2012 Δεν μας λες ούτε τι error σου βγάζει, ούτε σε ποια γραμμή. Για κάποιο λόγο δεν βλέπει τη μέθοδο newLine. Δοκίμασε να αντικαταστήσεις τη γραμμή που χρησιμοποιείς την newLine με αυτό: >String newline = System.getProperty("line.separator"); out.write(newline);
VagosM Δημοσ. 8 Ιανουαρίου 2012 Μέλος Δημοσ. 8 Ιανουαρίου 2012 Συγνώμη που παρέλειψα να σας πω που βγάζει ερορ String[] surename=string[1].split("-"); // at Askisi1.main(Askisi1.java:32) ??? χτυπά λάθος το java1 έχει όνομα επιθ-κωδικο και θέλει να πάει ονομα - επιθ - κωδικό
xlomo Δημοσ. 8 Ιανουαρίου 2012 Δημοσ. 8 Ιανουαρίου 2012 Για να καταλάβω έχεις 1 αρχείο το οποίο έχει πολλές γραμμές πχ όνομα επιθ-κωδικο όνομα2 επιθ2-κωδικο2 όνομα3 επιθ3-κωδικο3 κτλ... και θες αυτο να το κάνεις ? όνομα-επιθ-κωδικο όνομα2-επιθ2-κωδικο2 όνομα3-επιθ3-κωδικο3 σωστά ? Με το int am = Integer.parseInt(surename[1]); τι προσπαθείς να κάνεις ?
GKNSB Δημοσ. 8 Ιανουαρίου 2012 Δημοσ. 8 Ιανουαρίου 2012 Υποθέτω να περνάει κάπως τον κωδικό, αλλά δεν έχει κάποιο νόημα αφού τον έχεις ήδη διαβάσει σαν String προηγουμένως. Όπως και να'χει πάρε τον κώδικα που δουλεύει. >/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Manos */ import java.io.*; public class Askisi1{ public static void main (String[] args){ Writer out = null; try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("Java1.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); String[] string =strLine.split(" "); String name=string[0]; String[] surename=string[1].split("-"); // at Askisi1.main(Askisi1.java:32) ??? String s =surename[0]; String am= surename[1]; try { File file = new File("output.txt"); out = new BufferedWriter(new FileWriter(file)); out.write(name); // wtrite to out put format name - surename - code out.write(" "); out.write("-"); out.write(" "); out.write(s); out.write(" "); out.write("-"); out.write(" "); out.write(am); String newline = System.getProperty("line.separator"); out.write(newline); //out.newLine(); //// grami } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } //Close the input stream and out put in.close(); out.close(); }catch(IOException e) { System.out.println("Couldn't read the cardList out"); e.printStackTrace(); } } }
VagosM Δημοσ. 8 Ιανουαρίου 2012 Μέλος Δημοσ. 8 Ιανουαρίου 2012 μου βγάζει πάλη το ίδιο μήνυμα Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Askisi1.main(Askisi1.java:31) Java Result: 1
xlomo Δημοσ. 8 Ιανουαρίου 2012 Δημοσ. 8 Ιανουαρίου 2012 κάνε μας ένα copy paste το περιεχόμενο του Java1.txt που χρησιμοποιείς
GKNSB Δημοσ. 8 Ιανουαρίου 2012 Δημοσ. 8 Ιανουαρίου 2012 Σου έχω επισυνάψει και αρχείο που είναι ακριβώς το input που περιγράφεις. Και σου δείχνω το output ότι είναι αυτό που ζητάς. Πως ακριβώς καταφέρνεις να μην παίζει ο κώδικας;
VagosM Δημοσ. 9 Ιανουαρίου 2012 Μέλος Δημοσ. 9 Ιανουαρίου 2012 Vagelis Adamidis-15428 Gianis Vretos-15923 Αυτά έχω μέσα στο txt Ευχαριστώ πολύ για την βοήθεια σας...
virxen75 Δημοσ. 9 Ιανουαρίου 2012 Δημοσ. 9 Ιανουαρίου 2012 Σου έχω επισυνάψει και αρχείο που είναι ακριβώς το input που περιγράφεις. Και σου δείχνω το output ότι είναι αυτό που ζητάς. Πως ακριβώς καταφέρνεις να μην παίζει ο κώδικας; έχεις καταλάβει ότι ο κώδικας που εστειλες σαν σωστό στην ουσία είναι λάθος? για να το διαπιστώσεις δοκίμασέ το με αρχείο που να έχει 2 και όχι 1 εγγραφές. εγώ πάντως θα το έγραφα κάπως έτσι > import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.Writer; public class Askisi1{ public static void main (String[] args){ Writer out = null; File file=null; FileInputStream fstream=null; String newline = System.getProperty("line.separator"); try{ fstream = new FileInputStream("Java1.txt"); file = new File("output.txt"); } catch (FileNotFoundException e) { System.out.println("error="+e); System.exit(1); } try{ out = new BufferedWriter(new FileWriter(file)); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine,name,s,am; while ((strLine = br.readLine()) != null) { System.out.println (strLine); String[] string =strLine.split(" "); if (string.length==2){ name=string[0]; String[] surname=string[1].split("-"); if (surname.length==2){ s =surname[0]; am= surname[1]; out.write(name); out.write(" "); out.write("-"); out.write(" "); out.write(s); out.write(" "); out.write("-"); out.write(" "); out.write(am); out.write(newline); } } } in.close(); out.close(); }catch (IOException e) { System.out.println("error="+e); } } }
GKNSB Δημοσ. 9 Ιανουαρίου 2012 Δημοσ. 9 Ιανουαρίου 2012 έχεις καταλάβει ότι ο κώδικας που εστειλες σαν σωστό στην ουσία είναι λάθος? για να το διαπιστώσεις δοκίμασέ το με αρχείο που να έχει 2 και όχι 1 εγγραφές. εγώ πάντως θα το έγραφα κάπως έτσι > import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.Writer; public class Askisi1{ public static void main (String[] args){ Writer out = null; File file=null; FileInputStream fstream=null; String newline = System.getProperty("line.separator"); try{ fstream = new FileInputStream("Java1.txt"); file = new File("output.txt"); } catch (FileNotFoundException e) { System.out.println("error="+e); System.exit(1); } try{ out = new BufferedWriter(new FileWriter(file)); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine,name,s,am; while ((strLine = br.readLine()) != null) { System.out.println (strLine); String[] string =strLine.split(" "); if (string.length==2){ name=string[0]; String[] surname=string[1].split("-"); if (surname.length==2){ s =surname[0]; am= surname[1]; out.write(name); out.write(" "); out.write("-"); out.write(" "); out.write(s); out.write(" "); out.write("-"); out.write(" "); out.write(am); out.write(newline); } } } in.close(); out.close(); }catch (IOException e) { System.out.println("error="+e); } } } Ναι έχεις δίκιο. Είναι κάτι που δε θα δούλευε έτσι κι αλλιώς αφού δεν έχει βάλει ο φίλος μας το append true. Πάντα για τον δικό του κώδικα μιλάω >/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Manos */ import java.io.*; public class Askisi1 { public static void main (String[] args){ Writer out = null; try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("Java1.txt"); // Get the object of DataInputStream DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { // Print the content on the console System.out.println (strLine); String[] string =strLine.split(" "); String name=string[0]; String[] surename=string[1].split("-"); // at Askisi1.main(Askisi1.java:32) ??? String s =surename[0]; String am= surename[1]; try { File file = new File("output.txt"); out = new BufferedWriter(new FileWriter(file,true)); //ayto edw to true dhladh out.write(name); // wtrite to out put format name - surename - code out.write(" "); out.write("-"); out.write(" "); out.write(s); out.write(" "); out.write("-"); out.write(" "); out.write(am); String newline = System.getProperty("line.separator"); out.write(newline); //out.newLine(); //// grami } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } } //Close the input stream and out put in.close(); out.close(); }catch(IOException e) { System.out.println("Couldn't read the cardList out"); e.printStackTrace(); } } }
Προτεινόμενες αναρτήσεις
Δημιουργήστε ένα λογαριασμό ή συνδεθείτε για να σχολιάσετε
Πρέπει να είστε μέλος για να αφήσετε σχόλιο
Δημιουργία λογαριασμού
Εγγραφείτε με νέο λογαριασμό στην κοινότητα μας. Είναι πανεύκολο!
Δημιουργία νέου λογαριασμούΣύνδεση
Έχετε ήδη λογαριασμό; Συνδεθείτε εδώ.
Συνδεθείτε τώρα