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

تخزين بيانات من المستحدم داخل مصفوفة

بدأه PROGRAMMER_15 في 2 فبراير 2009 · 11 رد · 3,048 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

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

هذه محاولتي

البرنامج يقوم بشكل صحيح بالطلب من المستخدم إدخال عدد عناصر المصفوفة لكنه بعد ذلك يعطي الكمبايلر خطأ

أرجو المساعدة ولكم جزيل الشكر

package javaapplication13;
import java.util.Scanner;

public class Main {


	static Scanner input=new Scanner(System.in);
	static Scanner arraysize=new Scanner(System.in);
	public static void main(String[] args) {



	  System.out.println("Enter how many rwos you want in the Array");

	  int n;// Array size
	  n=arraysize.nextInt();
	  int r=n;

	  System.out.println("You have:"+r+"rows"+" "+"In your Array");

	  String f;
	  String[] invarray=new String[r];
	  invarray[r]=f;

	  for(int i=0;i<invarray.length;i++)
	  {
		  System.out.println("Enter the inventory information:");
	  f=input.nextLine();
		invarray=invarray[n];


	}
	  System.out.println(f);
	}

}
#2

بعد محاولات أعتقد أن الكود الآن صحيح فهو يطلب من المستخدم إدخال عدد عناصر المصفوفة ويخزن بيانات في المصفوفة بناء على عدد العناصر المدخل من قبل المستخدم.

أرجو الاطلاع على الكود . سؤالي الآن كيف يمكنني اتاحة فرصة للمستخدم بالبحث داخل المصفوفة؟

package javaapplication13;
import java.util.Scanner;
import javax.swing.*;

public class Main {


	static Scanner input=new Scanner(System.in);
	static Scanner arraysize=new Scanner(System.in);
	public static void main(String[] args) {



	  System.out.println("Enter how many rwos you want in the Array");

	  int n;// Array size
	  n=arraysize.nextInt();
	  int r=n;

   System.out.println("You have:"+r+"rows"+" "+"In your Array");

  String f;
	  String[] invarray=new String[r];


	  for(int i=0;i<invarray.length;i++)
	  {

		  System.out.println("Enter the inventory information Number:"+i);
  f=input.nextLine();
	  invarray=f;



	  }
		 for (int i=0, b=invarray.length; i<b; i++) {
	  if (i != 0) System.out.print(", ");
	  System.out.print(invarray);
	}
	  System.out.println();
	}

}

تم تعديل هذه المشاركة بواسطة PROGRAMMER_15 في 2 فبراير 2009 في 06:48

#3

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

يمكنك استعمال هذه الميثود


{
for (int i = 0; i < array.length; i++)
{
//if the string is found, return the actual index
if (array.equals(strToFind))
return i;
}
//if the loop ends, this meens the string was not found, so we return -1
return -1;
}
 public static int find(String[] array, String strToFind)

هذه الميثود تنتظر array والجملة اللتي تبتحث عنها

مثال :


if (index == -1)
System.out.println("No such element");
else
//your code
 int index = find(invarray, "The string you're looking for");

#4

كل الشكر أخي الكريم على تجاوبك . فقط لدي سؤال أخير أنا كتبت الكود كاملاً وقد أمضبت وقت طويل لكتابته ربما لأنني مبتدء في الجافا لذلك تفاجأت أن المطلوب هو أنه يجب علي تقسيم الكود إلى ميثود، كل ميثود لها عمل معين. وهذا هو السؤال:

2) Implement a class which simulates an inventory by creating and manipulating an array of part objects (I refer to this array as inventory array). The class has a main method and also the following methods:

a) A method which prompts the user for information about each of the parts and stores it in the inventory array.

b ) A method which displays the information for each part in the inventory array.

c) A method which searches the array for a given ID number and either prints the information available about the part or a message which says that the part is not in the array. The method also returns the subscript for the item (if it was found) or -1 (if it was not found).

d) A main method which first loads the inventory array with information from the user, then displays the inventory array and then (in a loop) allows the user to search for parts until the user wants to quit.

