Tuesday, July 28, 2009

This program won't give an output! Why??

import javax.swing.JOptionPane;


import java.io.*;


public class shipWindows


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


{


char shipcode;


JOptionPane.showInputDialog ("Enter a shipcode: ");


shipcode = (char) System.in.read ();


switch (shipcode)


{


case 'B' : case 'b': JOptionPane.showMessageDialog (null, "Battleship", "Code of Ship",JOptionPane.INFORMATION_MESSAGE);b...


case 'C' : case 'c': JOptionPane.showMessageDialog (null, "Cruiser", "Code of Ship", JOptionPane. INFORMATION_MESSAGE);break;


case 'D' : case 'd': JOptionPane.showMessageDialog (null, "Destroyer", "Code of Ship", JOptionPane. INFORMATION_MESSAGE);break;


case 'G' : case 'g': JOptionPane.showMessageDialog (null, "Gunboat", "Code of Ship", JOptionPane. INFORMATION_MESSAGE);break;


default: JOptionPane.showMessageDialog (null, "You entered an invalid code", "Code of Ship", JOptionPane. ERROR_MES

This program won't give an output! Why??
Says error down the bottom that could b y
Reply:I don't even think that code would complile. But that might just be from posting it on yahoo. On here it is missing an ending parenthesis, semicolon, and two ending brackets at the end.





You shouldn't use case statements with two condition per line. I've never even tried it on Java and would assume it wouldn't compile but I will have to check when I get home.





so instead of


case 'B': case 'b':


code;


break;





It should be


case 'B':


code;


break;


case 'b':


code;


break;





If you want to save yourself the extra typing though, convert the character to upper case before you do the switch statements. Then you only need the case statement with the upper case character in it.





shipcode = (char) System.in.read ();


shipcode = Character.toUpperCase(shipcode );





That should turn small letters into uppercase and shouldn't change uppercase characters at all.


No comments:

Post a Comment