buttertop
buttertop

Reputation: 1

Java: Calling a class within a for loop

I've just started learning Java, so I apologize if this question is somewhat basic, and I'm sure my code is not as clean as it could be. I've been trying to write a small quiz program that will input a list of German verbs from a txt file (verblist.txt). Each line of the text file contains five strings: the German verb (verbger), the English translation (verbeng), the praeteritum and perfekt conjugations conjugations (verbprae and verbperf) and whether it uses haben or sein as the auxiliary verb (H or S, stored as verbhaben). The verb set is chosen by generating a random number and selecting the "row" of the two dimensional array. The GUI then displays the first two variables, and the user has to input the last three. If the last three match the values in the txt file, the user gets it correct and moves on to another verb.

I'm at the point where the code is working the way I want it to - for one verb. The way I've been organizing it is in two classes. One, VerbTable, imports the text file as a two dimensional array, and the other, RunVerb, generates the GUI and uses an ActionListener to compare the user input to the array. What I can't figure out now is how, after the user gets one verb set correct, I can then loop through the entire set of verbs.

I was thinking of creating a for loop that loops through the number of rows in the text file (saved in the code as height), generating a new random number each time to select a different verb set (or "row" in the two dimensional array.) Essentially, I'd like to get a loop to run through the entire VerbRun class, and pause for the ActionListener, until all of the verb sets have been displayed.

Here is the VerbTable class, which generates the array and random number to select the row:

package looptest;

import java.io.*;
import java.util.*;

public class VerbTable {
    public int width;
    public int height;
    public int randnum;

    public String verbger = new String("");
    public String verbeng = new String("");
    public String verbprae = new String("");
    public String verbperf = new String("");
    public String verbhaben = new String("");

    public VerbTable() {

        File file = new File("verblist.txt");
        try {
            /* For array height and width */
            Scanner scanner1 = new Scanner(file).useDelimiter("\n");
            int height = 0;
            int width = 0;
            while (scanner1.hasNextLine()) {
        String line = scanner1.nextLine();
        height++;
                String[] line3 = line.split("\t");
                width = line3.length;
            }
            System.out.println("Height: " + height);
            System.out.println("Width: " + width);
            this.width = width;
            this.height = height;
            /* Array height/width end */

            /* random number generator */       
            int randnum1 = 1 + (int)(Math.random() * (height-1)); 
            this.randnum = randnum1;
            /* random number generator end */

            Scanner scanner = new Scanner(file).useDelimiter("\n");

            String verbtable[][];
            verbtable = new String[width][height];

            int j = 0;
                while (scanner.hasNextLine()){

                    String verblist2 = scanner.next();
                    String[] verblist1 = verblist2.split("\t");
                    System.out.println(verblist2);
                    verbtable[0][j] = verblist1[0];
                    verbtable[1][j] = verblist1[1];
                    verbtable[2][j] = verblist1[2];
                    verbtable[3][j] = verblist1[3];
                    verbtable[4][j] = verblist1[4];
                    j++;
               }
                    this.verbger = verbtable[0][randnum];
                    this.verbeng = verbtable[1][randnum];
                    this.verbprae = verbtable[2][randnum];
                    this.verbperf = verbtable[3][randnum];
                    this.verbhaben = verbtable[4][randnum];
     }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }
    }    

public int getRand(){
    return this.randnum;
}      

public int getWidth(){
    return this.width;
}

public int getHeight(){
    return this.height;
}

public String getVerbger(){
    return this.verbger;
}

public String getVerbeng(){
    return this.verbeng;
}

public String getVerbprae(){
    return this.verbprae;
}

public String getVerbperf(){
    return this.verbperf;
}

public String getVerbhaben(){
    return this.verbhaben;
}

    public static void main(String[] args) throws IOException {
    }
}

And here is the RunVerb class:

package looptest;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*; 

