Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

the following is my sources code:

package functiontest;

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FunctionTest {

public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
    int option;
    String cname, cpassword="testpassword",chp="010-000000";

//        cname="clerk test";

    System.out.println("1. add");
    System.out.println("2. delete");
    option = scan.nextInt();
    switch(option)
    {
        case 1:
            System.out.print("Enter clerk name:");
            cname = scan.nextLine();
            File cfile = new File("clerk/"+cname+".txt");
               FileWriter cwrite;
               try
               {
                   cwrite = new FileWriter(cfile);
                   BufferedWriter cbuf = new BufferedWriter(cwrite);
                   cbuf.write("clerk name:" +cname);
                   cbuf.write("clerk password:"+cpassword);
                   cbuf.write("clerk handphone number:"+chp);
                   cbuf.flush();
                   cbuf.close();
                   System.out.println("The clerk profile create completely");
               }catch(IOException e)
               {
                   System.out.println("Error! clerk profile cannot create");
               }
            ;break;
        case 2:
            String dclerk;
//                dclerk = "clerk test";
                System.out.print("
Please enter clerk name for delete:");
                dclerk = scan.next();
                File dcfile = new File("clerk/"+dclerk+".txt");
                if(!dcfile.exists()) 
                {
                    System.out.println("Error! the clerk profile not exist");
                }
                try
                {
                    dcfile.delete();
                    System.out.println(dclerk + "'s prifle successful delete");
                }catch(Exception e)
                {
                    System.out.println("Something wrong! " + dclerk +" profile cannot delete");
                    };break;
        }
    }
}

i cannot the enter the variable name cname

        cname = scan.nextLine()

when i run the program it show the result as below:

run:
1. add
2. delete
1
Enter clerk name:   The clerk profile create completely

when i use .next():

cname = scan.next()

it cannot read the

cname

with spaces, example

clerk test 

it will read

clerk 

only how should i do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
288 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your problem is that the line

option = scan.nextInt();

does not read the new line character after the integer. So that new line is finally read when you do nextLine(), later on.

To fix this, you should add an extra

scan.nextLine()

after the call to nextInt(), then use

cname = scan.nextLine();

when you want to read the clerk's name.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...