وهذا هو البرنامج الذي قمت بكتابته:

package javaapplication13;
import java.util.Scanner;

/**
 *
 * @authorAhmed
 */
public class Main {


	static Scanner inputid=new Scanner(System.in);
	static Scanner inputds=new Scanner(System.in);
	static Scanner inputqu=new Scanner(System.in);
	static Scanner arraysize=new Scanner(System.in);
	static Scanner stringfind=new Scanner(System.in);
	public static void main(String[] args) {



	  System.out.println("Enter how many rwos you want in the Array:");

	  int n;// Array size
	  n=arraysize.nextInt();
	  int r=n;


	  System.out.println("You have:"+" "+r+" "+"rows"+" "+"In your Array");

	  int A;// to get the ID
	  String E;// to get the DEscription
	  int C;// to get the Quantity
	  int[] invarrayID=new int[r];// First Array for ID Info.
	  String[] invarrayDS=new String[r];// Second Array for Description Info.
	  int[] invarrayQ=new int[r]; // Third Array fro Quantity Info.



	  for(int i=0;i<invarrayID.length;i++)// First loop for enter the ID
	  {

		  System.out.println("Enter the ID for the Inventory Item Number"+i+": ");
		  A=inputid.nextInt();
		  invarrayID=A;
	  }
		  for(int b=0;b<invarrayDS.length;b++)// Second loop for enter the Description
		  {
		  System.out.println("Enter the Description for the Inventory Item Number"+b+": ");
		  E=inputds.nextLine();
		  invarrayDS=E;
		  }
		  for(int t=0;t<invarrayQ.length;t++)// Third loop for enter the Quantity
		  {
		  System.out.println("Enter the Quantity for the Inventory Item Number"+t+": ");
		  C=inputqu.nextInt();
		  invarrayQ[t]=C;
		  }


	  // Next code for searching
	  int index=-1;
	  int g;
	  System.out.println("Enter the ID Number that you want to search for:");
	  g=stringfind.nextInt();

	  for(int i=0;i<invarrayID.length;i++)
		  if(invarrayID==g)
		  {
	  index=i;
	  break;
		   }
	  if(index!=-1)
		  System.out.println("Found:"+"ID:"+invarrayID[index]+", "+"Description:"+invarrayDS[index]+", "+"Quantity:"+invarrayQ[index]);

	  else
		 System.out.println("There is no information with this ID !!!");
	  System.exit(0);
	}

}

كيف يمكنني عمل المطلوب في السؤال وهل برنامجي صحيح.

تم تعديل هذه المشاركة بواسطة PROGRAMMER_15 في 4 فبراير 2009 في 00:43

#5

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

المطلوب منك خلق ميثود لكل عملية، ولكلك تقوم بكل شيء داخل الmain

سأصحح الكود وأجيبك بعد قليل

#6

كل الشكر أخي بانتظارك.

#7
	static Scanner inputid=new Scanner(System.in);
	static Scanner inputds=new Scanner(System.in);
	static Scanner inputqu=new Scanner(System.in);
	static Scanner arraysize=new Scanner(System.in);
	static Scanner stringfind=new Scanner(System.in);

مالفائدة من تعريف أكثر من سكانر ؟ ألا يكفي سكانر واحد ؟ < مبتدئة أخرى

#8

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

هذا حل ممكن


import java.io.InputStreamReader;
import java.io.IOException;

