الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

JAVA if statement - ممكن مساعده في..؟

مغلق
بدأه نواره في 18 ديسمبر 2003 · 8 رد · 870 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله

اخواني في الله

محتاجة مساعدتكم .. وانا عضوة جديدة في المنتدى

حاليا انا مبتدأه في برمجة الجافا وكاتبه برنامج محتارة فيه في نقطة معينه ياريت تساعدوني فيها..

اريد ان استخدم

If statement

لثلاث حالات..

الحالة الأولى : اذا كتب المستخدم اسم الملف وبعده كتب:

(-a)

مثال:

file1.txt -a

يقرأ البرنامج اسم الملف و-a فيرتب الكلمات بالأحرف الأبجدية

الحالة الثانية : اذا كتب المستخدم اسم الملف وبعده كتب:

(-n)

file1.txt -n

يرتب الأرقام تصاعديا

الحالة الثالثة : اذا كتب المستخدم اسم الملف وبعده كتب:

(-nr)

يرتب الأرقام تنازلياً

انا بس اريد اعرف كيف اكتب الـحالات الثلاث (كيف اقول للبرنامج اقرأ ما كتبه المستخدم ويجزأ ماكتبه الى اسم الملف و نوع الترتيب)، عملية الترتيب سويتها الحمدلله.

ملاحظة:

By writing the command line flag (-a) the words will be listed in alphabetical order. Or the flags (-n/-nr) for the Descending/ ascending order

#2

اهلا بك اختي ......

جيد انت بداتي و نحن سوف نساعدك .... انت تعرفين ان الطريقة main يجب ان تعرفي لها مصفوفة تمثل ما يدخله المستخدم بعد اسم الملف يعني

public static void main(String [] arg)

لا حظتي String [] arg مصفوفة بالقيم التي يدخلها المستخدم اي في حالتك a او n او nr

فيصبح كالتالي....

if(arg[0].equals("a")) {       }
else if (arg[0].equals("n") ) {     }
else if (arg[0].equals("nr") ) {     }

و اي استفسار انا جاهز ......... ولو

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

#3

السلام عليكم

يا مرحبتين اخوي ..تسلم وماتقصر على الخدمة ..

راح أجربها حلاً .. لكن سؤال ..

المستخدم راح يدخل جملة وحده تشمل اسم الملف ووراه الحالة التي يريد يصف بها المعلومات..

مثلا :

file1.txt -a

وفي برنامجي حاليا عندي هذا الكود لاستيعاب الجملة من المستخدم:

TIO.putln("Please Enter the Input File that Holding  the text to be processed, followed by a flag (-a, -n, or -nr):  ");
inputFileName = TIO.getln().trim();

طبعا الـ getln راح تاخذ الجملة كامله بدون تجزيئها.. صح؟

فهمت علي؟؟

انا والله ماعرف حق الجافا كثير الـ C اتصور لي كانت اسهل..مع ان في شبه كبير

#4

السلام عليكم

يا مرحبتين اخوي ..تسلم وماتقصر على الخدمة ..

راح أجربها حلاً .. لكن سؤال ..

المستخدم راح يدخل جملة وحده تشمل اسم الملف ووراه الحالة التي يريد يصف بها المعلومات..

مثلا :

file1.txt -a

وفي برنامجي حاليا عندي هذا الكود لاستيعاب الجملة من المستخدم:

TIO.putln("Please Enter the Input File that Holding  the text to be processed, followed by a flag (-a, -n, or -nr):  ");
inputFileName = TIO.getln().trim();

طبعا الـ getln راح تاخذ الجملة كامله بدون تجزيئها.. صح؟

فهمت علي؟؟

انا والله ماعرف حق الجافا كثير الـ C اتصور لي كانت اسهل..مع ان في شبه كبير

في انتظار ردك

#5

use

String.substring(int Begin,int End);

ahabdeen.jpg

<marquee>

مش موجود بصفة دايمة للامتحانات

</marquee>

#6

السلام عليكم

يعطيك العافيه .. على المساعده ..

حاليا قربت اخلص من الكود .. بس طالعتلي مشكلة لا استطيع افهم لماذا!؟!؟!؟

يمكن اذا شفتوه تعطوني رايكم

الكود هو :

