I'm having some problems with this code. I have to write a code where it can display columns from a text file Swap field for swap field and column 2 is called column 2.
public static zero main (string [] args) {int lineNum = 0; String delimiter = ""; If (args.length! = 3) {System.out.println ("Usage: Java Swap Column File Name column # column #"); System.exit (-1); } String datafilename = args [0]; String column: text = arguments [1]; String column bytext = Args [2]; Int column A = integer Pyrite (columnite); Int column B = integer. PRIIST (columnByText); File datafile = new file (datafile name); Scanner input; String output text = null; System.out.printf ("DataFileName =% s, column A =% d, column B =% d \ n", datafilename, column A, column B); Try {Input = new scanner (datafile); While (input. HasNextLine ()) {string input text = input NXtine (); LineNum ++; Output Text = Swap Field (input text, column A, column B, delimiter); System.out.printf ("% d:% s \ n", line new, output text); }} Hold (filenoted expression FNF) {System.out.printf ("File not found:% s \ n", datafile name); }} Stable string swapfilld (string input, int field a, int field b, string delim) {string output buffer = ""; // code needed here ReturnBuffer; }
OK, then you have a string input delimited < / Code>, and swap fields field a
and field b
? Fixed string swap field (
string input, int field A, int field B, string cymel) by
{string [] bits = input. Split (delim); String temp = bits [field a]; Bits [field A] = bits [field B]; Bits [Field B] = Temporary; Return String.Join (Sealim, Beats); }
In this code, .split ()
method input
in an array, delim
separator (See below for assumptions about this as explained in the form of a regular expression). Two relevant (zero-indexed) fields are then swapped, and .join ()
is rebuilt by using string
.
Note that the last line ( .join ()
) requires Java 8. If you do not have Java 8 then you can use StringUtils.join
from Apache Commons Lang.
I also believe here that your delim
.split ()
is in the correct format for the method, which is to say that this string literally Which includes escape and other reggae characters, it looks like a commendable enough assumption if it is a delimiter in a text file (usually a comma, space or tab) that further assumes that in delimiter input
There are nowhere between the quotes or any other thing. You have not mentioned anything about the quote; If you want to be able to handle such things then you will need to add something to make it clear.
Comments
Post a Comment