Kaka
Kaka

Reputation: 37

Program looping infinitly

I wrote program that take command from user and perform particular functionality. However, there is something wrong with the functionality read and write input to file which cause the loop to run indefinitely.

 import java.util.HashMap;
 import java.util.Scanner;


 public class cShell{

    static String Currentpath="C:\\";
    public String Current = Currentpath;

    static HashMap<String, ICommand> myhashData=new HashMap<String, ICommand>();

    public static void main(String[] args)
    {


    myhashData.put("ltf", new cITF());
    myhashData.put("nbc", new cNBC());
    myhashData.put("gdb", new cGDB());
    myhashData.put("Tedit", new cTedit());

    do
    {


            System.out.print(Currentpath+"> ");

            String Input = null;
            Scanner scan = new Scanner(System.in);
            if(scan.hasNext()){
                Input = scan.nextLine().trim();

            }

            //if(Input.equals("exit")){
            //  System.exit(0);
            //}

            if(myhashData.containsKey(Input))
            {
                ICommand myCommand=myhashData.get(Input);
                myCommand.Execute();
            }
            else
            {
                System.out.println("Invalid Command");
            }


    }while(!"Input".equals("exit"));
  }
}

And here is the class which provide the functionality for read and write.

 import java.util.*;
 import java.io.*;
 //import java.lang.System.*;

 public class cTedit implements ICommand{

     @Override
     public void Execute() {
    // TODO Auto-generated method stub


         System.out.println("Enter the file name to be edited");
         Scanner scan = new Scanner(System.in);
         String filename = scan.nextLine();

                 InputStreamReader cin = null;
         FileWriter out = null;

    try{
        cin = new InputStreamReader(System.in);
        out = new FileWriter(cShell.Currentpath+"\\"+filename);
        System.out.println("Enter character, 'q' to quit");
        char c;

        do{
            c = (char) cin.read();
            out.write(c);
        }while(c!= 'q');
    }
    catch(Exception e){
        System.out.println("Error");
    }
    finally{

            try{
            cin.close();
            out.close();
        }
        catch(IOException e)
        {
            System.out.println("File did not close");
        }
             }
       }
  }

The problem is that after the reading and writing, the program output the message "Invalid Command" which is defined inside the class cShell. Can anyone point to me where this the cause..??

Upvotes: 0

Views: 63

Answers (2)

Aniket Thakur
Aniket Thakur

Reputation: 68915

Change

while(!"Input".equals("exit"));

to

while(!Input.equals("exit"));

As "Input" can never be equal to "exit" condition is always true and hence it loops infinitely.

For NPE you can add null checks

finally{

        try{
        if(cin != null)
           cin.close();
        if(out != null)
           out.close();
    }
    catch(IOException e)
    {
        System.out.println("File did not close");
    }
   }

Upvotes: 2

Christian Tapia
Christian Tapia

Reputation: 34146

The do-while loop will run forever becauses its termination condition is:

!"Input".equals("exit")

The string "Input" will never be equal to the string "exit". You may want to use the variable Input instead:

!input.equals("exit")

Note:

  • Try to follow Java naming conventions. Use 'mixedCase' for methods/variables and use 'CamelCase' for classes/interfaces. In other words, the variable name should be input, not Input.

Upvotes: 3

Related Questions