I need to round a float to the nearest whole number using
DecimalFormat (specific requirements for a program). I have tried
DecimalFormat grade = new DecimalFormat("0");
but it does nothing at all. What should I put in the parentheses to
just round to the nearest whole number?
OK, I'll show my code I guess...
Try compiling
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
public class Ch7Ex4 extends JFrame implements ActionListener
{
JTextPane textPane = new JTextPane();
int[] grades = new int[0];
float average,gTotal,Average;
public Ch7Ex4()
{
super("Grade Averager");
}
public JMenuBar createMenuBar()
{
//create an instance of the menu
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
//construct and populate the File menu
JMenu mnuFile = new JMenu("File", true);
mnuFile.setMnemonic(KeyEvent.VK_F);
mnuFile.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuFile);
JMenuItem mnuFileExit = new JMenuItem("Exit");
mnuFileExit.setMnemonic(KeyEvent.VK_X);
mnuFileExit.setDisplayedMnemonicIndex(1);
mnuFile.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
//construct and populate the Edit menu
JMenu mnuEdit = new JMenu("Edit", true);
mnuEdit.setMnemonic(KeyEvent.VK_E);
mnuFileExit.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuEdit);
JMenuItem mnuEditInsert = new JMenuItem("Insert New Grades");
mnuEditInsert.setMnemonic(KeyEvent.VK_I);
mnuEditInsert.setDisplayedMnemonicIndex(0);
mnuEdit.add(mnuEditInsert);
mnuEditInsert.setActionCommand("Insert");
mnuEditInsert.addActionListener(this);
return mnuBar;
}
//create the content pane
public Container createContentPane()
{
//create the JTextPane and center panel
JPanel centerPanel = new JPanel();
setTabsAndStyles(textPane);
textPane = addTextToTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(650, 200));
centerPanel.add(scrollPane);
//create Container and set attributes
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(centerPanel, BorderLayout.CENTER);
return c;
}
//method to create tab stops and set font styles
protected void setTabsAndStyles(JTextPane textPane)
{
//create Tab Stops
TabStop[] tabs = new TabStop[3];
tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT,
TabStop.LEAD_NONE);
tabs[1] = new TabStop(295, TabStop.ALIGN_LEFT,
TabStop.LEAD_NONE);
tabs[2] = new TabStop(450, TabStop.ALIGN_LEFT,
TabStop.LEAD_NONE);
TabSet tabset = new TabSet(tabs);
//set Tab Style
StyleContext tabStyle = StyleContext.getDefaultStyleContext();
AttributeSet aset =
tabStyle.addAttribute(SimpleAttributeSet.EMPTY,
StyleConstants.TabSet, tabset);
textPane.setParagraphAttributes(aset, false);
//set Font Style
Style fontStyle =
StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = textPane.addStyle("regular", fontStyle);
StyleConstants.setFontFamily(fontStyle, "SansSerif");
Style s = textPane.addStyle("italic", regular);
StyleConstants.setItalic(s, true);
s = textPane.addStyle("bold", regular);
StyleConstants.setBold(s, true);
s = textPane.addStyle("large", regular);
StyleConstants.setFontSize(s, 16);
}
//method to add new text to the JTextPane
public JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
//clear previous text
doc.remove(0,doc.getLength());
//insert title
doc.insertString(0,"\tGRADES",textPane.getStyle("large"));
//insert detail
for (int j = 0; j<grades.length; j++)
{
doc.insertString(doc.getLength(), "\n" + grades[j],
textPane.getStyle("regular"));
}
if (average>0)
{
doc.insertString(doc.getLength(), "\nThe average of
the grades is " + average,
textPane.getStyle("regular"));
}
}
catch (BadLocationException ble)
{
System.err.println("Couldn't insert text.");
}
return textPane;
}
//event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if (arg == "Insert")
{
gTotal=0;
average=0;
Average=0;
grades=new int[0];
//accept new data
int newGrade=0,x=0;
while (newGrade != -1)
{
String newGrades = JOptionPane.showInputDialog(null,
"Please enter the new grades, and -1 if you are
done entering data");
newGrade = Integer.parseInt(newGrades);
//add new data to arrays
if (newGrade>0)
{
grades = enlargeArray(grades);
grades[grades.length-1] = newGrade;
x=x+1;
gTotal=gTotal+newGrade;
}
else if (x==50)
{
JOptionPane.showMessageDialog(null, "You have
entered the maximum amount of grades",
"Error",JOptionPane.INFORMATION_MESSAGE);
newGrade=-1;
}
}
DecimalFormat grade = new DecimalFormat("0");
average=gTotal/x;
float Average = Float.parseFloat(grade.format(average));
Average=Average;
sort(grades);
}
}
//method to sort arrays
public void sort(int tempArray[])
{
//loop to control number of passes
for (int pass = 1; pass < tempArray.length; pass++)
{
for (int element = 0; element < tempArray.length - 1;
element++)
if (tempArray[element] > tempArray[element + 1])
{
swap(grades, element, element + 1);
}
}
addTextToTextPane();
}
//method to swap two elements of an array
public void swap(int swapArray[], int first, int second)
{
int hold; //temporary holding area for swap
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;
}
//method to enlarge an array by 1
public int[] enlargeArray(int[] currentArray)
{
int[] newArray = new int[currentArray.length+1];
for(int i = 0; i<currentArray.length; i++)
newArray
= currentArray;
return newArray;
}
//main method executes at run time
public static void main(String args[])
{
JFrame.setDefaultLookAndFeelDecorated(true);
Ch7Ex4 f = new Ch7Ex4();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setJMenuBar(f.createMenuBar());
f.setContentPane(f.createContentPane());
f.setSize(725,300);
f.setVisible(true);
}
}