Announcement

Collapse
No announcement yet.

[JavaBasic Help] Mảng và Scanner

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Java Script] [JavaBasic Help] Mảng và Scanner

    Tình hình là em có bài tập quản lý thẻ Visit thì gặp vấn đề ngay ở chổ nhập dữ liệu cho thẻ.
    code của em như sau:
    class VisitCard
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package lab7.edu;
    
    /**
     *
     * @author SagaNhim
     */
    public class VisitCard {
    	String Fullname;
    	String Company;
    	String Position;
    	String OfficePhone;
    	String Email;
    	String OfficeAdress;
    
    	VisitCard() {}
    
    	public String getCompany() {
    		return Company;
    	}
    
    	public void setCompany(String Company) {
    		this.Company = Company;
    	}
    
    	public String getEmail() {
    		return Email;
    	}
    
    	public void setEmail(String Email) {
    		this.Email = Email;
    	}
    
    	public String getFullname() {
    		return Fullname;
    	}
    
    	public void setFullname(String Fullname) {
    		this.Fullname = Fullname;
    	}
    
    	public String getOfficeAdress() {
    		return OfficeAdress;
    	}
    
    	public void setOfficeAdress(String OfficeAdress) {
    		this.OfficeAdress = OfficeAdress;
    	}
    
    	public String getOfficePhone() {
    		return OfficePhone;
    	}
    
    	public void setOfficePhone(String OfficePhone) {
    		this.OfficePhone = OfficePhone;
    	}
    
    	public String getPosition() {
    		return Position;
    	}
    
    	public void setPosition(String Position) {
    		this.Position = Position;
    	}
    
    	public VisitCard(String Fullname, String Company, String Position, String OfficePhone, String Email, String OfficeAdress) {
    		this.Fullname = Fullname;
    		this.Company = Company;
    		this.Position = Position;
    		this.OfficePhone = OfficePhone;
    		this.Email = Email;
    		this.OfficeAdress = OfficeAdress;
    	}
    	
    	
    }
    class VisitCardManage
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package lab7.edu;
    
    import java.io.IOException;
    import java.util.Scanner;
    
    /**
     *
     * @author SagaNhim
     */
    public class VisitCardManager {
    	
    	public static void main(String[] args)throws IOException {
    		VisitCard[] B = new VisitCard[10];
    		VisitCard D = new VisitCard();
    		inputData(D);
    		//inputVisitCard(B);
    	}
    	public static void inputVisitCard(VisitCard[] A) throws IOException{
    		int n;
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Nhap vao so luong VC: ");
    		n = sc.nextInt();
    		A = new VisitCard[10];
    		for (int i = 0; i < n; i++) {
    			inputData(A[i]);
    		}
    		
    	}
    	public static void inputData(VisitCard c) throws IOException {
    		Scanner sc = new Scanner(System.in);
    		//VisitCard c = new VisitCard();
    		checkFullName(c);
    		System.out.println("Nhap Company: ");
    		c.Company = sc.nextLine();
    		System.out.println("Nhap position: ");
    		c.Position = sc.nextLine();
    		System.out.println("Nhap Office Adress: ");
    		c.OfficeAdress = sc.nextLine();
    		checkOfficePhone(c);
    		checkEmailnull(c);
    		checkEmail(c);
    		}
    	public static void displayVisitCardByName(VisitCard c1) throws IOException{
    		System.out.println(c1.Fullname);
    		System.out.println("\n");
    		System.out.println(c1.Company);
    		System.out.println("\n");
    		System.out.println(c1.Position);
    		System.out.println("\n");
    		System.out.println(c1.OfficeAdress);
    		System.out.println("\n");
    		System.out.println(c1.OfficePhone);
    		System.out.println("\n");
    		System.out.println(c1.Email);
    		System.out.println("\n");
    	}
    	public static void displayAllVisitCard(VisitCard []A, int n) throws IOException{
    		for (int i = 0; i < n; i++) {
    			displayVisitCardByName(A[i]);
    			System.out.println("\n");
    		}
    		
    	}
    	private static void checkEmail(VisitCard c1) throws IOException{
    		if (c1.Email.indexOf('@')== -1) {
    			System.out.println("Email khong hop le, phai co @.");
    			checkEmailnull(c1);
    			checkEmail(c1);
    		}
    	}
    	private static void checkFullName(VisitCard c1) throws IOException{
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Full Name: ");
    		c1.Fullname = sc.nextLine();
    		if(c1.Fullname.length() == 0)
    		{
    			System.out.println("Full Name khong dc bo trong");
    			checkFullName(c1);
    		}
    	}
    	private static void checkOfficePhone(VisitCard c1) throws IOException{
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Office Phone: ");
    		c1.OfficePhone = sc.nextLine();
    		if(c1.OfficePhone.length() == 0)
    		{
    			System.out.println("Office Phone khong dc bo trong");
    			checkOfficePhone(c1);
    		}
    	}
    	private static void checkEmailnull(VisitCard c1) throws IOException{
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Email : ");
    		c1.Email = sc.nextLine();
    		if(c1.Email.length() == 0)
    		{
    			System.out.println("Email khong dc bo trong");
    			checkEmailnull(c1);
    		}
    	}
    }
    ==> em nhập 1 phần tử thì nó chạy ok, nhưng khi chuyển sang tạo mảng để nhập nhiều phần tử thì nó phát sinh lỗi ngay các dòng có sc.nextLine().
    => code
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package lab7.edu;
    
    import java.io.IOException;
    import java.util.Scanner;
    
    /**
     *
     * @author SagaNhim
     */
    public class VisitCardManager {
    	
    	public static void main(String[] args)throws IOException {
    		VisitCard[] B = new VisitCard[10];
    		VisitCard D = new VisitCard();
    		//inputData(D);
    		inputVisitCard(B);
    	}
    	public static void inputVisitCard(VisitCard[] A) throws IOException{
    		int n;
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Nhap vao so luong VC: ");
    		n = sc.nextInt();
    		A = new VisitCard[10];
    		for (int i = 0; i < n; i++) {
    			inputData(A[i]);
    		}
    		
    	}
    	public static void inputData(VisitCard c) throws IOException {
    		Scanner sc = new Scanner(System.in);
    		//VisitCard c = new VisitCard();
    		checkFullName(c);
    		System.out.println("Nhap Company: ");
    		c.Company = sc.nextLine();
    		System.out.println("Nhap position: ");
    		c.Position = sc.nextLine();
    		System.out.println("Nhap Office Adress: ");
    		c.OfficeAdress = sc.nextLine();
    		checkOfficePhone(c);
    		checkEmailnull(c);
    		checkEmail(c);
    		}
    	public static void displayVisitCardByName(VisitCard c1) throws IOException{
    		System.out.println(c1.Fullname);
    		System.out.println("\n");
    		System.out.println(c1.Company);
    		System.out.println("\n");
    		System.out.println(c1.Position);
    		System.out.println("\n");
    		System.out.println(c1.OfficeAdress);
    		System.out.println("\n");
    		System.out.println(c1.OfficePhone);
    		System.out.println("\n");
    		System.out.println(c1.Email);
    		System.out.println("\n");
    	}
    	public static void displayAllVisitCard(VisitCard []A, int n) throws IOException{
    		for (int i = 0; i < n; i++) {
    			displayVisitCardByName(A[i]);
    			System.out.println("\n");
    		}
    		
    	}
    	private static void checkEmail(VisitCard c1) throws IOException{
    		if (c1.Email.indexOf('@')== -1) {
    			System.out.println("Email khong hop le, phai co @.");
    			checkEmailnull(c1);
    			checkEmail(c1);
    		}
    	}
    	private static void checkFullName(VisitCard c1) throws IOException{
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Full Name: ");
    		c1.Fullname = sc.nextLine();
    		if(c1.Fullname.length() == 0)
    		{
    			System.out.println("Full Name khong dc bo trong");
    			checkFullName(c1);
    		}
    	}
    	private static void checkOfficePhone(VisitCard c1) throws IOException{
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Office Phone: ");
    		c1.OfficePhone = sc.nextLine();
    		if(c1.OfficePhone.length() == 0)
    		{
    			System.out.println("Office Phone khong dc bo trong");
    			checkOfficePhone(c1);
    		}
    	}
    	private static void checkEmailnull(VisitCard c1) throws IOException{
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Email : ");
    		c1.Email = sc.nextLine();
    		if(c1.Email.length() == 0)
    		{
    			System.out.println("Email khong dc bo trong");
    			checkEmailnull(c1);
    		}
    	}
    }
    ai biết giúp em với...
    GAME DEVELOPER

  • #2
    Chỗ hàm inputVisitCard bạn khởi tạo một mảng A 10 phần tử nhưng chưa khởi tạo cho từng phần tử của mảng, các phần tử của mảng hiện giờ vẫn là null, khi bạn truyền phần tử này vào inputData bạn đã truyền vào giá trị null, tất nhiên c.Company, c.Position sẽ gây lỗi.

    Bạn nên sửa lại trong vòng lặp là: A[i] = new VisitCard(); inputData(A[i]);

    Comment


    • #3
      Originally posted by 08520297 View Post
      Chỗ hàm inputVisitCard bạn khởi tạo một mảng A 10 phần tử nhưng chưa khởi tạo cho từng phần tử của mảng, các phần tử của mảng hiện giờ vẫn là null, khi bạn truyền phần tử này vào inputData bạn đã truyền vào giá trị null, tất nhiên c.Company, c.Position sẽ gây lỗi.

      Bạn nên sửa lại trong vòng lặp là: A[i] = new VisitCard(); inputData(A[i]);
      tks anh, đã chạy ngon rồi, nãy giờ em tìm hiểu thì thấy người ta nói dùng ArrayList, em chưa rõ lắm về vấn đề ArrayList, nếu anh biết có thể chi tiết co em biết đc ko, ví dụ vào đoạn code trên chẳng hạn
      GAME DEVELOPER

      Comment


      • #4
        Khi làm java thì mình thường hay sử dụng các collection util như ArrayList hay Set.
        Các HashTable (bảng băm) có thể hoạt động như một table trong csdl, với key và item

        PS: mà em để prefix là "Javascript" là sai rồi đấy, chỉ là Java thôi.
        Last edited by 08520099; 08-06-2012, 18:57.
        Waiting for the day my nickname get painted black and underlined ...!

        Comment


        • #5
          ArrayList thích hợp khi mình không biết chắc mảng sẽ có bao nhiêu phần tử.

          Như chương trình trên, bạn đã biết mảng của mình sẽ có n pt (n do người dùng nhập vào) thì chỉ việc A = new VisitCard[n]; cho khỏe (đừng giới hạn ở 10, vì có thể n > 10).

          Còn nếu không có n xác định chẳng hạn, thì mình có thể dùng ArrayList để chứa các VisitCard:

          import java.util.ArrayList;

          ArrayList<VisitCard> A = new ArrayList<>(); // tạo một ArrayList chứa các đối tượng VisitCard

          Lúc này phương thức inputData có thể viết lại như sau:

          public void inputData() {
          VisitCard c = new VisitCard();
          // ... nhập liệu cho c
          A.add(c); // thêm c vào mảng
          }

          Comment


          • #6
            Bạn tìm hiểu thêm về các Collection: ArrayList, Set, Hashtable... sẽ thấy chúng rất hữu ích và tiện lợi. Khi viết ứng dụng thường dùng khá nhiều đó ;-)

            Comment


            • #7
              Để cái nhãn bài viết cứ tưởng là JavaScript....
              Chưa....

              Comment


              • #8
                Các anh có thể Giúp em các phương thức Display dc không, nó lại sai nữa rồi (
                GAME DEVELOPER

                Comment


                • #9
                  Cái này là Lab7 mà, mình cũng nộp lab7 hả Toàn?
                  Đây là phương thức display theo tên và display tất cả trong class VisitCardManager
                  Code:
                  public void displayByName(){
                          System.out.print("Ten can tim: ");
                          [COLOR="#FF0000"]String Name = in.next();[/COLOR]
                          int found = 0;
                          for(int i = 0; i<listCard.length;i++){
                              if(listCard[i].getFullName().indexOf(Name) != -1){
                                  listCard[i].display();
                                  found ++;
                              }
                          }
                          if(found == 0) System.out.println("Khong tim thay Card nao phu hop");
                      }
                      public void displayAll(){
                          for(int i = 0; i<listCard.length;i++) listCard[i].display();
                      }
                  Trong hai phương thức này có gọi phương thức display() của class VisitCard

                  Code:
                  public void display(){
                          System.out.println("Full Name: "+ fullName);
                          System.out.println("Company: "+ Company);
                          System.out.println("Postion: "+ Position);
                          System.out.println("Office Phone: "+ officePhone);
                          System.out.println("Email: "+ Email);
                          System.out.println("Office Address: "+ officeAddress);
                      }
                  Ah tiện đây hỏi các anh luôn một điều mà em thắc mắc: Đôi lúc trong lập trình Java, em dùng phương thức nextLine() của Class Scanner nhưng khi Run hay Debug nó bỏ qua dòng lệnh này mà thực hiện dòng lệnh tiếp theo luôn, nhưng khi em sửa thành phương thức next() thì nó dừng lại cho mình nhập dữ liệu.

                  Ví dụ là trong code phía trên dòng lệnh mà em tô đỏ! Em phải thay thành in.next() nó mới chịu dừng lại.
                  University of Information Technology - Faculty of Computer Engineering
                  [ E ] 09520401@gm.uit.edu.vn || dangdx.es@gmail.com
                  [ F ] https://www.fb.com/dangdao261
                  :happy:

                  Comment


                  • #10
                    Originally posted by 09520401 View Post
                    Cái này là Lab7 mà, mình cũng nộp lab7 hả Toàn?
                    Đây là phương thức display theo tên và display tất cả trong class VisitCardManager
                    Code:
                    public void displayByName(){
                            System.out.print("Ten can tim: ");
                            [COLOR="#FF0000"]String Name = in.next();[/COLOR]
                            int found = 0;
                            for(int i = 0; i<listCard.length;i++){
                                if(listCard[i].getFullName().indexOf(Name) != -1){
                                    listCard[i].display();
                                    found ++;
                                }
                            }
                            if(found == 0) System.out.println("Khong tim thay Card nao phu hop");
                        }
                        public void displayAll(){
                            for(int i = 0; i<listCard.length;i++) listCard[i].display();
                        }
                    Trong hai phương thức này có gọi phương thức display() của class VisitCard

                    Code:
                    public void display(){
                            System.out.println("Full Name: "+ fullName);
                            System.out.println("Company: "+ Company);
                            System.out.println("Postion: "+ Position);
                            System.out.println("Office Phone: "+ officePhone);
                            System.out.println("Email: "+ Email);
                            System.out.println("Office Address: "+ officeAddress);
                        }
                    Ah tiện đây hỏi các anh luôn một điều mà em thắc mắc: Đôi lúc trong lập trình Java, em dùng phương thức nextLine() của Class Scanner nhưng khi Run hay Debug nó bỏ qua dòng lệnh này mà thực hiện dòng lệnh tiếp theo luôn, nhưng khi em sửa thành phương thức next() thì nó dừng lại cho mình nhập dữ liệu.

                    Ví dụ là trong code phía trên dòng lệnh mà em tô đỏ! Em phải thay thành in.next() nó mới chịu dừng lại.
                    uhm thanks Dạng, tao làm giúp thèn bạn. Vửa rớt môn DKTD buồn quá..
                    GAME DEVELOPER

                    Comment


                    • #11
                      Originally posted by 09520686 View Post
                      uhm thanks Dạng, tao làm giúp thèn bạn. Vửa rớt môn DKTD buồn quá..
                      nhưng mà có nộp không?
                      đktđ lớp mình te tua quá
                      University of Information Technology - Faculty of Computer Engineering
                      [ E ] 09520401@gm.uit.edu.vn || dangdx.es@gmail.com
                      [ F ] https://www.fb.com/dangdao261
                      :happy:

                      Comment


                      • #12
                        Xem thử 2 link này
                        Running the following in Eclipse initially caused Scanner to not recognize carriage returns in the console effectively blocking further input: price = sc.nextFloat(); Adding this line before the ...

                        I have been having trouble while attempting to use the nextLine() method from java.util.Scanner. Here is what I tried: import java.util.Scanner; class TestRevised { public void menu() { ...
                        Waiting for the day my nickname get painted black and underlined ...!

                        Comment

                        LHQC

                        Collapse
                        Working...
                        X