Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

Missing return statement

Thread view: 
Yuriy_Ivanov - 07 Jan 2007 15:30 GMT
package trianglecalculation;

import java.text.*;
import java.io.*;
import java.util.*;
/* This class uses ArrayList of triangular objects to
   calculate their total area and perimeter.  
*/  
public class Main
{

   /* this method is used to read in the object sides,
   it needs additional validation  */
   public static double input (BufferedReader r, String prompt) throws
IOException
   {
       double side=0.0;
   
       // handle the exceptions
       try {
           System.out.print( "Please enter " + prompt);
           String myString = r.readLine();
           side = Double.parseDouble(myString);
           }

       catch (NumberFormatException nfe)
               {
                   System.out.println("Please enter a number!\n" + nfe);
               }
       return side;
   }       // end of input()

public static double main(String[] args) throws IOException  // Array list
will be used instead of array?!
   {
       ArrayList aList = new ArrayList();    // Created with default size
(10).
       Triangle ob = new Triangle(); // will be used as work object

       int choice = 0;     // for the user choice
       double side1, side2, side3;
       boolean exceptThrown = false; // if exception is thrown
       InputStreamReader isr = new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);

       do  {
               try {
                       System.out.print(" Please choose a triangular
object to add: ");
                       System.out.println( " 1: General; 2: Equilateral;
3: Right; 0: None ");
                       String myString = br.readLine();   //reads a line
(string) from the buffer br
                       //Parses the string argument
                       choice = Integer.parseInt(myString);
                   }

               catch (NumberFormatException nfe)
                   {
                       exceptThrown = true;
                       System.out.println( "Please enter a number!\n" +
nfe);
                   }

               switch (choice)
               {
                 case 1: side1 = input(br, "side1: " );
                     side2 = input (br, "side2: " );
                     side3 = input (br, "side3: " );      
                     Triangle tri = new Triangle(side1, side2, side3);
                     aList.add((Object) tri); //add to the list
                     break;

                 case 2: side1 = input(br, "side: ");
                       EquilateralTriangle equi = new
EquilateralTriangle(side1);
                       aList.add((Object) equi); //add to the list
                       break;
               
                 case 3: side1 = input(br, "side1: ");
                     side2 = input (br, "side2: ");      
                     RightTriangle rig = new RightTriangle(side1,
side2);
                     aList.add((Object) rig); //add to the list
                     break;

                 case 0: break;
                   default: System.out.println( "\n Wrong choice!" );
                   exceptThrown = true;  
               }
   
           } while (choice != 0 && !exceptThrown);  //repeat the input

       if (!exceptThrown)   // will skip, if an exception was thrown
           {
               double totalA, totalP; // for the total area and
perimeter
               System.out.println("\nObjects \t\t\t area \t\t
perimeter:");
               // To accumulate the total area and perimeter
               totalA = totalP =0.0;
               /* In this loop each object from the array list will be
extracted and its name, area, and perimeter printed out. */

               for(int i = 0; i < aList.size(); i++)
                   {
                       ob = (Triangle)aList.get(i); //casting to
Triangle
                       System.out.print (ob.displayName());
                       System.out.print ( "\t--> " + ob.area());
                       System.out.println ( " \t: " + ob.perimeter());
                       totalA +=  ob.area();
                       totalP +=  ob.perimeter();
                   }

               /*  if you would like to specify the number of fraction
digits,
                 use the NumberFormat abstract class.      */  
                 NumberFormat nf;  // this is not an object
                //Returns a general-purpose number format for the current
default locale      
                 nf = NumberFormat.getNumberInstance();
                 nf.setMaximumFractionDigits(3); // to specify the number
of fraction digits

                String areaTotal = nf.format(totalA); // convert to
String
                String perimTotal = nf.format(totalP);
                System.out.print("\nTotal area and perimeter are:");
                System.out.println("\t\t" +areaTotal+ " :  
"+perimTotal);

               
           } // end if

       
    } // end main - here it says there is a missing return statement
} // end class

I don't need there to be an else, nor do I need an "=" within the if, so
any ideas what I can do to fix this?
Lars Enderin - 07 Jan 2007 15:41 GMT
Yuriy_Ivanov skrev:
> package trianglecalculation;
>
[quoted text clipped - 137 lines]
> I don't need there to be an else, nor do I need an "=" within the if, so
> any ideas what I can do to fix this?

The main method is declared to return a double, that's why it needs a
return statement. Declare it void instead.
Yuriy_Ivanov - 07 Jan 2007 17:07 GMT
>The main method is declared to return a double, >that's why it needs a
>return statement. Declare it void instead.

Thanks, it's compiling properly now.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.