النتائج 1 إلى 2 من 2

الموضوع: Reading a string the user enters at a command-line prompt in Java

  1. #1
    elfr3on el3ashk الصورة الرمزية A7med Baraka
    تاريخ التسجيل
    Jun 2008
    الدولة
    Egypt - Cairo
    المشاركات
    4,695
    معدل تقييم المستوى
    10

    Thumbs up Reading a string the user enters at a command-line prompt in Java


    End Google Ads 201810 - BS.net 01 --> Reading interactive command-line input

    Introduction
    While Java is generally used to create applets, servlets, and general-purpose applications, you may occasionally need to create applications that interactively communicate with a user at a command-line, such as a Unix or DOS prompt. In these cases you normally see a prompt like this:

    • Enter your name: _


    While this is often the domain of scripting ********s, you may want to use Java for this purpose to take advantage of how easy it is to accomplish most tasks with Java - especially networking and database access.
    Unfortunately, this is one area where it's difficult to use vanilla Java methods. If you're used to simple echo and read statements, you're in for a bit of shock. I suspect the creators of Java didn't expect to see their ******** used for this purpose, or they just assumed developers would create their own classes to simplify this process.
    In this article we'll present a technique we use when creating Java programs that require interactive command-line input. If you like this basic technique, and would like to see expanded coverage of how to read numeric values, or would like to see a custom class for handling command-line input, just write us by using the link at the bottom of this article, and we'll provide a follow-up as quickly as we can

    Reading a string the user enters at a command-line prompt
    The basic technique of reading a String provided by a user at a command-line is fairly simple, but more lengthy than you'd expect. It involves the use of the System.in object, along with the InputStreamReader and BufferedReader classes. The code in Listing 1 shows how you can prompt the user to enter a String value, such as their name, and then read that value.

    كود:
    import java.io.*;  
    public class ReadString {  
       public static void main (String[] args) {  
          //  prompt the user to enter their name 
          System.out.print("Enter your name: ");  
          //  open up standard input 
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
          String userName = null;  
          //  read the username from the command-line; need to use try/catch with the 
          //  readLine() method 
          try { 
             userName = br.readLine(); 
          } catch (IOException ioe) { 
             System.out.println("IO error trying to read your name!"); 
             System.exit(1); 
          }  
          System.out.println("Thanks for the name, " + userName);  
       }  
    }  // end of ReadString class
    Listing 1: The ReadString.java program shows how you can prompt the user to enter their name, and then read the name into the userName variable.

    Listing 1 demonstrates how you can print a prompt to the user using the System.out.print() method. Notice that we use the print() method instead of println(). Using the print() method lets us keep the cursor on the same line of output as our printed text. This makes it look like a real prompt (instead of having the user's response appear on the line below our prompt).
    Next, we read the user's input by passing the System.in object to the InputStreamReader, and then into the BufferedReader. The BufferedReader class gives us the readLine() method, and applies buffering to the input character input stream. Notice that the readLine() method can thrown an IOException error, so we have to enclose the statement in a try/catch statement.



  • #2
    Wa7ed Men el NaSS الصورة الرمزية wa7edmenelnass
    تاريخ التسجيل
    Jun 2008
    الدولة
    CMasterZ
    المشاركات
    260
    معدل تقييم المستوى
    250

    افتراضي رد: Reading a string the user enters at a command-line prompt in Java

    Thanks alot
    Nice Work ya Man !
    Yours
    CMasterZ...
    For Web Services
    Hosting, Domain, Design, Develop, VB Support
    منتديات سي ماسترز التقنية

  • معلومات الموضوع

    الأعضاء الذين يشاهدون هذا الموضوع

    الذين يشاهدون الموضوع الآن: 1 (0 من الأعضاء و 1 زائر)

    المواضيع المتشابهه

    1. Linux command complete reference - e-book
      بواسطة A7med Baraka في المنتدى linux - لينوكس
      مشاركات: 1
      آخر مشاركة: 11-27-2008, 06:50 PM
    2. Guide to ArgoUML - how to user argoUML -
      بواسطة A7med Baraka في المنتدى برمجيات أخرى
      مشاركات: 0
      آخر مشاركة: 11-12-2008, 08:16 PM
    3. Java Command Line Arguments In Eclipse IDE
      بواسطة A7med Baraka في المنتدى Java - جافا
      مشاركات: 0
      آخر مشاركة: 11-02-2008, 04:34 AM
    4. try/catch Statement in java
      بواسطة A7med Baraka في المنتدى Java - جافا
      مشاركات: 0
      آخر مشاركة: 10-31-2008, 12:28 AM

    الكلمات الدلالية لهذا الموضوع

    مواقع النشر (المفضلة)

    ضوابط المشاركة

    • لا تستطيع إضافة مواضيع جديدة
    • لا تستطيع الرد على المواضيع
    • لا تستطيع إرفاق ملفات
    • لا تستطيع تعديل مشاركاتك
    •  
    "وَقُل رَّبِّ زِدْنِي عِلْمًا"
    أعلانات نصية أستضافة , ريسيلر - Best Hosting | BarakaSoft Web Solutions

    BarakaSoft PageRank RSS RSS 2.0 XML MAP HTML 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 C/C++ | Java | C# | Network | Database | OS | Linux | Windows | Hacker & Security | Photoshop | Flash | Web Development | Free Programs | Mobile App | Free Java Course | Latest Technical News | Internet Programs | Antiviurse Programs | Graphics Programs | Network Programs | Portable Programs | vb Forums Development | Forums Development | CMS(Joomla-nuke-wordpress-mkportal...) | Photo | Anime |