so i have an input file that i would like to test out with this program... the file name is "sample.txt.asc" how do i run this input file in this program? i thought the order would go:
所以我有一个输入文件,我想用这个程序测试...文件名是“sample.txt.asc”我如何在这个程序中运行此输入文件?我以为订单会去:
1)javac project1.java 2)java project1< sample.txt.asc
1)javac project1.java 2)java project1
but it is giving me an error saying array index out of bounds... at line 5. sorry any help would be great! thanks
但它给了我一个错误,说数组索引超出界限...在第5行。抱歉任何帮助都会很棒!谢谢
import java.util.Scanner;
import java.io.*;
public class project1 {
public static void main( String[] args ) throws IOException {
Scanner inFile = new Scanner( new File( args[1] ) );
String[] cypherLines;
outputHeaderLines( inFile );
cypherLines = getCypherLines( inFile );
outputObscureLines( process( args[0], getDataArrayFrom( cypherLines ) ), cypherLines );
outputRestOfLines( inFile );
inFile.close();
}
public static void outputRestOfLines( Scanner F ) {
while ( F.hasNext() )
System.out.println( F.nextLine() );
}
public static void outputObscureLines( char[][] data, String[] lines ) {
// Print data chars and then rest of chars from lines
for ( int row = 0; row < data.length; row++ ) {
for ( int col = 0; col< data.length; col++ ) {
System.out.print( data[ row ][ col ] );
}
for ( int col = data.length; col < lines[ row ].length(); col++ ) {
System.out.print( lines[ row ].charAt( col ) );
}
System.out.println();
}
// Now output that "extra" line we remembered
System.out.println( lines[ lines.length - 1 ] );
}
public static char[][] process( String command, char[][] dataArray ) {
char[][] newDataArray = duplicate( dataArray );
for ( int i = 0; i < command.length(); i++ ) {
switch ( command.charAt( i ) ) {
case 'v':
case 'V':
newDataArray = flipVertical( newDataArray ); break;
case 'h':
case 'H':
newDataArray = flipHorizontal( newDataArray ); break;
case 'r':
case 'R':
newDataArray = rotate( newDataArray ); break;
default:
// this should be an error, but we'll presume the
// command sequence is valid and do nothing.
}
}
return newDataArray;
}
public static char[][] rotate( char[][] data ) {
char[][] temp = new char[ data.length ][ data.length ];
for ( int row = 0; row < data.length; row++ )
for ( int col = 0; col < data.length; col++ )
temp[ data.length - col - 1 ][ row ] = data[ row ][ col ];
return temp;
}
public static char[][] flipHorizontal( char[][] data ) {
char[][] temp = new char[ data.length ][ data.length ];
for ( int col = 0; col < data.length; col++ )
for ( int row = 0; row < data.length; row++ )
temp[ row ][ data.length - col - 1 ] = data[ row ][ col ];
return temp;
}
public static char[][] flipVertical( char[][] data ) {
char[][] temp = new char[ data.length ][ data.length ];
for ( int row = 0; row < data.length; row++ )
for ( int col = 0; col < data.length; col++ )
temp[ data.length - row - 1 ][ col ] = data[ row ][ col ];
return temp;
}
public static char[][] duplicate( char[][] data ) {
char[][] temp = new char[ data.length ][ data.length ];
for ( int row = 0; row < data.length; row++ )
for ( int col = 0; col < data[ row ].length; col++ )
temp[ row ][ col ] = data[ row ][ col ];
return temp;
}
public static char[][] getDataArrayFrom( String[] lines ) {
char[][] data = new char[ lines.length - 1 ][ lines.length - 1 ];
// Only process valid cypher lines
for ( int row = 0; row < lines.length - 1; row++ )
for ( int col = 0; col < lines.length - 1; col++ )
data[ row ][ col ] = lines[ row ].charAt( col );
return data;
}
public static String[] getCypherLines( Scanner F ) {
String[] lines = new String[0];
String currentLine = F.nextLine();
while ( ( currentLine.length() == 64 ) &&
( lines.length <= 64 ) &&
noEqualSigns( currentLine ) ) {
lines = push( lines, currentLine );
currentLine = F.nextLine();
}
// I have a current line of cypher text I need to preserve.
return push( lines, currentLine );
}
public static void outputHeaderLines( Scanner F ) {
String currentLine = "";
do {
currentLine = F.nextLine();
System.out.println( currentLine );
} while ( ! blank( currentLine ) );
System.out.println();
}
public static String[] push( String[] list, String item ) {
String[] newList = new String[ list.length + 1];
for ( int i = 0; i < list.length; i++ )
newList[ i ] = list[ i ];
newList[ list.length ] = item;
return newList;
}
public static boolean noEqualSigns( String line ) {
// If they exist, they'll be at the end, so just check the
// last char
return line.charAt( line.length() - 1 ) != '=';
}
public static boolean blank( String line ) {
for ( int i = 0; i < line.length(); i++ )
if ( ( line.charAt( i ) != ' ' ) &&
( line.charAt( i ) != (char)9 ) )
return false;
return true;
}
}
1 个解决方案
#1
-1
For ex:
I have an jar (export as jar) file (program.jar) as program and in.txt file as input file.
I will run:
例如:我有一个jar(导出为jar)文件(program.jar)作为程序和in.txt文件作为输入文件。我会跑:
java -jar program.jar in.txt
With javac, you should use javac javaclass.java in.txtjava -jar program.jar in.txt使用javac,你应该使用javac javaclass.java in.txt
Notice without "<" letter.
And in program code, I should use args[0] as input file name instead of args[1].
Hope it will help.
I just mention to command line, I didn't read you code.
注意没有“<”字母。在程序代码中,我应该使用args [0]作为输入文件名而不是args [1]。希望它会有所帮助。我只是提到命令行,我没有读你的代码。
#1
-1
For ex:
I have an jar (export as jar) file (program.jar) as program and in.txt file as input file.
I will run:
例如:我有一个jar(导出为jar)文件(program.jar)作为程序和in.txt文件作为输入文件。我会跑:
java -jar program.jar in.txt
With javac, you should use javac javaclass.java in.txtjava -jar program.jar in.txt使用javac,你应该使用javac javaclass.java in.txt
Notice without "<" letter.
And in program code, I should use args[0] as input file name instead of args[1].
Hope it will help.
I just mention to command line, I didn't read you code.
注意没有“<”字母。在程序代码中,我应该使用args [0]作为输入文件名而不是args [1]。希望它会有所帮助。我只是提到命令行,我没有读你的代码。