public class RunVerb extends JFrame {
VerbTable dimensions = new VerbTable();
int width = dimensions.getWidth();
int height = dimensions.getHeight();
int randnum = dimensions.getRand();
String verbgerin = dimensions.getVerbger();
String verbengin = dimensions.getVerbeng();
String verbpraein = dimensions.getVerbprae();
String verbperfin = dimensions.getVerbperf();
String verbhabenin = dimensions.getVerbhaben();
String HabenSeinSelect = new String("");

public JTextField prae = new JTextField("",8);
public JTextField perf = new JTextField("",8);
public JLabel verbger = new JLabel(verbgerin);
public JLabel verbeng = new JLabel(verbengin);
public JRadioButton haben = new JRadioButton("Haben");
public JRadioButton sein = new JRadioButton("Sein");

public RunVerb() {
    JButton enter = new JButton("Enter");
    enter.addActionListener(new ConvertBtnListener());

    prae.addActionListener(new ConvertBtnListener());
    perf.addActionListener(new ConvertBtnListener());

    JPanel content = new JPanel();
    content.setLayout(new FlowLayout());

    content.add(verbger);
    verbger.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20));
    content.add(verbeng);
    verbeng.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 40));
    content.add(new JLabel("Praeteritum:"));
    content.add(prae);
    content.add(new JLabel("Perfekt:"));
    content.add(perf);

    content.add(haben);
    haben.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 10));
    haben.setSelected(true);
    content.add(sein);
    sein.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
    ButtonGroup bg = new ButtonGroup();
    bg.add(haben);
    bg.add(sein);

    content.add(enter);

    setContentPane(content);
    pack();                               
    setTitle("Verben");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null); 
}

class ConvertBtnListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        String outprae = prae.getText();
        int praenum = 0;
        if (outprae.equals(verbpraein)){
            praenum = 1;
        }

        String outperf = perf.getText();
        int perfnum = 0;
        if (outperf.equals(verbperfin)){
            perfnum = 1;
        }

        boolean habenselect = haben.isSelected();
        boolean seinselect = sein.isSelected();       

        if (habenselect == true){
            HabenSeinSelect = "H";
        }
        else {
            HabenSeinSelect = "S";
        }

        int habennum = 0;
        if (HabenSeinSelect.equals(verbhabenin)) {
            habennum = 1;
        }

        int numtot = praenum + perfnum + habennum;
        if (numtot == 3){
            System.out.println("Correct.");
        }
        else{
            System.out.println("Incorrect.");
        }
        numtot = 0;

    }
}
    public static void main(String[] args) {
        window.setVisible(true);
    }
}

So what would be the best way to cycle through the entire verbtable array until all of the rows have been displayed? Should I create a for loop, and if so, where should it go? Should I make a new class that contains the loop and references the VerbRun class? If so, what would be the best way to go about it?

I hope this makes sense! Thank you!

Upvotes: 0

Views: 1799

Answers (2)

Bhaskar
Bhaskar

Reputation: 7523

I would have a method like getNextRandomVerb() in then VerbTable class that generates the next verb to be shown. It will keep track of the verbs that have been shown ( for a given user-session ofcourse ) already and ensure the next one picked is not a repeat. Your RunVerb class seems to more responsible for managing the GUI , so this is not the place to define how to get the next verb to display.

Upvotes: 0

Adithya Surampudi
Adithya Surampudi

Reputation: 4454

To go through all the verbs exactly in a random order, you may not want to generate random numbers each time, as the random number can repeat. You have to create a random permutation of verbs, one way to do it is Collections.shuffle

see http://download.oracle.com/javase/6/docs/api/java/util/Collections.html

Also You dont have to create a new RunVerb Object, instead create once, and use setters to change the UI, and the functionality of Action Listeners. So pseudo code would be

  Collections.shuffle(verbsList);
  for(verb : verbsList)
  {
  setLabel1(verb[0]);
  setLabel2(verb[1]);...
  }

Upvotes: 1

Related Questions