simple java program giving O/P more than once .Pls point out the mistakes
807588Jun 5 2009 — edited Jun 6 2009I am doing a Marksheet generation program . The program is menu driven and still in initial stage.But when I tried to run it till what I have done it is giving O/P more than once .Here is the program
package Marksheet;
import java.io.*;
class Student
{
String name,roll;
char sec;
int marks[]=new int[3];
int total;
float percent;
}
class Main
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
char ch;
do
{
System.out.println("\t Menu");
System.out.println("1. Enter data of the student");
System.out.println("2. View marks of a student");
System.out.println("3. View master marksheet");
System.out.println("4. Edit data of the student");
System.out.println("5. Exit");
System.out.println("\n Enter your choice (1-5) : ");
ch=(char)br.read();
System.out.println(ch);
switch(ch-48)
{
case 1:{System.out.println("Inside 1");
break;
}
case 2:{System.out.println("Inside 2");
break;
}
case 3:{System.out.println("Inside 3");
break;
}
case 4:{System.out.println("Inside 4");
break;
}
case 5:{System.out.println("Exiting");
break;
}
default:{System.out.println("Please enter value between 1-5 \n");
}
}
}while((ch-48)!=5);
}
}
This is the sample input and output :
Menu
1. Enter data of the student
2. View marks of a student
3. View master marksheet
4. Edit data of the student
5. Exit
Enter your choice (1-5) :
1
1
Inside 1
Menu
1. Enter data of the student
2. View marks of a student
3. View master marksheet
4. Edit data of the student
5. Exit
Enter your choice (1-5) :
Please enter value between 1-5
Menu
1. Enter data of the student
2. View marks of a student
3. View master marksheet
4. Edit data of the student
5. Exit
Enter your choice (1-5) :
2
2
Inside 2
Menu
1. Enter data of the student
2. View marks of a student
3. View master marksheet
4. Edit data of the student
5. Exit
Enter your choice (1-5) :
Please enter value between 1-5
Menu
1. Enter data of the student
2. View marks of a student
3. View master marksheet
4. Edit data of the student
5. Exit
Enter your choice (1-5) :
5
5
Exiting
The program is not stopping at the statement "ch=(char)br.read();"
alternate time .
As you can see first time it stopped , next time it didn't stop and
took some value for the input by itself , thats why the default
message came ; it repeats alternately as is obvious from the
output.Pls point out the error .
Is there any method to flush standard input as we have fflush(stdin)
in C/C++ ?