2014-02-27 86 views
2

我知道我必須使用Scanner類來首先創建掃描器對象來接收用戶在java中的輸入,但我不知道如何將多個單詞輸入到一個字符串變量中。例如,我有這樣的代碼在這裏:如何輸入字符串到Java?

System.out.println("Please enter the following information"); 

System.out.print("Author:"); 
author = input.nextLine(); 

System.out.print("Title:"); 
title = input.nextLine(); 

System.out.print("Grade Level:"); 
gradeLevel = input.nextDouble(); 


System.out.print("Pages:"); 
pages = input.nextDouble(); 

它根據許多論壇,應該允許用戶輸入整個行成字符串變量,但相反,它會跳過作者完全提示。我究竟做錯了什麼?

編輯:我已經添加下面的完整循環:

do { 

    System.out.printf("What kind of book is this?\n" 
      + "(0 = Textbook, 1 = Novel, 2 = Biography, 3 = Author Information, 4 = Other)"); 

    switch(input.nextInt()) { 
     case 0: 
      System.out.println("Please enter the following information."); 

      System.out.print("Author:"); 
      author = input.nextLine(); 

      System.out.print("Title:"); 
      title = input.nextLine(); 

      System.out.print("Grade Level:"); 
      gradeLevel = input.nextDouble(); 
      input.readLine(); 

      System.out.print("Pages:"); 
      pages = input.nextDouble(); 

      //Creation of textBooks 
      texts[i] = new Textbook(gradeLevel, author, pages, title); 

      boolText = true; 
      break; 
     case 1: 
      System.out.print("Please enter the following information.\n"); 

      System.out.print("Author:"); 
      author = input.nextLine(); 
      System.out.print("Genre:"); 
      genre = input.next(); 
      System.out.print("Title:"); 
      title = input.nextLine(); 
      System.out.print("Pages:"); 
      pages = input.nextDouble(); 

      //Creation of Novels 
      novels[i] = new Novel(genre, title, author, pages); 

      boolNovel = true; 
      break; 
     case 2: 
      System.out.print("Please enter the following information.\n"); 

      System.out.print("Author:"); 
      author = input.nextLine(); 
      System.out.print("Title:"); 
      title = input.nextLine(); 
      System.out.print("Subject:"); 
      subject = input.nextLine(); 
      System.out.print("Pages:"); 
      pages = input.nextDouble(); 

      //Creation of Biographies 
      bio[i] = new Biography(subject, title, author, pages); 

      boolBiography = true; 
      break; 
     case 3: 
      System.out.print("Please enter the following information.\n"); 

      System.out.print("Author:"); 
      author = input.nextLine(); 
      System.out.print("Email:"); 
      email = input.next(); 
      System.out.print("Address:"); 
      address = input.nextLine(); 
      System.out.print("Number of Titles:"); 
      numOfTitles = input.nextDouble(); 

      //Creation of Author Infortmation 
      authors[i] = new Author(author, address, email, numOfTitles); 

      boolAuthor = true; 
      break; 
     case 4: 
      System.out.println("Please enter the following information."); 

      System.out.print("Title:"); 
      title = input.nextLine(); 
      System.out.print("Author:"); 
      author = input.nextLine(); 
      System.out.print("Pages:"); 
      pages = input.nextDouble(); 

      //Creation of Author Infortmation 
      other[i] = new Books(title, author, pages); 

      boolOther = true; 
     default: 
      System.out.println("Program will now terminate."); 
      System.exit(1); 
    } 

    i++; 
    } 
    while (i < t); 

回答

1

你有這一個循環?緩衝區中可能還有一個/ n字符。在頂部,添加:input.nextLine();

+0

是的,對不起,這是在執行循環內的switch語句中。 – Brian

+0

是的!這就是訣竅!非常感謝你。 – Brian

4

按說這的工作,但很可能你是在一個循環中使用該代碼的更多。由於nextDouble不會消耗你將需要消耗該字符換行符明確

pages = input.nextDouble(); 
input.readLine() // added - consumes newline 
2

因爲你是一個循環

pages = input.nextDouble(); 
input.readLine() 

內部運行它這將解決這個問題。這就是爲什麼Author提示只是跳到下一個,因爲它已經得到了輸入。即\n(前一個雙輸入值的新行字符)

+0

我錯過了什麼,他說這是在一個循環? –

+0

通常,由於未消耗字符,在循環中會發生跳過問題 – Keerthivasan

+0

我嘗試添加input.readLine(),但netbeans給了我錯誤。根據我的理解,netbeans將每個詞作爲作者名稱作爲單獨的變量。 – Brian