java - How to loop through a text file and return a card object every two lines? -


so, i've got text file formatted have tried turn single cards , put them array, have put textfile , classes below show i've done:

a h 2 h 3 h 4 h 5 h 6 h 7 h 8 h 9 h t h j h q h k h d 2 d 3 d 4 d 5 d 6 d 7 d 8 d 9 d t d j d q d k d c 2 c 3 c 4 c 5 c 6 c 7 c 8 c 9 c t c j c q c k c s 2 s 3 s 4 s 5 s 6 s 7 s 8 s 9 s t s j s q s k s 

so comment i've added following classes:

card:

public class card {     public string number;     public string suit;      public card(string n, string s) {         number = n;         suit = s;     }      @override     public string tostring() {         return number + suit;     } } 

and deck (unfinished currently) -

import java.io.ioexception; import java.nio.file.files; import java.nio.file.paths; import java.util.arraylist; import java.util.list;  public class deck {   private arraylist < card > cards;    public deck() {     cards = new arraylist < card > ();   }    public list < card > builddeck() throws ioexception {      list < string > cardlines = files.readalllines(paths.get("cards.txt"));      // build card objects     list < card > result = new arraylist < card > ();     (int = 0; < cardlines.size(); += 2) { //if lines read smaller file read card       cards.add(new card(cardlines.get(i), cardlines.get(i + 1))); //add new card cards array     }     return result;   }    /*public void shuffle() {         // fill in     }*/  } 

and gui class -

import java.util.scanner;  public class game {   public scanner scan;    public void runmenu() {     string response;     {       printmenu();       system.out.println("what do:");       scan = new scanner(system.in);       response = scan.nextline().touppercase();       switch (response) {         case "1":           builddeck();           break;         case "2":           shufflecards();           break;         case "3":           dealcard();           break;         case "4":           movetoprevious();           break;         case "5":           move2pilesback();           break;         case "6":           amalgamateinmiddle();           break;         case "7":           playforme();           break;         case "8":           showlowscores();         case "q":           break;         default:           system.out.println("try again");       }     } while (!(response.equals("q")));   }    private void showlowscores() {     // todo auto-generated method stub   }    private void playforme() {     // todo auto-generated method stub   }    private void amalgamateinmiddle() {     // todo auto-generated method stub   }    private void move2pilesback() {     // todo auto-generated method stub   }    private void movetoprevious() {     // todo auto-generated method stub   }    private void dealcard() {     // todo auto-generated method stub   }    private void shufflecards() {     // todo auto-generated method stub   }    private void builddeck() {     // todo auto-generated method stub   }    private void printmenu() {     system.out.println("1 -  print pack ");     system.out.println("2 -  shuffle");     system.out.println("3 -  deal card");     system.out.println("4 -  move last pile onto previous one");     system.out.println("5 -  move last pile on 2 piles");     system.out.println("6 -  amalgamate piles in middle");     system.out.println("7 -  play me");     system.out.println("8 -  show low scores");     system.out.println("q - quit");   } } 

so after other errors appear fixed i've had comments, how can print out arraylist in deck class gui class option 1, thanks!

here's going retrieving cards file.

public static void main(string[] args) throws exception {     list<string> cardlines = files.readalllines(paths.get(yourcardfile));      // build card objects file     list<card> cards = new arraylist<>();     (int = 0; < cardlines.size(); += 2) {         cards.add(new card(cardlines.get(i), cardlines.get(i + 1)));     }      // print cards out     (card card : cards) {         system.out.println(card);     }      // need after have cards }  public static class card {     public string number;     public string suit;      public card(string n, string s) {         number = n;         suit = s;     }      @override     public string tostring() {         return number + suit;     } } 

results (not cards screen captured):

enter image description here


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -