Note that there are some explanatory texts on larger screens.

plurals
  1. POExceptions in java
    primarykey
    data
    text
    <p>The code compiles fine but gives run time errors like</p> <pre><code>Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at lab2.readFile(lab2.java:92) at lab2.main(lab2.java:79) </code></pre> <p>I'm not really sure what the issues are a I have thrown the exceptions in the code which is as follows.</p> <pre><code>import java.io.*; import java.util.*; class Customer { int account_id; char[] ch1 = new char[20]; String name = new String (ch1); char[] ch2 = new char[80]; String address = new String (ch2); char[] ch3 = new char[10]; String phone_number = new String (ch3); char[] ch4 = new char[8]; String date_of_birth = new String (ch4); double account_balance; public int get_accountid(){ return account_id; } public String get_address(){ return address; } public String get_phone_number(){ return phone_number; } public String get_date_of_birth(){ return date_of_birth; } public double get_balance(){ return account_balance; } public void set_account_id(int num){ account_id = num; } public void set_address(String add){ address = add; } public void set_phone_number(String phone){ phone_number = phone; } public void set_date_of_birth(String dob){ date_of_birth = dob; } public void set_balance(double bal){ account_balance = bal; } Customer(){ // default constructor } // parametrized constructor Customer(int id, String name, String add, String dob, String num, double bal){ this.account_id = id; this.name = name; this.address = add; this.date_of_birth = dob; this.phone_number = num; this.account_balance = bal; } } public class lab2{ public static void main(String args[])throws IOException{ String filename; System.out.println("Enter the filename for the input file"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); filename = reader.readLine(); Customer[] records = readFile(filename); } public static Customer[] readFile(String filename)throws IOException{ Customer[] review = new Customer[30]; int i=0; Scanner scan = new Scanner (new File (filename)); while (scan.hasNext()){ while(i&lt;30){ review[i].set_account_id(scan.nextInt()); String[] st = scan.nextLine().split("="); review[i].set_address(st[1]); st = scan.nextLine().split("="); review[i].set_phone_number(st[1]); st = scan.nextLine().split("="); review[i].set_date_of_birth(st[1]); //st = scan.nextLine().split("="); review[i].set_balance(scan.nextDouble()); scan.nextLine(); i=i+1; } } return review; } } </code></pre> <p>SAMPLE INPUT FILE:</p> <pre><code> Account Id = 70 Name = Tan Beng How Address = Blk 111 #05-06, Nanyang Avenue, Singapore 639798 DOB = 12-07-1979 Phone Number = 799-8765 Account Balance = 2324.23 Account Id = 17 Name = Mohammed Azeem Address = Blk 230 #20-116, Yishun Ave 11, Singapore 439772 DOB = 19-11-1979 Phone Number = 224-0098 Account Balance = 1087.03 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload