gpt4 book ai didi

java - Windows Cmd : What to type to run a program with your input file?

转载 作者:行者123 更新时间:2023-12-02 11:06:54 26 4
gpt4 key购买 nike

所以我有一个要使用此程序进行测试的输入文件...文件名为“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



注意,没有 “<” 字母。

在程序代码中,我应该使用 args [0] 作为输入文件名,而不是 args [1]
希望它会有所帮助。
我只是提到命令行,我没有阅读您的代码。

关于java - Windows Cmd : What to type to run a program with your input file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com