public static void main(String[] args)
      {
      
         String inputFileName;   // Input file name
         String outputFileName;  // Output file name

        TIO.putln("Please Enter the Input File that Holding  the text to be processed, followed by a flag (-a, -n, or -nr):  ");
        inputFileName = TIO.getln().trim();
            
         int l = inputFileName.length();
         
         int aflag;
         int tempp;
         for (int j=0;j<l;j++)
         {

          if (inputFileName[j].equals("-"))
          {tempp=j-1;
         	 
         	 if (inputFileName[j+1].equals("a"))  // Print Result in Alphabetical Order
         	 aflag= 1;
         	 
         	 else if (inputFileName[j+1].equals("n"))
            {
             if (inputFileName[j+2].equals("r")) // Print Result in Decending Order
              aflag= 3;  
             
             else      // Print Result in Ascending Order
              aflag= 2;
            }                     	 
          }
          
          
         }
        

inputFileName = inputFileName.substring(0, tempp);  


 try
         {
            in = new TReader(new FileReader(inputFileName));
         }
         catch (FileNotFoundException e)
         {
             TIO.putln("Sorry. File \"" + inputFileName + "\"was Not Found.");
             return;
         }


if(aflag==1)
         { }
         
         else if (aflag==3)  {}

         else if (aflag==2)	{}

والتعليق هو:

array required, but java.lang.String found

if (inputFileName[j].equals("-"))

#7
        String [] inputFileName;  
       String [] outputFileName;

علشان تعرفيهم على انهم String Array مش String بس

ahabdeen.jpg

<marquee>

مش موجود بصفة دايمة للامتحانات

</marquee>

#8

استبدلي كل

if (inputFileName[j].equals("-"))

بالسطر

if ( inputFileName.charAt(j)=='-')

مقارنة الشارز char اسرع من مقارنة الصب سترينق substring

#9

السلام عليكم يا أخوان

الصراحه ماقصرتو وجزاكم الله خير الحمدالله انحلت المشكله بعون الله ثم مساعدتكم لي .. ابي اطور البرنامج اللي عندي عشان يكون قادر على فهم punctuation مثل النقطه والفاصله لكن اذا جت الفاصله بنص الكلمه مايجزأ الكلمه ويعتبرها كلمه وحده !!!

مدري اذا فاهمين علي او لا انا راح اكتب لكم المطلوب وراح اسوي كوبي للكود اللي وصلت له لحد الحين

remove punctuation from input text at the start and end of words(i.e "the end" should be considered as one word) and (St.John is one word as well) a further command line flag ("-cn") will produce output in n columns equally spaced across the screen

   public class Freq
   {
   
      static WordData[] words;  // Array that holds the words from the file
                              
      static int wordCount;   // Number of words that are stored in the array
   
      public static void main(String[] args)
      {
      
         TReader in;    // A stream for reading from the input file
         PrintWriter out;  // A stream for writing to the output file
         String inFile;    // Input file name - array
         String flagtemp; 	 // String holds the flag
         String flagdash; 	 // String to check the flag start 
         String [] inputFileName= new String [50];   // Input file name - string
         String outputFileName;  // Output file name
         words = new WordData[10];  // Start with space for 10 words.
         wordCount = 0;             
                 
        
        // User enters The Input File 
         System.out.println("*This is a Word Frequency Counting Progam*");
         System.out.println("------------------------------------------");
         System.out.println("Please Enter the Input File followed by a flag (-a, -n, or -nr):  ");
         
         BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
         inFile =null;
         
         try{
          inFile=br1.readLine();
         }
         catch(IOException e)
         {
          System.out.println ("Sorry. File \"" + inFile + "\"was Not Found.");
          System.exit(1);
         }
          
         int filelen= inFile.length();
         flagtemp= inFile.substring((filelen-2),filelen);
         flagdash=flagtemp.substring(0,1);
         if (flagdash.equals("-"))
         inFile=inFile.substring(0, (filelen-3));
         
         else
         {
         flagtemp= inFile.substring((filelen-3),filelen);
         inFile=inFile.substring(0, (filelen-4));
        }
        
         
      try
         {
             in = new TReader(new FileReader(inFile));
         }
         catch (FileNotFoundException e)
         {
             System.out.println("Sorry. File \"" + inFile + "\"was Not Found.");
             return;
         }
         
    
   	 // User enters The Output File
         System.out.println("Please Enter the Output file that you to have the result in: ");
         
         BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
         outputFileName =null;
         
         //outputFileName = TIO.getln().trim();
         
         try{
          outputFileName=br2.readLine();
         }
         catch(IOException e)
         {
          System.out.println ("Sorry. File \"" + outputFileName + "\"was Not Found.");
          System.exit(1);
         }
         
         try
         {
            out = new PrintWriter(new FileWriter(outputFileName));
         }
         catch (IOException e)
         {
             System.out.println("Sorry. File \"" + outputFileName + "\" was Not Found for Output.");
             System.out.println(e.toString());
             return;
         }
                
         try {
            while (true)
            {
               while ( ! in.eof() && ! Character.isLetter(in.peek()) )
               in.getAnyChar();
               if (in.eof())
               break;
               insertWord(in.getAlpha());
            }
         }
         
         catch (TReader.Error e)
         {
            System.out.println("Error with Input File");
            System.out.println(e.toString());
            return;
         }
         
         
         /* Result in alphabetical order. */
       
        if(flagtemp.equals("-a"))
         {  
         out.println();
         out.println();
         out.println("Words in alphabetical order And the number of occurrences:");
         out.println("----------------------------------------------------------");
         out.println();
         putWordList(out);
        }
         
       /* Result Sorted in Decending order. */
       else if(flagtemp.equals("-nr"))
         {
         sortByFrequency1();
         
         out.println();
         out.println();
         out.println("Words in Decending order of frequency And the number of occurrences:");
         out.println("--------------------------------------------------------------------");
         out.println();
         
         putWordList(out);
        }
         
       /* Result Sorted in Acending order. */
       else if(flagtemp.equals("-n"))
         { 
         sortByFrequency2();
         
         out.println();
         out.println();
         out.println("Words in Acending order of frequency And the number of occurrences:");
         out.println("-------------------------------------------------------------------");
         out.println();
         
         putWordList(out);
         }
         
         
         if (out.checkError())
         {
            System.out.println("Some error occurred while writing to the output file.");
            System.out.println("The output might be missing, incomplete, or corrupted.");
         }
         
         else 
         {
            System.out.println("Done.");
         }
      
      } // end main()

مشكورين

هذا الموضوع مغلق.

مواضيع مشابهة