2015-01-15 1 views
-1

간단한 은행 업무 신청서를 작성해야하는 과제가 있습니다. 나는 성과와 성, 고객 ID 및 균형을 가진 파일에 고객 정보를 저장해야합니다. 그런 다음 잔액을 확인하고 인출하고 입금하고 종료하는 메뉴 옵션을 만들어야합니다. 내가 현재 가지고있는 문제는 사용자가 균형을 확인하려고 할 때 균형을 보여 주려고하는 것입니다. 잔액이 내 파일의 네 번째 줄에 있으며 네 번째 줄만 표시 한 다음 그 번호를 사용하여 숫자를 더하거나 뺄 수있는 방법을 모르겠습니다. switch 문에서 case 1에서이 작업을 수행하려고합니다. 모든 힌트를 부탁드립니다.java에서 파일을 읽으시겠습니까?

import java.util.Scanner; 
import java.text.DecimalFormat; 
import java.io.*; 
public class bank { 

    public static void main(String[] args) throws IOException { 


     String fileName; // stores the name of the file 
     String bankCustomer; // used to display the customers bank information 
     int menuOption = 0; // allows the user to enter a number so they can select what they want to do 

     Scanner keyboard = new Scanner(System. in); 

     // enter the file name to see customer information 
     System.out.print("enter file name "); 
     fileName = keyboard.nextLine(); 

     File file = new File(fileName); 
     Scanner inputFile = new Scanner(file); 

     // read customer information from file i need this 
     while (inputFile.hasNext()) { 
      bankCustomer = inputFile.nextLine(); 
      System.out.println(bankCustomer); 
     } 

     System.out.println("Please enter the number for the following:\n 1) Balance \n 2) Deposit + \n 3) Withdrawal \n 4) Quit "); 

     menuOption = keyboard.nextInt(); 

     switch (menuOption) { 
      case 1: 
       // need to show balance how do i do that? 
       while (inputFile.hasNext()) { 

        bankCustomer = inputFile.nextLine(); 
        System.out.println(bankCustomer); 
       } 
       break; 
      default: 
       System.out.println("Invalid choice"); 
     } 
     inputFile.close(); 
    } 
} 

답변

0

이렇게하는 방법에는 여러 가지가 있습니다. 한 가지 방법은 Scanner 개체에서 모든 라인을 읽고리스트 (ArrayList에) 또는 문자열 배열로 변환하는 것입니다. Array 또는 List의 각 항목은 파일의 한 줄에 해당합니다.

리스트 또는 배열의 인덱스는 그 특정 파일의 내용을 제공한다.

============================================== =============================== 여전히 배열 당신을 배울 것을 지금, 당신의 의견에

확인을 기반으로 그것을 대답은 단지 나타내는 모든 널 (null) 검사를하지 않고 당신이 말한대로 파일 형식이 정확하게 가정 물론 당신의 은행 고객 이름

String bankCustomer = inputFile.nextLine(); 

//Assuming second line contains account number 
String bankAccountNumber = inputFile.nextLine(); 

// Assuming third line contains account type. 
String bankAccountType = inputFile.nextLine(); 

// Fourth line has your account balance 
String bankAccountBalanceStr = inputFile.nextLine(); 

double bankAccountBalance = 0d; 
if(null != bankAccountBalanceStr && 0 > bankAccountBalanceStr.length()){ 
    bankAccountBalance = Double.parseDouble(bankAccountBalanceStr); 
} 

을 읽는 방법을 할 수 있습니다.

+0

배열 –

+0

@RishiDholliwar 당신은 수업 만들기를 다루었습니까? 그렇다면, 모든 필드를 가진'User' 클래스를 생성하고 그것을 채 웁니다. –

+0

는 대답 – owaism

0

가장 쉬운 방법은 단순히 뭔가를 수행하여 읽고 처음 세 줄을 무시하는 것입니다

for (int i = 0; i < 3; i++) 
    inputFile.nextLine(); 

하고있는 Integer.parseInt() 메소드를 정수로 네 번째 줄을 읽고.

int total = Integer.parseInt (inputFile.nextLine()); 

두 단편에서 오류 및 예외 처리가 생략되었습니다.

+0

내 switch 문에이 내용을 붙여 넣은 다음 System.out.println (bankCustomer); 아무 일도 일어나지 않습니다. –

0

데이터와 코드를 다시 작성하지 않고, 당신이 bankcustomer 데이터의 전체 라인을 읽고있는 것으로 나타납니다. 데이터 파일 구조가 쉼표로 분리 된 경우, 선을 구성 요소로 분리 할 수 ​​있습니다.

balance = customer.balance. etc. 

이것은 대략적인하지만 난 그게 도움이되기를 바랍니다 : 당신이 데이터 클래스로 번역 선을 일단 데이터 클래스

public static class customer { 
    public String first; 
    public String last; 
    public String customer_i; 
    public double balance: 

}

을 정의, 당신은 각을 참조 할 수 있습니다.

+0

임에도 불구하고 내 코드에서 이것을 구현하는 방법을 모르겠습니다. 첫 번째 줄을 첫 번째 줄에 넣거나 마지막 줄을 균형있게 배치하려면 어떻게합니까? –

0

당신도 3 개 라인을 읽고 그 값, 를 저장하지 않거나 키워드를 각 행을 검색 할 수 있습니다 : 우리가 배열을 포함하지 않은 아직 클래스 그래서 난없이 할 수있는 방법을 찾고 있어요

int balance; 
if(!bankCustomer.indexOf("balance") != -1) 
{ 
    balance = Integer.parseInt(bankCustomer); 
} 
관련 문제