JAVA Use Scanner instead of Buffered read in this code: import java.io.*; class
ID: 3603615 • Letter: J
Question
JAVA
Use Scanner instead of Buffered read in this code:
import java.io.*;
class Stack
{
private int array[];
private int top;
Stack()
{
array=new int[100];
top = -1;
}
public void push(int a)
{
if(top<99)
{
array[++top]=a;
}
else
{
System.out.println("Sorry more orders cannnot be processed.");
}
}
public void pop()
{
if(top==-1)
{
System.out.println("We dont have any other order to process.");
}
else
{
System.out.println("Processing order number "+array[top]);
top--;
}
}
public void display()
{
if(top==-1)
{
System.out.println("We don't have any order to display");
}
else
{
int i;
System.out.println("The list of orders goes as follows : ");
for(i=0;i<=top;i++)
{
System.out.println(array[i]);
}
}
}
}
public class Candies
{
public static void main(String args[])throws IOException
{
Stack obj;
obj=new Stack();
int ch,orderId;
orderId=1000;
ch=-1;
while(ch!=4)
{
System.out.println(" Welcome to the candy services 1: Place an order 2: Recieve the order 3: Display order list 4: Exit ");
System.out.print(" Please enter your choice : ");
BufferedReader p=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(p.readLine());
switch(ch)
{
case 1:
{
obj.push(orderId++);
break;
}
case 2:
{
obj.pop();
break;
}
case 4:
{
System.out.println("Thank You !");
break;
}
case 3:
{
obj.display();
break;
}
default:
{
System.out.println("Please enter a valid choice.");
break;
}
}
}
}
}
Explanation / Answer
Replace below statements:
BufferedReader p=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(p.readLine());
Modifed statements:
Scanner sc=new Scanner(System.in);
ch = sc.nextInt();
Everything else will remain the same.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.