public class Main2
{
//We use BufferedReader to read the input for the user
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

int nbRows = 0; //the number of the rows in the inventory
int[] invarrayID;// First Array for ID Info.
String[] invarrayDS;// Second Array for Description Info.
int[] invarrayQ; // Third Array fro Quantity Info.

//the method create the arrays
void createInventoryArrays() throws IOException
{
System.out.print("Enter how many rows you want in the Array: ");
//the method readLine return a String, so we cast it to an int
nbRows = Integer.parseInt(in.readLine());
//we print the number of rows
System.out.println("You have: " + nbRows + " rows In your Array");
//we create the arrays
invarrayID=new int[nbRows];
invarrayDS=new String[nbRows];
invarrayQ=new int[nbRows];
}
//a) A Method which prompts the user for information about each of the parts and stores it in the inventory array
void fillInventory() throws IOException
{
for (int i = 0; i < nbRows; i++)
{
System.out.println("------------------");
System.out.println("Element " + (i+1));
System.out.println("------------------");
System.out.print("ID: ");
invarrayID = Integer.parseInt(in.readLine());
System.out.print("Description: ");
invarrayDS = in.readLine();
System.out.print("Quantity: ");
invarrayQ = Integer.parseInt(in.readLine());
}
}
//b ) A Method which displays the information for each part in the inventory array.
void displayInfos() throws IOException
{
for (int i = 0; i < nbRows; i++)
{
System.out.println("--------------------------------------------------------");
System.out.println(invarrayID + ": " + invarrayDS + ". Quantity:" + invarrayQ);
System.out.println("--------------------------------------------------------");
}
}
//c) A Method which searches the Array for a given ID number and either prints the information available about the part or a message
//which says that the part is not in the array. The Method also returns the subscript for the item (if it was found) or -1 (if it was not found).
int search() throws IOException
{
System.out.print("Enter the ID Number that you want to search for: ");
int idToSearch = Integer.parseInt(in.readLine());
for (int i = 0; i < nbRows; i++)
{
if (invarrayID == idToSearch)
{
//if the id is found, we print its informations
System.out.println(invarrayID + ": " + invarrayDS + ". Quantity:" + invarrayQ);
//we return the index of this id in the array
return i;
}
}
//if we reach the end of the loop, it meens the id wasn't found, so we print a message ans return -1
System.out.println("There is no information for the ID " + idToSearch + " !!!");
return -1;
}
//d) A main Method which first loads the inventory Array with information from the user,
//then displays the inventory Array and then (in a loop) allows the user to search for parts until the user wants to quit
public static void main(String[] args)
{
Main2 m = new Main2();
try {
m.createInventoryArrays();
//loads the inventory Array with information from the user
m.fillInventory();
//then displays the inventory Array
m.displayInfos();
//and then (in a loop) allows the user to search for parts until the user wants to quit
String answer = "no";
while (answer.equals("no"))
{
m.search();
System.out.print("Do you want to quit? (yes/no): ");
answer = in.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;

تم تعديل هذه المشاركة بواسطة herch في 4 فبراير 2009 في 01:57

#9

يعجز اللسان عن شكرك

الله يجزاك كل خير ويزيدك علم على علم يا رب.

#10

أخي الكريم

عندما وضعت الكود داخل Net Beans للتنفيد تظهر خطوط حمراء على الكثير من الأكواد مما يوحي بوجود أخطاء وعندما أضع الفأرة على هذه الخطوط يتم عرض التالي:

class, interface, or enum expected

وعندما اضغط زر تنفيذ يقول لي المترجم

javaapplication14.Main class wsan't found in javaapplication14 project

وشكراً

تم تعديل هذه المشاركة بواسطة PROGRAMMER_15 في 4 فبراير 2009 في 03:22

#11

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

أخي هل كان يعمل قبل إضافته إلى Netbeans؟؟

تأكد من أن class بc minuscule

تأكد من انك حددت الpackage في بداية الكود، لأنه في الكود الذي كتبت، لم أحدد ال package

حقيقة لا أدري مصدر الخطأ، فأنا ليس لدي Netbeans، ولكن تأكد من أن هذه الجملة موجودة في بداية الكود

package javaapplication13;

أو حسب الpackage الذي تستعمل

تأكد أيضا من إسم الclass، لأني في الكود وضعت Main2 وليس Main

تم تعديل هذه المشاركة بواسطة herch في 4 فبراير 2009 في 03:34

#12

البرنامج يعمل الأن

الخطأ كان ان كلمة class كانت مكتوبة بحرف كابيتل وايضا اسم الكلاس لدي معرف على أنه Main وليس Main2

البرنامج يعمل كل الشكر

تحياتي لك

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

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

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

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

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