top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java: Why sometimes while loop runs without reading the input inside the block?

0 votes
381 views

Like I have a tiny java game in which name of movie is hidden and we have to guess that. Here if your guess is correct that letter will appear at correct place and if wrong you will loose one chance. When we run the code, it runs sometimes without taking any input which it should not do. The java code is following.

import java.util.*;
import java.io.*;
import java.lang.*;
class StrDemo
{
    static StringBuilder change(StringBuilder sb)
    {
        StringBuilder sb1=new StringBuilder("");
        int len=sb.length();
        for(int i=0;i<len;i++)
        {
            if(sb.charAt(i)!=' ')
                sb1.append("*");
            else
                sb1.append(" ");
        }
        return sb1;
    }

    public static void main(String a[])
        throws Exception
        {
            StringBuilder sb2=new StringBuilder("hum sath sath hain");
            StringBuilder sb3=change(sb2);
            int j=0;
            StringBuilder sb4=new StringBuilder("Bollywood");

            while(true)
            {
                System.out.println(sb4);
                System.out.println(sb3);
                System.out.println("Enter Char");

                Scanner sc=new Scanner(System.in);

                char ch=(char)System.in.read();
                int c=0;
                int len=sb2.length();

                for(int i=0;i<len;i++)
                {
                    if(sb2.charAt(i)==ch)
                    {
                        sb3.setCharAt(i,ch);
                        c++;
                    }
                }

                if(c==0)
                    sb4.setCharAt(j++,'*');

                if(sb4.equals("*********"))
                    System.out.println("you lost...");
                else if(sb3.equals("hum sath sath hain"))
                    System.out.println("...u won");
            }   
        }
}
posted Dec 7, 2015 by Ratnam Gupta

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
 
Best answer

I hope I understood the problem correctly, if not then please comment so that I can provide correct pointer to solve the problem.

  1. Scanner sc is defined but not used in the program. I believe System.in.read() should be sc.read().
  2. You may need to skip the new line character using nextLine(); which may be causing the whole issue (Though I have not tested the program making a guess)

Again I am suggesting this based on the guess if I am wrong please comment and I will try to help.

answer Dec 7, 2015 by Salil Agrawal
its working with logic 2.thanx
Similar Questions
+1 vote

I'm using Python 2.7 under Windows and am trying to run a command line program and process the programs output as it is running. A number of web searches have indicated that the following code would work.

import subprocess

p = subprocess.Popen("D:PythonPython27Scriptspip.exe list -o",
 stdout=subprocess.PIPE,
 stderr=subprocess.STDOUT,
 bufsize=1,
 universal_newlines=True,
 shell=False)
for line in p.stdout:
 print line

When I use this code I can see that the Popen works, any code between the Popen and the for will run straight away, but as soon as it gets to the for and tries to read p.stdout the code blocks until the command
line program completes, then all of the lines are returned. Does anyone know how to get the results of the program without it blocking?

...