JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Method : nextLine()
lonely741215

數學才是王道



發文: 196
積分: 0
於 2012-02-11 18:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
 
import com.deitel.ch17.AccountRecord;
 
public class CreateTextFile
{
   private Formatter output; // object used to output text to file
 
   // enable user to open file
   public void openFile()
   {
      try
      {
         output = new Formatter( "clients.txt" ); // open the file
      } // end try
      catch ( SecurityException securityException )
      {
         System.err.println(
            "You do not have write access to this file." );
         System.exit( 1 ); // terminate the program
      } // end catch
      catch ( FileNotFoundException fileNotFoundException )
      {
         System.err.println( "Error opening or creating file." );
         System.exit( 1 ); // terminate the program
      } // end catch
   } // end method openFile
 
   // add records to file
   public void addRecords()
   {
      // object to be written to file
      AccountRecord record = new AccountRecord();
 
      Scanner input = new Scanner( System.in );
 
      System.out.printf( "%s\n%s\n%s\n%s\n\n",
         "To terminate input, type the end-of-file indicator ",
         "when you are prompted to enter input.",
         "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
         "On Windows type <ctrl> z then press Enter" );
 
      System.out.printf( "%s\n%s", 
         "Enter account number (> 0), first name, last name and balance.",
         "? " );
 
      while ( input.hasNext() ) // loop until end-of-file indicator
      {
         try // output values to file
         {
            // retrieve data to be output
            record.setAccount( input.nextInt() ); // read account number
            record.setFirstName( input.next() ); // read first name
            record.setLastName( input.next() ); // read last name
            record.setBalance( input.nextDouble() ); // read balance
 
            if ( record.getAccount() > 0 )
            {
               // write new record
               output.format( "%d %s %s %.2f\n", record.getAccount(), 
                  record.getFirstName(), record.getLastName(),
                  record.getBalance() );
            } // end if
            else
            {
               System.out.println(
                  "Account number must be greater than 0." );
            } // end else
         } // end try
         catch ( FormatterClosedException formatterClosedException )
         {
            System.err.println( "Error writing to file." );
            return;
         } // end catch
         catch ( NoSuchElementException elementException )
         {
            System.err.println( "Invalid input. Please try again." );
            input.nextLine(); // ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
         } // end catch
 
         System.out.printf( "%s %s\n%s", "Enter account number (>0),",
            "first name, last name and balance.", "? " ); //★★★★★★★★★★★★★
      } // end while
   } // end method addRecords
 
   // close file
   public void closeFile()
   {
      if ( output != null )
         output.close();
   } // end method closeFile
} // end class CreateTextFile


請問在☆行的input.nextLine() 為什麼可以消掉之前輸入的資料呢?為什麼不是要求再輸入一筆資料,反而是在沒有任何出現要求輸入資料的情況下,反而直接跳到了★行呢?謝謝各位大大!
不知道為什麼,用兩台不同的電腦看我這篇文章,竟然行數會不一樣,所以就使用星星符號代替!!


lonely741215 edited on 2012-02-14 18:33
reply to postreply to post
數學啊數學~總有一天我要狂嗑你!!
作者 Re:Method : nextLine() [Re:lonely741215]
fredfred690





發文: 66
積分: 0
於 2012-02-13 23:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
88行?

reply to postreply to post
作者 Re:Method : nextLine() [Re:fredfred690]
lonely741215

數學才是王道



發文: 196
積分: 0
於 2012-02-14 13:06 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
厄,抱歉,看錯行了,已經更正,謝謝~Big Smile

reply to postreply to post
數學啊數學~總有一天我要狂嗑你!!
» JWorld@TW »  Java 新手區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8