1

Ive written this code for a programming class, its complete and correct im just interested in this for my own knowledge. How can I have this program run outside of Eclipse and place both the scanner and output into a JFrame or something similar? Any comments and advice would mean a lot. I am very new to java, this is my third program.

This is the main class

    import java.util.Scanner;
    public class SeqGenTest 
    {

/**
 * Instance Variables passed to SeqGen.java
 */
String max;
String minmax;
String maxdiv;
boolean loop = true;
int a;
int b;
int c;
/**
 * main method that allows user input to select and execute commands
 * based upon the input of said user
 * @param max, minmax, maxdiv,
 * @return ints based upon calculation
 */
public static void main(String[] args) 
{
    boolean loop = true;
    while(loop = true)
    {
    System.out.println("Enter a command (max, minmax, maxdiv, end)");
    Scanner inpt = new Scanner(System.in);
    String userInpt = inpt.nextLine();

    if (userInpt.equalsIgnoreCase("max"))
    {
        System.out.println("Please enter your Max (intergers)");
        int maximum = 0; //= inpt.nextInt();
        try {
            maximum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        if (maximum <= 0)
        {
            System.out.println("Error, empty sequence");
        }
        else if (maximum < -9 || maximum > 200)
        {
            System.out.println("Error, bounds exception");
        }
        else if (maximum > 0 && maximum < 201)
        {
        System.out.println("Here is your sequence:");
        SeqGen x = new SeqGen (maximum);
            while(x.hasNextValue()){
            System.out.println(x.getNextValue());}
        }
        }
    else if (userInpt.equalsIgnoreCase("minmax"))
        {
        System.out.println("Please enter your min, max (intergers)");
        int minimum = 0;
        int maximum = 0;
        try {
            minimum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        try {
            maximum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        if (minimum > maximum)
        {
            System.out.println("Error, empty sequence");
        }
        else if (maximum < -9 || maximum > 200 || minimum < -9 || minimum > 200)
        {
            System.out.println("Error, bounds exception");
        }
        else if (maximum > 0 && maximum < 201 && minimum > 0 && minimum < 201)
        {
        System.out.println("Here is your sequence:");
        SeqGen x = new SeqGen (minimum, maximum);
            while(x.hasNextValue()){
            System.out.println(x.getNextValue());}
        }
        }
    else if (userInpt.equalsIgnoreCase("maxdiv"))
        {
        System.out.println("Please enter your min, max, div (intergers)");
        int minimum = 0;
        int maximum = 0;
        int divisor = 0;
        try {
            minimum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        try {
            maximum = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        try {
            divisor = inpt.nextInt();    
        } catch (Exception e) {
            System.out.println("The value you have input is not an integer");
            inpt.close();
            return;
        }
        if (maximum < minimum || minimum < 1 || divisor > maximum)
        {
            System.out.println("Error, empty sequence");
        }
        else if (maximum < -9 || maximum > 200 || minimum < -9 || minimum > 200 || divisor < -9 || divisor > 200)
        {
            System.out.println("Error, bounds exception");
        }
        else if (maximum > 0 && maximum < 201 && minimum > 0 && minimum < 201 && divisor > 0 && divisor < 201)
        {
        System.out.println("Here is your sequence:");
        SeqGen x = new SeqGen (minimum, maximum, divisor);
            while(x.hasNextValue()){
            System.out.println(x.getNextValue());}
        }
        }
    else if (userInpt.equalsIgnoreCase("end"))
        {
        System.out.println("Program Terminated Normally");
        inpt.close();
        System.exit(0);
        }
    else
        {
        System.out.println("Error, bounds exception. System will now     exit");
        inpt.close();
        System.exit(0);
        }

    }   
    }
    }

Here is the "other class?" im not sure what the proper term is.

    public class SeqGen
{
//instance vars
int minmax;
int max;
int maxdiv;
int current;
boolean hasNextValue;
boolean nextValue;
boolean isMax;
boolean isMinMax;
boolean isMaxDiv;
int getNextValue;
/**
 * Constructor for objects of class SeqGen
 */
public SeqGen(int a)
{
     minmax = 1;
     max = a;
     maxdiv = 1;
     current = minmax;
     isMax = true;

}

/**
 * Constructor for objects of class SeqGen 
 */
public SeqGen(int b, int a)
{
     minmax = b;
     max = a;
     maxdiv = 1;
     current = minmax;
     isMinMax = true;
}
public SeqGen(int b, int a, int c)
{
    minmax = b;
    max = a;
    maxdiv = c;
    current = minmax;
    isMaxDiv = true;

}

public boolean hasNextValue()
{
    if (current <= max) {return true; }
    else {return false;}
}
/**
 * Computes math based upon user entered commands
 * @return correct computation (int)
 */
public int getNextValue()
{
    while (current < max)
    {
        if (current%maxdiv == 0)
       {
         return current++;
       }
    else
       {
         current++;
       }
    }   
    return current++;

}
}
univr
  • 11
  • 3

2 Answers2

0

Basically I can think of two options:

  • Extend the JFrame class and use an embedded console with a JTextArea as shown on this answer.

  • Extend the JFrame class and make a frame that has a button to execute the selected command from a JCombobox that has all the different commands, a bunch of JTextField to grab the user input of the values and use a JLabel to print out the results.

    public class SeqGenTest extends JFrame{
    
        public SeqGenTest(){
    
            //set up all you UI here.
    
            //use these to show the frame.
            setSize(600, 600);
            setVisible(true);
        }
    }
    

Addional info:

Community
  • 1
  • 1
Jalal Sordo
  • 1,605
  • 3
  • 41
  • 68
0

You can use git bash, cmd, or anything of the like to run it and still get standard input and standard output. All you have to do is export it as a runnable jar, then run the following command:

java -jar myProgram.jar

Just replace myProgram.jar with the name of your jar, and make sure you're in the same directory as the jar by using the cd command to navigate. This will run it in the terminal.

Redempt
  • 169
  • 1
  • 1
  • 9