Can anyone pointed out what is wrong with this code. I got an annoying error something with Scanner method.

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

    public class cTedit implements ICommand{

        @Override
        public void Execute() {
            InputStream istream;
            OutputStream ostream = null;
            int x;
            final int EOF = -1;
            istream = System.in;

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

            File outFile = new File(cShell.Currentpath+"\\"+filename);
            System.out.println("Edit a file - Press Ctrl+z to end");

            try {

                ostream = new FileOutputStream(outFile);
                while((x = istream.read())!= EOF)
                    ostream.write(x);


            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("Error: " + e.getMessage());
            }
            finally{
                try{
                    istream.close();
                    ostream.close();
                }
                catch(IOException e)
                {
                    System.out.println("File did not close");
                }
            }
        }
    }

I also have another class where the main is:

    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("ls", new cLS());
                myhashData.put("gd", new cGD());
                myhashData.put("md", new cMD());
                myhashData.put("rnd", new cRND());
                myhashData.put("del", new cDEL());
                myhashData.put("hd", new cHD());
                myhashData.put("uhd", new cUHD());
                myhashData.put("ltf", new cITF());
                myhashData.put("nbc", new cNBC());
                myhashData.put("gdb", new cGDB());
                myhashData.put("Tedit", new cTedit());




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


                        Scanner scan = new Scanner(System.in);
                        String 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");
                        }



            }
    }

Recommended Answers

All 4 Replies

This is the complete error message...

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at cShell.main(cShell.java:35)

"an annoying error something with Scanner method"
It would be a lot more helpful if you gave us the exact complete error message, including the all-important line number(s)

(edit: this post should have appeared before the previous one!)

Scanners are the source of a lot of DaniWeb threads - they were intended to be simple, but too often don't work like you expect.
I suggest that since you are not using Scanner's parsing, why not just open a BufferedReader and use its readLine() method - far more predictable

you should have to try this:

InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

just import java.io.*;

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.