NEEDSHELP: JAVA having trouble with a program of mine?
// program to allow tracking of stock orders
//enabling JOptionPane
import javax.swing.JOptionPane ;
//beginning of class
public class Assignment3
{
//beginning of method main
public static void main(String args[])
{
//declaring variables
int numberOfShares ,
nOrderCounter ,
aOrderCounter ,
totalNShares ,
totalAShares ;
double averageNSharesPerOrder ,
averageASharesPerOrder ;
String numberOfSharesAsString ;
String exchangeNameAsString ;
//initializing variables
nOrderCounter = 0 ;
aOrderCounter = 0 ;
totalNShares = 0 ;
totalAShares = 0 ;
//ask for number of shares
numberOfSharesAsString = JOptionPane.showInputDialog
(null,
“Enter number of shares you wish to deposit” +
” (enter 0 or less to end)” ,
JOptionPane.QUESTION_MESSAGE) ;
numberOfShares = Integer.parseInt (numberOfSharesAsString) ;
//pull out first letter of exchange name
char c ;
//begin loop
while (numberOfShares > 0)
{
//ask for name of stock exchange again
exchangeNameAsString = JOptionPane.showInputDialog
(null,
“Enter desired exchange destination (NYSE or ASE)” ,
JOptionPane.QUESTION_MESSAGE) ;
c = exchangeNameAsString.charAt(0) ;
if ((c == ‘n’) || (c == ‘N’))
{
nOrderCounter = nOrderCounter + 1 ;
totalNShares = numberOfShares + totalNShares ;
}
else if ((c == ‘a’) || (c == ‘A’))
{
aOrderCounter = aOrderCounter +1 ;
totalAShares = numberOfShares + totalAShares ;
}
else
JOptionPane.showMessageDialog
(null,
“ERROR: not a valid exchange”) ;
//ask for number of shares again
numberOfSharesAsString = JOptionPane.showInputDialog
(null,”Enter number of shares (enter 0 or less to end)” ,
JOptionPane.QUESTION_MESSAGE) ;
numberOfShares = Integer.parseInt (numberOfSharesAsString) ;
}//end of while loop
//calculating the averages
averageNSharesPerOrder = (double) totalNShares / nOrderCounter ;
averageASharesPerOrder = (double) totalAShares / aOrderCounter ;
//print all info
if ((totalNShares > 0) || (totalAShares > 0))
{
JOptionPane.showMessageDialog
(null,
“NYSE:\n”
+ “You have ”
+ nOrderCounter + ” orders ”
+ totalNShares + ” shares ”
+ averageNSharesPerOrder + ” shares per order ”
+ “\nASE:\n”
+ “You have ”
+ aOrderCounter + ” orders ”
+ totalAShares + ” shares ”
+ averageASharesPerOrder + ” shares per order “) ;
}//end of info printing
else
JOptionPane.showMessageDialog
(null, “You did not enter any shares”) ;
//end
System.exit(0) ;
}//end of method main
}//end of class
the program has a 3 in the input box when i run it…does it do the same for you guys? and if so how do u fix it? id appreciate the help
also could someone tell me how to make an error message icon appear for my error messages with JOptionPane?
Answers and Views:
Answer by rascalflattsrule
You’re missing something here. After to end)”, you need to put “Input”
numberOfSharesAsString = JOptionPane.showInputDialog
(null,
“Enter number of shares you wish to deposit” +
” (enter 0 or less to end)” ,
JOptionPane.QUESTION_MESSAGE) ;
So like this instead:
numberOfSharesAsString = JOptionPane.showInputDialog
(null,
“Enter number of shares you wish to deposit” +
” (enter 0 or less to end)”,”Input”,
JOptionPane.QUESTION_MESSAGE);
Leave a Reply