สิ่งที่ฉันพยายามทำคือให้มันใช้ไฟล์จาวาที่มีลักษณะเช่นนี้
public class test1 {
public static void main ( string[] args ) {
System.out.println( "This is Test 1." );
}
}
และควรจะส่งออกไฟล์ข้อความโดยมีระยะห่างและการเยื้องที่เหมาะสม จนถึงตอนนี้ฉันสามารถเยื้องที่ถูกต้องสำหรับบรรทัดแรกได้ แต่ฉันมีปัญหากับ for วงที่สองที่พิมพ์ช่องว่างสำหรับวงเล็บปิด วงเล็บปิดท้ายจะพิมพ์ออกมาด้านนอกเหมือน 3 บรรทัดแรกแทนที่จะพิมพ์เข้าด้านใน ขออภัยหากตัวแปรของฉันทำให้สับสน
นี่คือรหัสของฉันจนถึงตอนนี้
public class JavaJustifier {
public static void main( String[] args )
throws FileNotFoundException {
for( int i = 1; i < 6; i++ ) {
justifyJava( "Test" + i + ".java",
"Justified" + i + ".txt",
4 );
}
}
public static void justifyJava( String inputFileName,
String outputFileName,
int tabSize )
throws FileNotFoundException {
int counter = 0;
int counter2 = 0;
int blah = 0;
File f = new File(inputFileName);
File p = new File(outputFileName);
if (p.exists())
p.delete();
Scanner input = new Scanner (f);
PrintStream name = new PrintStream(new File(outputFileName));
while (input.hasNextLine()) {
String line = input.nextLine();
Scanner lineScan = new Scanner(line);
if (line.contains("{") == true) {
name.print("{\r\n");
counter++;
for (int i = 1; i <= counter; i++) {
for (int j = 0; j <= tabSize; j++) {
name.print(" ");
}
}
System.out.println(counter);
} else if (line.contains("}") == true) {
name.print("\r\n");
counter--;
for (int x = 1; x <= counter; x++) {
for (int y = 1; y <= tabSize; y++) {
name.print(" ");
}
}
name.print("}");
System.out.println(counter);
} else {
name.print(line);
}
}
}
สิ่งที่ให้ฉันคือ
public class Test1
{
public static void main( String[] args )
{
System.out.println( "This is Test 1." );
}
}
สิ่งที่ฉันปรารถนาคือสิ่งนี้
public class Test1
{
public static void main( String[] args )
{
System.out.println( "This is Test 1." );
}
}
if (line.contains("{") == true) {
เพียงแค่เขียนif (line.contains("{")) {
- person jlordo   schedule 16.05.2013if (line.contains("{") == true) {
โดยพื้นฐานแล้ว คุณจะเพิกเฉยต่อข้อความที่เหลือและจัดการเฉพาะ{
เท่านั้น - person zEro   schedule 20.05.2013