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

سؤال حول الاستفادة من الاستثناءات في اعادة تنفيذ Step ما

بدأه Xacker في 15 سبتمبر 2008 · 4 رد · 669 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هل هناك طريقة او بشكل آخر ما أفضل طريقة للاستفادة من التقاط استثناء ما في اعادة تنفيذ خطوة ما مع بقاء امكانية الاستفادة من الاستثناء مرة اخرى.. غير واضح؟

عارف :blush:

مرة أخرى وبكود

String st =  JOptionPane.showInputDialog ("Enter a number");
try
{
	 int ist = Integer.parseInt (st);
} catch (Exception e) {
	 System.out.println ("Exception detected, exception message: " + e);
	 System.exit(0);
}

ما ابحث عنه امكانية اجبار المستخدم على ان يقوم بادخال الرقم مرة اخرى مع الانتباه الى فحصه مجدداً وهكذا الى ان يقوم بادخال رقم صحيح تماماً لتجاوز الاستثناء دون ان اضطر الى ان اقوم باغلاق البرنامج.

فما الطريقة لذلك؟

شكراً :)

تم تعديل هذه المشاركة بواسطة Xacker في 15 سبتمبر 2008 في 21:29

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#2

لعلي أكون قد فهمت ما تقصد .. هل هذا ما تريده :

String st =  JOptionPane.showInputDialog ("Enter a number");
boolean done = false;
while(!done)
{
try
{
	 int ist = Integer.parseInt (st);
	done = true;
} catch (Exception e) {
	 System.out.println ("Exception detected, exception message: " + e);
	  // done = false;
}
}// while

logo1.png تطبيق طمأنينة ، نسخة بيتا على أندرويد

عبدالله الشمّري - Al-Shammari

CodingAlone.com

twitter @abshammeri

abshammeri AT gmail.com

github : abshammeri

#3

نسيت!

صحيح ;)

يسلمو ايديك يا باشا, تمت الإجابة :)

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#4

بارك الله فيك أخي الشمري -سبقتني إلى نفس الفكرة :) -

#5

الآن وبعد انتهاء الوقت المخصص لتسليم الوظيفة أطرحها لعل أحداً ما يستفيد منها في يوم من الأيام (رغم أنها متواضعة وبسيطة :blush:)

// برنامج لقراءة مصفوفتين, اسماء طلاب وعلاماتهم
// طباعة العلامات والاسماء في جدول
// الترتيب والطباعة تنازليا حسب العلامات
// الترتيب والطباعة تصاعديا حسب الاسماء
// اظهار اعلى علامة لطالب في القسم
// اظهار علامات واسماء الطلاب الراسبين
// a program to store students names and marks
// then print out some specific information about the process
import javax.swing.JOptionPane;

public class hm02
{
	// declare public vars
	static int istNumber;
	static String[] names; // array of strings for students names
	static int[] marks; // array of integers for students marks
	static String stNumber;

	public static void main (String[] args)
	{
		// call a subroutine to receive students numbers to enter
		ReadInteger();
		// call a subroutine to receive students names
		ReadNames();
		// call a subroutine to receives students marks
		ReadMarks();
		// call a subroutine to print students names and marks in a table
		System.out.println ("You have entered the following inforamtion:");
		PrintTable();
		// call a subroutine to sort students table by marks descending and print the table
		SortIntArray();
		System.out.println ("Students table information arranged in descending order by mark:");
		PrintTable();
		// call a subroutine to sort students table by names ascending and print the table
		SortNamesArray();
		System.out.println ("Students table information arranged in ascending order by name:");
		PrintTable();

		SortIntArray();
		// print out first student on class, the student with highest mark & ofcourse who has mark +60
		if (marks[0] > 59)
		{
			System.out.println (names[0] + " is first on class with total of " + marks[0] + " marks.\n");
		} else {
			System.out.println ("None is first on class because no one has passed!\n");
		}

		System.out.println ("Students that failed:");
		int j = 0;
		// search the table for marks less than 60 and print them as failed students
		for (int i = 0; i <= istNumber-1; i++)
		{
			if (marks < 60)
			{
				j += 1;
				System.out.println (" " + j + ". " + names + " with total of " + marks + " marks."); // print out failed students names with marks
			}
		}
	}

	// fault detection subroutine
	static String isEmpty(String num, String msg)
	{
		if (num == null || num.trim().length() == 0)
		{
			do // repeat
			{
				num = JOptionPane.showInputDialog ("Invalid input; " + msg);
			} while (num == null || num.trim().length() == 0); // as long as input is invalid
		}
		return num;
	}

