Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Issues with ascending number program

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Issues with ascending number program

    I am new to java as the name states. I am attempting a program where you need to input three numbers from a file and list them in ascending order. I need to use if and if else statements only, no arrays. Not sure where I went wrong on it but any help would be great.

    import java.io.*;
    import java.util.*;
     
    public class AscendingOrder
    {
     
    	public static void main(String[]args) throws FileNotFoundException
    	{
    		double num1, num2, num3;
     
    		Scanner inFile = new Scanner(new FileReader("c:\\numbers.text"));
     
    		num1 = inFile.nextDouble();
    		num2 = inFile.nextDouble();
    		num3 = inFile.nextDouble();
     
    		if ((num1 < num2) && (num1 < num3))
    		{
    			 System.out.print("The numbers in ascending order are: " + num1);
    		else if (num2 < num3)
    			  	 System.out.println(" " + num2 + " " + num3);
    		else
    			 System.out.println(" " + num3 + " " + num2);
    		}
    		if ((num2 < num1) && (num2 < num3))
    		{
    				  System.out.print("The numbers in ascending order are: " + num2);
    		else if (num1 < num3)
    			     System.out.println(" " + num1 + " " num3);
    		else
    			System.out.println(" " + num3 + " " num1);
    		}
    		if ((num3 < num1) && (num3 < num2))
    		{
    			System.out.print("The numbers in ascending order are: " + num3);
    		else if (num1 < num2)
    			     System.out.println(" " + num1 + " " + num2);
    		else
    		    System.out.println(" " + num2 + " " + num1);
    		}
    		inFile.close();
    	}
    }
    Last edited by newtojava2011; June 29th, 2011 at 12:05 PM.


  2. #2
    Junior Member
    Join Date
    Jun 2011
    Location
    Don't worry about it.
    Posts
    14
    My Mood
    Relaxed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    For future reference, please use [code] tags for code. It is much easier to read.

    Your braces for your if statements don't match up. it should look more like this:
    if (condition)
    {
    //code
    }
     
    else if (condition)
    {
    //code
    }
     
    else
    {
    //code
    }

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Thanks, and will do next time

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Still not compiling for some reason telling me exception in thread "main"

    import java.io.*;
    import java.util.*;
     
    public class AscendingOrder
    {
     
    	public static void main(String[]args) throws FileNotFoundException
    	{
    		double num1, num2, num3;
     
    		Scanner inFile = new Scanner(new FileReader("c:\\numbers.text"));
     
    		num1 = inFile.nextDouble();
    		num2 = inFile.nextDouble();
    		num3 = inFile.nextDouble();
     
    		if ((num1 < num2) && (num1 < num3))
    		{
    			 System.out.print("The numbers in ascending order are: " + num1);
    		}
     
    		else if (num2 < num3)
    		{
    				 System.out.println(" " + num2 + " " + num3);
    		}
     
    		else
    		{
    			 System.out.println(" " + num3 + " " + num2);
    		}
    		if ((num2 < num1) && (num2 < num3))
    		{
    				  System.out.print("The numbers in ascending order are: " + num2);
    		}
     
    		else if (num1 < num3)
    		{
    			     System.out.println(" " + num1 + " " num3);
    		}
     
    		else
    		{
    			System.out.println(" " + num3 + " " num1);
    		}
    		if ((num3 < num1) && (num3 < num2))
    		{
    			 System.out.print("The numbers in ascending order are: " + num3);
    		}
     
    		else if (num1 < num2)
    		{
    				 System.out.println(" " + num1 + " " + num2);
    		}
     
    		else
    		{
    		    System.out.println(" " + num2 + " " + num1);
    		}
    		inFile.close();
    	}
    }

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Any help would be greatly appreciated

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Issues with ascending number program

    Still not compiling for some reason telling me exception in thread "main"
    That sounds like an execution error not a compile error.
    Please copy and paste here the full text of the error message. The message says what is wrong and where.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Exception in thread "main" java.lang.NoClassDefFoundError: AscendingOrder
    Press any key to continue . . .

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Usage: java [-options] class [args...]
    (to execute a class)
    or java [-options] -jar jarfile [args...]
    (to execute a jar file)

    where options include:
    -client to select the "client" VM
    -server to select the "server" VM
    -hotspot is a synonym for the "client" VM [deprecated]
    The default VM is client.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    A ; separated list of directories, JAR archives,
    and ZIP archives to search for class files.
    -D<name>=<value>
    set a system property
    -verbose[:class|gc|jni]
    enable verbose output
    -version print product version and exit
    -version:<value>
    require the specified version to run
    -showversion print product version and continue
    -jre-restrict-search | -jre-no-restrict-search
    include/exclude user private JREs in the version search
    -? -help print this help message
    -X print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
    enable assertions
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
    disable assertions
    -esa | -enablesystemassertions
    enable system assertions
    -dsa | -disablesystemassertions
    disable system assertions
    -agentlib:<libname>[=<options>]
    load native agent library <libname>, e.g. -agentlib:hprof
    see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
    load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
    load Java programming language agent, see java.lang.instrument

    Press any key to continue . . .

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    That help at all?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Issues with ascending number program

    java.lang.NoClassDefFoundError: AscendingOrder
    That says that the java program did NOT find the class: AscendingOrder

    Where is the AscendingOrder.class file?
    How are you trying to execute your program?
    Are you entering the java command on a command prompt window?

  11. #11
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Not sure if this is what your asking for but im using TextPad. Saved as .java but hasn't compiled so it wont save as .class file yet.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Issues with ascending number program

    Not sure where I went wrong on it
    What does this mean?

    Are you getting errors from the javac/compiler when it compiles your program?
    Or are you having problems executing the javac command? The OS can't find it.

  13. #13
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    The compiler

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Issues with ascending number program

    The compiler
    Yes that is an issue if you don't have one. Do you have one that works for you?

  15. The Following User Says Thank You to Norm For This Useful Post:

    newtojava2011 (June 29th, 2011)

  16. #15
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Not sure if I'm understanding you correctly but yes my compiler works fine, this program is just not compiling. Sorry if I seem like an idiot I am new to programming and do not completely understand everything as of yet.

  17. #16
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Thanks for the help by the way

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Issues with ascending number program

    this program is just not compiling
    Then I would expect the compiler to be outputting error messages.
    Copy and paste them here if you want help fixing them or understanding them.

  19. #18
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Usage: java [-options] class [args...]
    (to execute a class)
    or java [-options] -jar jarfile [args...]
    (to execute a jar file)

    where options include:
    -client to select the "client" VM
    -server to select the "server" VM
    -hotspot is a synonym for the "client" VM [deprecated]
    The default VM is client.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    A ; separated list of directories, JAR archives,
    and ZIP archives to search for class files.
    -D<name>=<value>
    set a system property
    -verbose[:class|gc|jni]
    enable verbose output
    -version print product version and exit
    -version:<value>
    require the specified version to run
    -showversion print product version and continue
    -jre-restrict-search | -jre-no-restrict-search
    include/exclude user private JREs in the version search
    -? -help print this help message
    -X print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
    enable assertions
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
    disable assertions
    -esa | -enablesystemassertions
    enable system assertions
    -dsa | -disablesystemassertions
    disable system assertions
    -agentlib:<libname>[=<options>]
    load native agent library <libname>, e.g. -agentlib:hprof
    see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
    load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
    load Java programming language agent, see java.lang.instrument

    Press any key to continue . . .

  20. #19
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Issues with ascending number program

    Not sure if this helps but when I try and complile it says under tool output "tool completed with exit code 1"

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Issues with ascending number program

    What are you trying to do with the java command?
    Go to this site and read about the java and the javac commands:
    Java SE 6 Documentation

  22. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Issues with ascending number program

    it says under tool output
    "it"?
    Sounds like you need to study how to use your IDE.

  23. #22
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Issues with ascending number program

    Quote Originally Posted by Norm View Post
    "it"?
    Sounds like you need to study how to use your IDE.
    From the error output, I suspect it's being run from the command line.

    @OP - please post the full text of the command you are using to compile the program, and where the source file is situated.

Similar Threads

  1. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  2. Prime Number Program for class
    By chachunga in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 22nd, 2011, 12:05 AM
  3. Replies: 4
    Last Post: November 14th, 2010, 10:44 AM
  4. my program. Random number generater.
    By james137 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2009, 01:58 PM
  5. Help With Odd/Even number program
    By JonoScho in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 23rd, 2009, 09:53 AM