Results 1 to 15 of 15
- 07-16-2008, 12:45 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
My code is not working properly ..modify it
problems-->
1. not taking any input inside Case 1 & Case 2.
2. System.exit() is not working .
3. is their any problem in do-while loop ?
import java.io.*;
public class shyamin {
public static void main ( String []args) throws IOException {
char input=' ';
do {
System.out.println("--------------- MENU ----------------");
System.out.println("1. Enter the name ");
System.out.println("2.Enter Roll ");
System.out.println("0.Exit");
System.out.println("Enter the Choice");
input=(char)System.in.read();
byte b[]=new byte[10];
switch(input) {
case'1':
System.out.println("Enter the Name ");
int l=System.in.read(b);
System.out.write(b,0,l-1);
break;
case'2':
System.out.println("Enter the Roll ");
byte c[]=new byte[4];
int len=System.in.read(c);
System.out.write(c,0,len-1);
break;
case '0':
System.out.println(" Invalid ");
System.exit(5);
//break;
}//switch
} while(input >2);
}
}
help pls.:::mad:
- 07-16-2008, 01:21 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
There is no any errors on exit. It should work. What error you get, any exceptions?
- 07-16-2008, 02:27 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
Two problems ..
1. when i m tring to take user input in Case 1 , it is onle printing, not taking the input from the keyboard . it should take the name & roll no. as input.
2. it is not comming out of the prog. after System.exit();
- 07-16-2008, 03:06 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to use either scanner or a BufferedReader for this.
- 07-16-2008, 03:33 PM #5
the while test compares the value of input(a char) to the int value of 2. It is very difficult to enter an int of two from the keyboard. You usually only enter characters from a keyboard and their int values start at 32 for a space.
Read the doc for the in.read() method. What does it return?
Could you copy and paste here the console when you run your tests. Add comments to it to describe what is wrong.
- 07-16-2008, 03:40 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
thank norm
but i m giving as case '2' this means it will read it as char .
the prog. printing the statment in side the case '1' but not promting for user input in the same case means it is going in side the case .
- 07-16-2008, 03:43 PM #7
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
- 07-16-2008, 03:51 PM #8
Add some more println() statements to your program so you can see what the values are that the program is seeing and where the code is executing.
Not sure what you mean.case '2' this means it will read it as charIf input is defined as char, it will NOT have a value of 2. Its value could be '2' which is not the same as 2. Use type casting with println() to see the int value of '2' : println("'2'=" + (int)'2');while(input >2);
- 07-16-2008, 03:52 PM #9
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
1
Enter the Name
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
2
Enter the Roll
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
0
Invalid
- 07-16-2008, 03:58 PM #10
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
1
Enter the Name this messg. is in side case 1
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
2
Enter the Roll this messg. is in side case 2
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
0
- 07-16-2008, 04:20 PM #11
I see the output.
what is the problem??? Is it working?
Could you copy and paste here the console when you run your tests. Add comments to it to describe what is wrong.
- 07-16-2008, 05:02 PM #12
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
Norm it is going in the cases but not prompting for user i/p .
console -->
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
1
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
Enter the Name this messg. is in side case 1
--------------- MENU ----------------
1. Enter the name
2.Enter Roll
0.Exit
Enter the Choice
2
Enter the Roll this messg. is in side case 2
--------------- MENU ----------------
- 07-16-2008, 05:42 PM #13
Where is this supposed to happen?not prompting for user i/p .
I don't see any reference to i/p in the program.
- 07-16-2008, 05:45 PM #14
Scanner
Norm, readline ( or whatever it is ) for System.in ( the keyboard ) returns an int. This should ( I think ) be as you would expect it. Java has a class for reading such things from the keyboard: Scanner (Java 2 Platform SE 5.0) which I would expect does the type conversions by convention. I have not figured out any nomenclature for my own use nor do I know what is accepted practice, the Integer class has a toString method that will take the keyboard input and do a conversion to integer as it's binary form as used by the processor and so on.
The Scanner class will show accepted practice in Java. A cast is likely ineffective, as it wil give ( for example ) 0x20 for a spacebar. ( I think )Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-16-2008, 05:48 PM #15
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
after entering into case1 & 2 -->
byte b[]=new byte[10];
switch(input) {
case'1':
System.out.println("Enter the Name ");
int l=System.in.read(b); // Here
System.out.write(b,0,l-1);
break;
case'2':
System.out.println("Enter the Roll ");
byte c[]=new byte[4];
int len=System.in.read(c); // Here
System.out.write(c,0,len-1);
Similar Threads
-
Image not showed properly in struts
By ganesan in forum Web FrameworksReplies: 4Last Post: 01-13-2009, 09:21 AM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
Log4j not working properly....
By prakash_dev in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 12:13 PM -
Modify A* Algorithm
By prakharbirla in forum Advanced JavaReplies: 1Last Post: 02-13-2008, 06:25 PM -
how to properly express adding...
By paul in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks