Thursday, December 8, 2011

Java code,compiling but not working properly.?

import java.io.*;





public class projct


{


String cust_name;





int cust_id , account_no , code;





int balance , w , d;





BufferedReader br = new BufferedReader(new InputStreamReader(System.in));





public void run ( ) throws IOException


{


do


{


System.out.println("Enter code");


code = Integer.parseInt(br.readLine());


if ( code == 5555 )


{


getdata();


int ch;


char a = 'y';


do


{


System.out.println("Please select what you want to do");


System.out.println("Select a choice");


System.out.println("1.Withdrawl");


System.out.println("2.Deposit");


System.out.println("3.Balance enquiry");


System.out.println("4.Exit");


System.out.println("Enter your choice");


ch = br.read();


switch ( ch )


{


case '1' :


withdrawl();


break;


case '2' :


deposit();


break;


case '3' :


showdata();


break;


case '4' :


System.out.println("Do you want another transaction?");


System.out.println("Enter y for yes and n for no");


a = (char) br.read();


break;


default :


System.out.println("You have entered the wrong choice.Please try again");


break;


}


} while (!(ch == 'n'));


} else


System.out.println("Try again, wrong code");


} while (!(code == 5555));


}





public void getdata ( ) throws IOException


{


System.out.println("Enter customer i.d");


cust_id = Integer.parseInt(br.readLine());


System.out.println("Enter customer account number");


account_no = Integer.parseInt(br.readLine());


System.out.println("Enter customer name");


cust_name = br.readLine();


System.out.println("Enter balance amount in account");


balance = Integer.parseInt(br.readLine());





}





public void withdrawl() throws IOException


{


System.out.println("Enter amount to be withdrawn ");


w =br.read();


if ( w %26lt; balance )


{


balance -= w;


System.out.println(" Amount has been withdrawn");


System.out.println(" New balance is : " + balance);


} else


{


System.out.println(" Sorry! Amount cannot be withdrawn");


}


}





public void deposit() throws IOException


{


System.out.println("Enter amount to be deposited");


String s2 = br.readLine();


d = Integer.parseInt(s2);


balance += d;


System.out.println("Amount has been deposited");


System.out.println("New balance is" + balance);


}





public void showdata() throws IOException


{


System.out.println("Customer name:" + cust_name);


System.out.println("Customer id" + cust_id);





System.out.println("Customer accont number" + account_no);





System.out.println("Balance amount in account" + balance);


}


}





The problem is that the results shown are all random numbers. When I type in 5560 for balance and 450 for withdraw(in option 1), it just gives me a random no. such as 7000 or 7600 or 4389 or something. What is wrong?Plz help. I need it by Monday.


I use BlueJ|||Your code is almost there. I have the philosophy of keeping everything String until I need a numeric value. If-IWY, I would re-write this program one more time. Just under String cust_name; add a new var -- String input;


then whenever you br.nextLine() you do it as:


input = br.nextLine();





that will clear out any buffers, wipe off any extra white space and just get letters. You KNOW you have a String.





And, you did this at the deposit() method. In your code, you have String s2 = br.nextLine();


and then you convert to a number


Integer.parseInt(s2);





Same technique all over.





Actually, the only place you need a number is the switch() and the balance. Everything else could be a String.





Look, inside of any method()s...





public myMethod( this var belongs only to this method ) {





all the vars inside this method talk to NOBODY else. They can have the same name as outside vars, but, these vars belong and participate only inside this method


}


got it? method( var_nameDoesNotMatter ) { own little world; }





=====================





public void withdrawl() throws IOException {


System.out.println("Enter amount to be withdrawn ");


w = br.readLine(); // gets the String and clears any trash.


int debit = Integer.parseInt( w );


int onDeposit = Integer.parseInt ( balance );


if (debit %26lt; onDeposit) {


onDeposit -= debit;





System.out.println( debit + " has been withdrawn");


System.out.println(" New balance is : " + onDeposit);





balance = String.valueOf( onDeposit );


} else {


System.out.println(" Sorry! Amount cannot be withdrawn");


}


}





Change your switch statement to numbers of type int


at the top of the class name your choices like this


int ONE = 1;


int TWO = 2;


int THREE = 3;


int FOUR = 4;


...


input = br.readLine();


int ch = Integer.parseInt( input );


switch( ch) {


case ONE :





as it is, just before switch you take in a String, slam the String over to a char which happens as an ASCII of letter number 50. So, the easiest fix is the above suggestion. I won't get into why char is such a "beetch".|||Okay, so going through your code I believe the problem is with how you get the input from the user. Since you're reading in only 1 character you're leaving the "enter key" (carriage return -and or line feed if linux-) in the buffer. This is causing all subsequent reads from the buffer to be off.





Solution:


You have a few choices:


1) after getting the char you expect, call another read to remove the "enter key" from the buffer


2) read input in as one line. Then use Integer.parse() to convert strings to numbers and the charAt() for strings to get the character at a specific position in a string.

No comments:

Post a Comment