How to end a infinite while loop with user input - java

I am trying to create a loading sequence of 3 dots that repeats itself until input from the user breaks the loading sequence specifically the enter key. i connot for the life of me get the infinite while loop to end with input
public class loop {
public static void AnyKey() {
try {
System.in.read();
loading(false);
} catch (Exception e){}
}
public static void pause(long duration) {
try{
Thread.sleep(duration);
} catch (InterruptedException e){}
}
public static void loading(boolean status){
if (status == true) {
while (status) {
pause(500);
int i;
for (i = 0; i <3; i++){
System.out.print(".");
pause(500);
}
System.out.print("\b\b\b");
}
}
}
public static void main(String[] args) {
loading(true);
AnyKey();
}
}

In your current code, the main method calls loading and never leaves the function. If you go through loading(true) step by step, you find that since while(status) is always true you are stuck there and AnyKey() is never called.
Also, System.in.read(); is a blocking call. This means that you will wait for user input but will be unable to print the '...'. Instead I recommend your read the documentation for input stream, there you will find the .read() function but also the .available() function which will let you know if any characters have been entered in the input buffer.
Those should be all the tools you need to figure this one out (I think).
Hope this helps!

I figured it out i needed to learn about and use Threads and global variables check out my code below im fairly pleased with myself i was working on this for 3 days now lol
import java.util.Scanner;
class AnyKey extends Thread {
public void run() {
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
loadingDots.loadingStatus = false;
}
}
public class loadingDots {
public static boolean loadingStatus;
public static void pause(long duration) {
try {
Thread.sleep(duration);
} catch (InterruptedException e) {}
}
public static void loading(){
loadingStatus = true;
while (loadingStatus) {
pause(500);
int i;
for (i = 0; i < 3; i++) {
if (!loadingStatus){
break;
}
System.out.print(".");
pause(500);
}
System.out.print("\b\b\b");
}
}
public static void main(String[] args) {
AnyKey anykey = new AnyKey();
anykey.start();
loading();
}
}

Related

Java Thread wont run unless i put Thread.sleep(1);

I have a piece of practice code that is supposed to accept 1010 as the code when a user enters the code on the keyboard. The thread that keeps checking if the code was entered right wont run unless i put a Thread.sleep(1); in the run()
I wanted to know whats the reason behind this.
Class1:
import java.util.Scanner;
public class Class1 {
private static boolean valid = true, accepted = false, exit = false;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
newThread t1 = new newThread();
t1.start();
do {
try {
int code = Integer.parseInt(input.nextLine());
if(code == 1010)
accepted = true;
else
System.out.println("Please try again!");
}catch(Exception e) {
System.out.println("Please try again!");
}
}while(!exit);
}
public static boolean getValid() {
return valid;
}
public static void setValid(boolean input) {
valid = input;
}
public static boolean getAccepted() {
return accepted;
}
public static void setAccepted(boolean input) {
accepted = input;
}
}
newThread:
public class newThread extends Thread{
public void run() {
do {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(Class1.getAccepted())
Class1.setValid(false);
}while(Class1.getValid());
System.out.println("Code accepted");
}
}
Expected without Thread.sleep(1);:
1010
Code accepted
Actual results:
1010
Without sleep the newThread consumes all cpu and has no natural break point. In the
Java Language Specification you can read more about it.

How to make 2 threads share data? (Java)

