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.
Code java:
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();
}
}
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:
Code :
if (condition)
{
//code
}
else if (condition)
{
//code
}
else
{
//code
}
Re: Issues with ascending number program
Thanks, and will do next time
Re: Issues with ascending number program
Still not compiling for some reason telling me exception in thread "main"
Code java:
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();
}
}
Re: Issues with ascending number program
Any help would be greatly appreciated
Re: Issues with ascending number program
Quote:
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.
Re: Issues with ascending number program
Exception in thread "main" java.lang.NoClassDefFoundError: AscendingOrder
Press any key to continue . . .
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 . . .
Re: Issues with ascending number program
Re: Issues with ascending number program
Quote:
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?
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.
Re: Issues with ascending number program
Quote:
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.
Re: Issues with ascending number program
Re: Issues with ascending number program
Yes that is an issue if you don't have one. Do you have one that works for you?
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.
Re: Issues with ascending number program
Thanks for the help by the way
Re: Issues with ascending number program
Quote:
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.
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 . . .
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"
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
Re: Issues with ascending number program
Quote:
it says under tool output
"it"?
Sounds like you need to study how to use your IDE.
Re: Issues with ascending number program
Quote:
Originally Posted by
Norm
"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.