Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: How can I move a user's input into a table and manipulate it with previous methods?

1. How can I move a user's input into a table and manipulate it with previous methods?

Code:
``` //This is the code I have so far.//
import java.util.Scanner;

public class AnnualPayCalculator
{
public static void main(String[ ] args)

{
double salesAmount ;

Scanner keyboard = new Scanner(System.in);

System.out.print(" Enter the total dollar amount of sales for this year.");
salesAmount=keyboard.nextDouble( );

if ( salesAmount >= 320000 ) {
System.out.println( " Your total compensation for this year is " +
( 0.0925 * salesAmount + 30000 ));
}

if ( salesAmount < 320000 ) {
System.out.println( " Your total compensation for this year is " +
( 0.08 * salesAmount + 30000 ));
}
}```
//The above code compiled correctly and is working. This is where I get lost. I need a table with two columns to be created. It must be populated with the user's input for the sales amount in the first column. The program must also add the commission earned for that amount with the annual salary and place it in the second column.//

//I also need the table to be populated with figures created by increasing the sales amount in increments of 5000 until the amount is greater than or equal to 1.5 * times the user sales amount (In the first column). The program must also add the commission total with the annual salary for each amount and display it in the second column.//

//Does anyone have any suggestions about how the user input can be manipulated and moved into a table?//

2. Table is a bit ambiguous here. I assume you mean a tabular style command line output?
That's done by simply using tabs. In Java, you simply provide the \t character to indicate a tab. Since this looks like a school assignment, I can't just provide you the code, but I can certainly provide an example:
PHP Code:
``` System.out.println("firstcolumn\tSecondColumn\tThirdColumn");  ```
The \t is important, but you can separate these into multiple System.out.print instead of println to make it clearer. Make sure you finish with a println (even if you provide it with nothing), or add a \n for the last column at the end.

As for incrementing, that indicates you need to loop to a condition and increase by an amount. A simple for loop can do this for you.

Also, in the future please wrap your code in [php][/php] or [code][/code] tags to preserve the format.

3. Users who have thanked Fou-Lu for this post:

Basic Chaos (10-25-2012)

4. this is like what I need, but the data should be program generated not typed in by me

Code:
```import java.awt.*;
import javax.swing.*;
class TableDemo {
public static void main(String args[])
{

JFrame frame = new JFrame("Commission Sample Table");
String columns[] = {"Total Sales","Total Compensation"};
Object data[][] = {
{"\$400,000","\$62,000"},
{"405,000","\$67,000"},
{"410,000","\$67,925"}
};
JTable table = new JTable(data,columns);
frame.setVisible(true);
frame.setBounds(0,0,600,600);
frame.add(table.getTableHeader(),BorderLayout.PAGE_START);
frame.add(table);
}
}```

5. Okay, using the swing JTable with the gui instead of the command line?
That's no problem to do, but I'd recommend not using Object[][] as your data handling. Instead, use the TableModel of the JTable as it will accept a collection of object.
The TableModel is easiest done with the DefaultTableModel (or you can write your own, but that's a more advanced feature). Its use is simply as such:
PHP Code:
``` JFrame frame = new JFrame("Commission Sample Table");TableModel tmData = new DefaultTableModel();JTable table = new JTable(tmData);((DefaultTableModel)tmData).addColumn("Total Sales");((DefaultTableModel)tmData).addColumn("Total Compensation");                frame.getContentPane().add(new JScrollPane(table));frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setBounds(0, 0, 600, 600);frame.setVisible(true);  ```
Now, TableModel can be given new rows on the fly. This is still beneficial even if not responding to events; adding new records is simply accepting Object[] or vectors. For an example:
PHP Code:
```     public static void insertCalculatedNumbers(DefaultTableModel tm, double dStart)    {        for (double d = dStart; d <= dStart * 1.5; d += 5000)        {            tm.addRow(new Object[]{d, "Calculated Result Here"});        }    }  ```
Given a DefaultTableModel, this will increment dStart by 5000 until it hits 1.5x the size of dStart. It will insert a new row on each iteration. Calculated result is probably best done in another method instead of inline with this code.

6. Users who have thanked Fou-Lu for this post:

Basic Chaos (10-28-2012)

7. Thanks for your help. I think I understand now

8. I unquestionably longed for to rise a acknowledgement to be means to appreciate we for all a glorious contribution we have been display on www.codingforums.com . My enlarged internet poke has right away been famous with great strategies to go over with my friends as well as family. we would demonstrate which most of us visitors essentially have been unquestionably sanctified to exist in a conspicuous village with really most undiluted people with profitable hints. we feel indeed beholden to have detected your web pages as well as demeanour brazen to so most some-more extraordinary mins celebration of a mass here. Thank we again for all a details.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•