Thursday 15 August 2013

Read specific lines and columns from file using Java -


I have a text file that looks like this:

  ABCDA 5 7 10 2 B1 5 4 20C10 1 8 4D5 7 4 2  

So it is considered a matrix and what I have to do:

1) Vector name To get (which can easily be done by reading the first line)

2) To visualize numbers only.

3) To store the numbers in the 2D array.

I have already done the first phase, the second and the third.

This is my code:

Lecturer. Java:

Import java.io.buffferedReader; Import java.io.fileReader; Import java.io.IOException; Public Class Lecturefichier {Public Zero Learfisher () {Buffer Redirector BR = Faucet; Try {string sCurrentLine; Br = new BufferedReader (new FileReader ("myFile")); SCurrentLine = br.readLine (); Println ("************************************); String [] nomsAuteurs = sCurrentLine.split (""); For (string author: nomsAuteurs) {System.out.println (auteur); } System.out.println ("****************************************************************************************** ****************************************************************** ((sCurrentLine = br.readline () For Collins = sCurrentLine.split (""); (String Colon: Collins) {System.out.println (colonne);}}} Hold (IOException e) {e.printStackTrace ();} Finally {try {if (Br! = Null) br.close ();} hold (IOException pre) {ex.printStackTrace ();}}}}

Main. Java:

When I If I run this code, then I get a result like this:

A57102B15420C10184D5742

This is due to this paragraph:

  for (string colon: colon) {System.out.print (colonne);}  

a println (colonne) in a separate line

  5 7 10 2 1 5 4 20 10 1 8 4 5 7 4 2  Code>  

What did I do? And how can I fix it?

Thank you!

Your code should be,

  while ((sCurrentLine = br .readline ()) = null (string) [colonus = sCurrentLine.split (""); (Int i = 1; i & lt; colonnes.length; i ++) for {System.out.print (colonus [i] + ""); } System.out.println (); }  

For the output you expected.

You should print all the elements in a row for the loop, and then the line from which you read, will have to print a new line. File.


No comments:

Post a Comment