作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个要使用此程序进行测试的输入文件...文件名为“sample.txt.asc”,如何在此程序中运行此输入文件?我认为订单会去:
1)javac project1.java 2)java project1
但是它给我一个错误,说数组索引超出范围...在第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;
}
}
最佳答案
例如:
我有一个jar文件(作为jar导出)文件(program.jar)作为程序,而in.txt文件作为输入文件。
我将运行:
java -jar program.jar in.txt
With javac, you should use javac javaclass.java in.txt
关于java - Windows Cmd : What to type to run a program with your input file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158532/
我是一名优秀的程序员,十分优秀!