I'm currently practicing Threads so I tasked myself to write a program that will create 2 threads. The first one will endlessly print a character and the second one will endlessly wait for input and then pass it to the first Thread. Then Thread #1 should print the passed character. Here's what I wrote:
public class A extends Thread {
public char dif;
Scanner stdin = new Scanner(System.in);
#Override
public void run() {
for (; ; ) {
dif = stdin.nextLine().charAt(0);
MyThread.setCh(dif);
}
}
}
This thread takes input and then passes it to this one:
public class MyThread extends Thread {
public static char ch;
public static void setCh(char cha) {
ch = cha;
}
public static char getCh() {
return ch;
}
#Override
public void run() {
for(;;) {
try {
Thread.sleep(300);
}
catch(InterruptedException e) {
e.printStackTrace();
}
System.out.print(getCh());
}
}
}
And what happens in the main():
MyThread endless = new MyThread();
MyThread.setCh('$');
A set = new A();
endless.start();
set.start();
However, this doesn't work as intended. No matter what I type, the program keeps printing $. Also for some reason the first time I type a character I get an Out of bounds exception.
Probably, the easiest way to approach this, is to use BlockingQueue.
Effectively, in your example the thread, that receives character from System.in is producer and the thread that prints received character is consumer.
So, here is the code that achieves your goal:
import java.util.*;
import java.util.concurrent.*;
class Setup {
public static void main(String[] args) {
BlockingQueue<Character> q = new LinkedBlockingQueue<>();
Producer p = new Producer(q);
Consumer c = new Consumer(q);
new Thread(p).start();
new Thread(c).start();
}
}
class Producer implements Runnable {
private final BlockingQueue<Character> queue;
private final Scanner scanner = new Scanner(System.in);
Producer(BlockingQueue<Character> q) { queue = q; }
public void run() {
try {
while (true) {
queue.put(produce());
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt(); // set interrupt flag
} finally {
scanner.close();
}
}
Character produce() {
return scanner.nextLine().charAt(0);
}
}
class Consumer implements Runnable {
private final BlockingQueue<Character> queue;
Consumer(BlockingQueue<Character> q) { queue = q; }
public void run() {
try {
while (true) {
consume(queue.take());
}
} catch (InterruptedException ex) {}
}
void consume(Character c) {
System.out.println("Received character: " + c);
}
}
The problem actually you have it is quite small delay in Thread.sleep(300);
Try to set sleep for a few seconds Thread.sleep(5000);. You have to type something before it will print previous char

Unidentifiable NZEC Error in Code-ENTEXAM

I was working on one of the problems on codechef.com https://www.codechef.com/problems/ENTEXAM
Here is my solution for the problem-
import java.io.*;
class Entrance_Final
{
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
static int test_case=0;//Test cases
static int students=0;
static int qualifiers=0;
static long result=0;
static int exams=0;
static long max_marks=0;
static long[]sigma_res;
static long sergey_score=0;
public static void main(String[]args)throws IOException
{
try
{
//System.out.println("Enter number of test cases.");
test_case=Integer.parseInt(in.readLine());
for(int lv=1;lv<=test_case;lv++)
comp_min_marks();
}
catch(Exception e)
{
System.err.println(e);
}
}
public static void comp_min_marks()throws IOException
{
try
{
//System.out.println("Enter students,enrollees,exams and maximum marks.");
String a=in.readLine();
a=a.trim();
int flag=0;
int times=1;
for(int lv=0;lv<a.length();lv++)
{
if(a.charAt(lv)==' '&&(times==1))
{
students=Integer.parseInt(a.substring(0,lv));
flag=lv+1;
times++;
}
else if(a.charAt(lv)==' '&&(times==2))
{
qualifiers=Integer.parseInt(a.substring(flag,lv));
flag=lv+1;
times++;
}
else if(a.charAt(lv)==' '&&(times==3))
{
exams=Integer.parseInt(a.substring(flag,lv));
flag=lv+1;
times++;
max_marks=Long.parseLong(a.substring(flag));
break;
}
}
sigma_res=new long[students-1];
//System.out.println("Enter the marks of all the students during their exams,each ones in one line");
for(int lv=0;lv<students-1;lv++)
{
String b=in.readLine();
sigma_res[lv]=int_sum(b);
}
//System.out.println("Now enter Sergey's scores");
if(exams==1)
{
//String b=in.readLine();
sergey_score=0;
}
else
{
String b=in.readLine();
sergey_score=int_sum(b);
}
sigma_res=doQuickSort(0,students-2);
result=sigma_res[students-qualifiers-1]-sergey_score+1;
if(result<0)
System.out.println("0");
else if(result<=max_marks)
System.out.println(result);
else
System.out.println("Impossible");
}
catch(Exception e)
{
System.err.println(e);
}
}
public static long int_sum(String b)throws IOException
{
try
{
b=b.trim();
long res=0;
int flag=0;
for(int lv=0;lv<b.length();lv++)
{
if(b.charAt(lv)==' ')
{
res+=Long.parseLong(b.substring(flag,lv));
flag=lv+1;
}
}
res+=Long.parseLong(b.substring(flag));
return res;
}
catch(Exception e)
{
System.err.println(e);
return -1;
}
}
private static long[] doQuickSort(int low,int high)throws IOException
{
try
{
if(high-low<1)
return sigma_res;
int wall=low;
int pivot_pos=(int)(Math.random()*(high-low))+low;
long pivot=sigma_res[pivot_pos];
long temp=sigma_res[high];
sigma_res[high]=pivot;
sigma_res[pivot_pos]=temp;
pivot_pos=high;
for(int lv=low;lv<=high-1;lv++)
{
if(pivot>sigma_res[lv])
{
temp=sigma_res[lv];
sigma_res[lv]=sigma_res[wall];
sigma_res[wall]=temp;
wall++;
}
}
temp=sigma_res[wall];
sigma_res[wall]=pivot;
sigma_res[pivot_pos]=temp;
pivot_pos=wall;
doQuickSort(low,wall-1);
doQuickSort(wall+1,high);
return sigma_res;
}
catch(Exception e)
{
System.err.println(e);
return sigma_res;
}
}
}
As you have probably noticed, I have enclosed all the code within my program in rather redundant try-catch blocks returning arbitrary exceptions. This is because I am always getting an NZEC-Error for my code (when I submit it online) and despite using these blocks, the error is persisting. I have repeatedly had a look at the constraints of the problem but had no luck figuring out what the issue is.
P.S I do not have access to the test cases of this problem.
Since there is no response here and I was able to figure out the issue, I suppose I might as well as answer my question. So first of all there was no exception at all, instead I was getting a VirtualMachineError in the line
sigma_res=new long[students-1];
(No idea why this was happening,all I know is the value of 'students' exceeded its constraint-defined limits).
After looking at a few solutions, I figured the issue was caused by taking the println statements out of the main() method and placing them in another one.
The moment I put the println statement back into main(), the solution was accepted.
P.S I still don't know why the program wouldn't terminate when the println statements where in another method.

interrupt class Main, if the method returns "false"

I would like to block a Card in my abstract ATM.
At this manner, I need to interrupt the program if the PIN wasn't accepted after the third attempt so that the Main class won't execute the next Methods.
Is it System.exit(0) the optimal decision? I chose this one because it's simple, but I'm not sure.
public boolean authenticity(int tries) {
if (tries <= 3)
{
short pin = sc.nextShort();
if (pin == 1234) {
System.out.println("PIN is correct");
System.out.println("Card is active for operation!");
return true;
} else {
System.out.println("PIN isn't correct! You used " + tries +" attempt!");
return authenticity(++tries);
}
}
System.out.println("\nCard was blocked!");
System.exit(0);
return false;
}
class Main looks so:
public class Main {
private static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
ATM atm = new ATM();
MasterCard aeroflotCard = new MasterCard();
atm.initCard(aeroflotCard);
aeroflotCard.authenticity(1); // if pin is wrong, than you are looser:)
System.out.println("\nRefill your balance:");
aeroflotCard.add(sc.nextInt());
aeroflotCard.balance();
}
You may try the following piece of code:
public boolean authenticity(int tries) throws yourException {
if (tries <= 3) {
// ...
} else {
throw new yourException("\nCard was blocked!");
}
}
In the main method:
public static void main(String[] args) {
try {
aeroflotCard.authenticity(1);
System.out.println("\nRefill your balance:");
aeroflotCard.add(sc.nextInt());
aeroflotCard.balance();
} catch (yourException e) {
System.err.println(e.getMessage());
}
}
In the yourException class:
public class yourException extends Exception {
// here you can override needed methods
}

Java - multithreading and synchronization

I have two very similar programs each trying to run two threads OddThread and EvenThread and trying to print the odd and even numbers in sequence . While the first one works , the second one hangs . Can anyone please pinpoint the bug in the second program ?
The first one which works :
public class ThreadTest {
public static void main(String[] args) {
System.out.println("Odd Even test");
NumHolder objNumHolder = new NumHolder();
Odd o1 = new Odd(objNumHolder, "Odd Number Thread");
Even e1 = new Even(objNumHolder, "Even Number Thread");
o1.start();
e1.start();
}
}
class NumHolder {
private int intCurrNum;
private boolean isEven = false;
public synchronized void printOddNumber(String tname) {
while (isEven == true){
try {
wait();
}catch (InterruptedException e) {
}
}
isEven = true;
System.out.println("Thread Name="+tname + "===Number="+intCurrNum);
intCurrNum += 1;
notifyAll();
}
public synchronized void printEvenNumber(String tname) {
while (isEven == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
isEven = false;
System.out.println("Thread Name="+tname + "===Number="+intCurrNum);
intCurrNum += 1;
notifyAll();
}
}
class Even extends Thread {
private NumHolder objNumHolder;
public Even(NumHolder p_objNumHolder, String name) {
super(name);
objNumHolder=p_objNumHolder;
}
public void run() {
for (int i = 0; i < 10; i++) {
objNumHolder.printEvenNumber(getName());
}
}
}
class Odd extends Thread {
private NumHolder objNumHolder;
public Odd(NumHolder p_objNumHolder,String name) {
super(name);
objNumHolder = p_objNumHolder;
}
public void run() {
for (int i = 0; i < 10; i++) {
objNumHolder.printOddNumber(getName());
}
}
}
The second code which hangs :
class PrintClass {
int intCurrNum;
private boolean isEven = false;
synchronized void printOdd(){
while(isEven){
try{
wait();
}catch(InterruptedException ie){
System.out.println("Interrupted exception in printOdd()");
ie.printStackTrace();
}
isEven = true;
System.out.println("Thread Name="+Thread.currentThread().getName() + "===Number="+intCurrNum);
intCurrNum += 1;
notifyAll();
}
}
synchronized void printEven(){
while(!isEven){
try{
wait();
}catch(InterruptedException ie){
System.out.println("Interrupted exception in printEven()");
ie.printStackTrace();
}
isEven = false;
System.out.println("Thread Name="+Thread.currentThread().getName() + "===Number="+intCurrNum);
intCurrNum += 1;
notifyAll();
}
}
}
class ThreadOdd extends Thread {
PrintClass pc = null;
ThreadOdd(PrintClass pc , String name){
super(name);
this.pc = pc;
}
public void run(){
for (int i = 0; i < 10; i++) {
pc.printOdd();
}
}
}
class ThreadEven extends Thread {
PrintClass pc = null;
ThreadEven(PrintClass pc,String name){
super(name);
this.pc = pc;
}
public void run(){
for (int i = 0; i < 10; i++) {
pc.printEven();
}
}
}
public class EvenOddPrintClass {
public static void main(String[] args){
PrintClass pc = new PrintClass();
Thread to = new ThreadOdd(pc,"ThreadOdd");
Thread te = new ThreadEven(pc,"ThreadEven");
to.start();
te.start();
}
}
Thanks.
I suggest you run your code in the debugger and step through both threads. It's very educational. You will see exactly where the error is.
In both versions, isEven starts out as false.
In the first version, printOddNumber will skip the whole while loop, print the odd number, set isEven to true and notify the even thread, which will print the even number and notify the odd thread again etc. in sequence.
In the second version, printOddNumber will skip the whole while loop, including printing the number and notifying the even thread. After 10 attempts it will exit without having printed anything, and leaving the even thread hanging without ever having notified it.
Interesting. So initially the isEven = false. If the printOdd() is called first then the while (isEven) test is false so printOdd() will exit immediately without generating any output. The while loops in your first program only encompass the wait test, not the entire method.
Then when printEven() is called by the other thread, it will call wait() and hang since there is no other thread to call notifyAll().
You only should want the while loop around the wait since you are going to exit after you print out the even or odd number anyway, right? So the logic in the first program is correct.
public class CountDownApp
{
public static void main(String[] args)
{
Thread count1 = new CountDownEven();
Thread count2 = new CountDownOdd();
count1.start();
count2.start();
}
}
class CountDownEven extends Thread
{
public void run()
{
for(int i=10;i>0;i-=2)
{
System.out.print(+i+"-");
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class CountDownOdd extends Thread
{
public void run()
{
for(int i=9;i>0;i-=2)
{
System.out.print(+i+"-");
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

Categories

Resources