2016-10-18 4 views
-1

Ive는 input = nextLine()을 시도하고 String 데이터에 입력을 추가했지만 여전히 null을 반환합니다. 여기서 가능한 문제가 될 수 있습니까?파일에 기록 된 출력이 왜 0입니까? 내 코드의 문제점은 무엇입니까?

[철회 방법]

public void Withdraw(){ 

    System.out.print("Enter withdrawal amount:"); 
    double withdraw = input.nextDouble(); 
    System.out.println("Your withdrawal amount: " + withdraw); 
     if(this.balance - withdraw < 100){ 
      System.out.println("The maintaining balance should not be less than 100. "); 
     }else 
    this.balance -= withdraw; 
    System.out.println("Your new balance is:" + this.balance); 
} 

[파일에 추가 여기서

public void withdrawWriter(){ 
try{ 
     String data = "Your withdrawal amount: " + withdraw; 

     File file = new File(this.name + ".txt"); 

     //if file doesnt exists, then create it 
     if(!file.exists()){ 
      file.createNewFile(); 
     } 

     //true = append file 
     FileWriter fileWritter = new FileWriter(file.getName(),true); 
      BufferedWriter bufferWritter = new BufferedWriter(fileWritter); 
      bufferWritter.write("\r\n"); 
      bufferWritter.write(data); 
      bufferWritter.close(); 

     System.out.println("Done"); 

    }catch(IOException e){ 
     e.printStackTrace(); 
    } 
} 

는 스크린 샷입니다 :

CLICK HERE FOR THE PHOTO

답변

1

그것은 스크린 샷 분명하다 당신은 둘 다 지역 변수 le double withdraw이고 필드는 double withdraw입니다. 로컬 없애 경우

그래서 그냥, 작동 할 수 있습니다 : 당신은 철수 지역 변수 안에 입력을 수용하고 withdrawWriter 방법 내에서 인스턴스 변수를 사용하려고

withdraw = input.nextDouble(); 
0

. 다음과 같이 입력을 허용하는 동안 인출 인스턴스 변수를 사용하십시오.

public void Withdraw(){ 

    System.out.print("Enter withdrawal amount:"); 


**withdraw = input.nextDouble();** 

    System.out.println("Your withdrawal amount: " + withdraw); 
     if(this.balance - withdraw < 100){ 
      System.out.println("The maintaining balance should not be less than 100. "); 
     }else 
    this.balance -= withdraw; 
    System.out.println("Your new balance is:" + this.balance); 
} 
관련 문제