无法找到我的逻辑错误

时间:2012-01-24 16:07:45

标签: java netbeans

我制作了一个简单的程序: 使用文件(读写) 最终类扩展 但该计划不起作用。 Netbeans显示没有错误但是当我运行它时......出现了某种错误.....我很难理解我的错误在哪里(我认为这是一个合乎逻辑的错误)。

这是一个简单的程序:

package detyre_kursi;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Detyre_kursi {

  public static void main(String[] args) {
    LlogariBankare llogaria1 = new LlogariBankare("aaa", 1000); 
    llogaria1.Balanca();    
  }

}



class LlogariBankare {
  //variablat e instances
  private String id;
  private int nrLlogarise;
  private int vitiHapjes;
  private double balanca;
  static int nrTranasksioneve = 0;

  public LlogariBankare() {
    System.out.println("Ju keni harruar te vendosi id dhe nrLlogarise");

  }

  public LlogariBankare(String id, int nrLlogarise) {
    this.id = id;
    this.nrLlogarise = nrLlogarise;
    vitiHapjes = 0;
    balanca = 0; 
    Lexim(this.id, this.nrLlogarise); 
  }

  public double getBalanca() {
    return balanca;
  }

  public int getVitiHapjes() {
    return vitiHapjes;
  }

  private void Lexim(String s, int llog) {
    try {
      File file = new File("c:\\java\\balanca.txt");
      Scanner scanner = new Scanner(file);
      while (scanner.hasNextLine()) {
        if (scanner.next().equals(s) && scanner.nextInt() == llog) {
          vitiHapjes = scanner.nextInt();
          balanca = scanner.nextDouble();
        }
      }
    } catch (IOException e) {
      e.getMessage();
    }   
  }


  void Balanca() { 
    try{
      File file = new File("c:\\java\\test.txt");
      PrintWriter out = new PrintWriter(file);
      out.println(this.balanca);  
    } catch (IOException e) {
      e.getMessage();
    }   
    System.out.println(this.id + " , ju keni " + this.balanca + 
        " lek ne llogarine tuaj te krijuar ne vitin " + vitiHapjes + 
        " dhe keni kryer " + nrTranasksioneve + " transaksione gjithsej");
  }


  void Terheqe(double terheqe) {
    this.balanca -= terheqe;
    System.out.println("Ju sapo keni terhequr " + terheqe + " nga llogaria juaj");
    nrTranasksioneve++;
  }

  void Depozitim(double depozitim) {
    this.balanca += depozitim;
    System.out.println("Ju sapo keni depozituar " + depozitim + " nga llogaria juaj");
    nrTranasksioneve++;
  }


}

class Interesi extends LlogariBankare {
  int vitiTanishem = 2012;
  double interesi = 0;
  int diferencaViteve = vitiTanishem - getVitiHapjes();

  Interesi(String id, int nrLlogarise) {
    super(id,nrLlogarise); 
  }

  void gjejInteresisn() {
    interesi = getBalanca() + getBalanca() * diferencaViteve * 0.01;
  }

}

文件balanca中有这一行: aaa 1000 1990 34000

可怜的话,这是银行的简单版本。

您从文件中读取余额,并且 你使用Terheqe()和Depozitim()来表示 - 和余额。 您可以使用Balance()查看您拥有多少美元。当我运行它时,会出现此错误:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at detyre_kursi.LlogariBankare.Lexim(Detyre_kursi.java:57)
at detyre_kursi.LlogariBankare.<init>(Detyre_kursi.java:40)
at detyre_kursi.Detyre_kursi.main(Detyre_kursi.java:11)

Java结果:1

3 个答案:

答案 0 :(得分:1)

此行导致问题。 scanner.nextInt()可能不是int,除非你有特殊的理由,否则我觉得做两个next()调用并不好。

if(scanner.next().equals(s)&&scanner.nextInt()==llog){

答案 1 :(得分:0)

这只是一个猜测,但尝试更换:

scanner.next().equals(s)

使用:

s.equals(scanner.next())

答案 2 :(得分:0)

我认为你的逻辑问题来自

的构造函数
LlogariBankare llogaria1 = new LlogariBankare("aaa", 1000); 

再次检查。