	static void ReadInteger()
	{
		boolean done = false;
		while (!done)
		{
			try
			{
				// receive students number from user.
				stNumber = JOptionPane.showInputDialog ("How many students do you want to enter?");

				// check if input is empty or has been cancelled by user
				stNumber = isEmpty(stNumber, "How many students do you want to enter"); // passes a special msg to display for each fault

				// convert input from String to Integer, using structered exception handler to detect invalid char input
				istNumber = Integer.parseInt (stNumber);
				done = true;
			} catch (Exception e) { // catch exception if it detects invalid char during conversion
				// prints out neat and clear message to inform user with the exception type.
				System.out.println ("Exception occured, possible reason: invalid input, suggest: use numbers only");
				System.out.println ("Exception message: " + e + "\n\n");
			} finally {
				// check if number is within the valid range for Integer types
				if (done == true && istNumber <= 0 || istNumber > 65535)
				{
					System.out.println ("Valid numbers range is [1 - 65535]\n\n"); // inform the user with valid range
					done = false;
				}
			}
		}

		// inform the user with the number he has given
		System.out.println ("You want to enter " + istNumber + " students inforamtion\n\n");
	}

	static void ReadNames()
	{
		names = new String [istNumber]; // declare an array of strings with length specified by number of students
		for (int i = 0; i <= istNumber-1; i++)
		{
			names = JOptionPane.showInputDialog ("Student number " + (i+1) + " name is?"); // ask the user to enter a student name
			names = isEmpty(names, "Student number " + (i+1) + " name is?"); // check if the given value is empty or null
		}
		// finished filling all students names, return
	}

	static void ReadMarks()
	{
		marks = new int [istNumber]; // declare an array of integers with length specified by number of students
		for (int i = 0; i <= istNumber-1; i++)
		{
			boolean done = false;
			while (!done)
			{
				String m = JOptionPane.showInputDialog (names + " mark is?"); // ask the user to enter a student mark
				m = isEmpty(m, names + " mark is?"); // check if the given value is empty or null
				try { // use exception handling to decide if the given value is an integer or contains invalid chars
					marks = Integer.parseInt (m);
					done = true;
				} catch (Exception e) { // catch exception if it detects invalid char during conversion
					// prints out neat and clear message to inform user with the exception type.
					System.out.println ("Exception occured, possible reason: invalid input, suggest: use numbers only");
					System.out.println ("Exception message: " + e + "\n\n");
				} finally {
					if (done == true && marks < 0 || marks > 100) // check if mark is a negative number, or over 100
					{
						System.out.println ("Student can't have a negative mark or mark over 100!\n");
						done = false;
					}
				}
			}
		}
	}

	static void PrintTable()
	{
		// print out table header
		System.out.println ("|		Student Name			|		Student Mark		|");
		System.out.println ("============================================================================
=");
		// enter a loop to print all students names & marks
		for (int i = 0; i <= istNumber-1; i++)
		{
			System.out.print (" " + (i+1) + ". "); // print a preceeding number for student
			System.out.print (names); // print student name
			for (int j = 0; j <= (35-names.length()); j++) // decide how many spaces to print after student name to fit the table column width;)
			{
				System.out.print (" "); // add trailing spaces
			}
			System.out.println ("				  " + marks); // print student mark exactly where we want it to appear, middle of the student mark column
		}
		System.out.println ("\n\n"); // add 2 line breaks for best view
	}

	static void SortIntArray()
	{
		// declare some vars
		int m;
		String n;
		for (int i = 0; i <= istNumber-1; i++) // index  first element in the marks array
		{
			for (int j = i+1; j <= istNumber-1; j++) // value to compare to as index+1 in marks array
			{
				// assume marks is maximum mark, compare it with the following array element
				if (marks < marks[j]) // check if the following element is higher
				{
					// swap between two values, put highest value on first array element to comapre it with all the array elements left
					m = marks; // save it
					marks = marks[j]; // move highest value to lowest value space
					marks[j] = m; // restore lowest value to highest value old space
					n = names; // do the same with students names to match their marks
					names = names[j];
					names[j] = n;
				}
			}
		}
	}

	static void SortNamesArray()
	{
		// declare some vars
		int s, m;
		String n;
		for (int i = 0; i <= istNumber-1; i++) // a bulk loop to go over all array elements.
		{
			for (int j = 1; j <= names.length-1; j++) // index second element in the names array
			{
				s = names[j].compareTo(names[j-1]); // compare index value with previous array element value;e.g: index with index-1
				if (s < 0)
				{
					// swap between names in the array
					m = marks[j]; // save it
					marks [j] = marks[j-1]; // move name at index-1 to index
					marks[j-1] = m; // restore saved name to index-1
					n = names[j]; // do the same with students marks to match their names
					names[j] = names[j-1];
					names[j-1] = n;
				}
			}
		}
	}
}

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…