Solved

Basic Java Program: Comparing three floating point numbers and printing the smallest number

Posted on 2008-10-21
12
1,412 Views
Last Modified: 2013-11-23
Hello!

I am still attempting to teach myself JAVA. I trying to write a program that prompts for three floating point numbers that have no more than three decimals and compare them. I am then trying to get it to output the smallest number. It seemed like a simple program and it probably is. I am just so new to java. Can anyone see what I am doing wrong?
/**

 * @(#)minimum3.java

 *

 * minimum3 application

 *

 * @author 

 * @version 1.00 2008/10/21

 */

 import java.util.Scanner; 

 

public class minimum3 {

    

    public static void main(String[] args) {

    	

    Scanner input = new Scanner(System.in );

    

    int number1;

    int number2;

    int number3;

    

    

    System.out.print( "Enter first number: ");

    	number1 = input.nextInt();

    	

    System.out.print( "Enter second number: ");

    	number2 = input.nextInt();

    	

    System.out.print( "Enter third number: ");

    	number3 = input.nextInt();

    

    

    if ( number1 < number2 ) && ( number 1 < number3 )

    		System.out.printf( "Smallest number is: %d\n, number1);

    		

    if ( number2 < number3 ) && ( number 2 < number1 )

    		System.out.printf( "Smallest number is: %d\n, number2);

    		

    if ( number3 < number 2) && ( number 3 < number1 )

    		System.out.printf( "Smallest number is: %d\n, number3);

    
 

}

Open in new window

0
Comment
Question by:Joseph Moody
  • 7
  • 4
12 Comments
 
LVL 2

Expert Comment

by:rnevet
ID: 22767494
Well if you copy pasted it.. then you are missing closing  " in your printf calls to start with.
In bold:
System.out.printf( "Smallest number is: %d\n", number2);

0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22767540
You did not closed the System.out.printf.
Should be -> System.out.printf( "Smallest number is: %d\n", number1);
" after \n

The if condition should be:
if (condition) {
   // your code
}

// in your case

// see the additional braces for the condition

if ( ( number1 < number2 ) && ( number 1 < number3 ) ) {

  System.out.printf( "Smallest number is: %d\n", number1);

}

Open in new window

0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 22767550
Thanks! I corrected that. I also saw I didn't have a closing bracket at the end. I am getting an "illegal start of expression" at line 32. Any clue as to why?
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22767554
You also should capitalize your class name.
minimum3  to Minimum3 (and file name to Minimum3.java)
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22767588
see code snippet. The closing bracket of your main method is missing.
public class minimum3 {

    

    public static void main(String[] args) {

        // your code

    } // this bracket is missing!

}

Open in new window

0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 22767620
Thanks manuel_m! I made the changes but I am getting ')' expected at line 32 (along with a bunch of other errors).
/**

 * @(#)Minimum3.java

 *

 * Minimum3 application

 *

 * @author 

 * @version 1.00 2008/10/21

 */

 import java.util.Scanner; 

 

public class Minimum3 {

    

    public static void main(String[] args) {

    	

    Scanner input = new Scanner(System.in );

    

    int number1;

    int number2;

    int number3;

    

    

    System.out.print( "Enter first number: ");

    	number1 = input.nextInt();

    	

    System.out.print( "Enter second number: ");

    	number2 = input.nextInt();

    	

    System.out.print( "Enter third number: ");

    	number3 = input.nextInt();

    

    

  if ( ( number1 < number2 ) && ( number 1 < number3 ) ) 	

{

  System.out.printf( "Smallest number is: %d\n", number1);

}
 

  if ( ( number2 < number1 ) && ( number 2 < number3 ) ) 

{

  System.out.printf( "Smallest number is: %d\n", number2);

}
 

if ( ( number3 < number2 ) && ( number 3 < number1 ) ) 

{

  System.out.printf( "Smallest number is: %d\n", number3);

}
 

    

    }

}

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:manuel_m
ID: 22767643
You have the three variables
number1
number2
number3
In your if condition you are trying to check number 1 and number 2 with a blank.
Variables, methods, etc. do not have any blanks in their names!
0
 
LVL 22

Author Comment

by:Joseph Moody
ID: 22767685
Ok. That makes sense. How do I fix that though?
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22767715
Just remove the blanks from the variable names in line 32, 37 and 42.

// line 32: replaced "number 1" with "number1"

if ( (number1 < number2) && (number1 < number3) ) {

// etc...

Open in new window

0
 
LVL 6

Accepted Solution

by:
manuel_m earned 500 total points
ID: 22767719
If you still want to check for floating point numbers you have to change the type of your 3 number variables to float.
float number1;
...

And getting the float from the input you have to take the method  input.nextFloat();

You also should think about an exception handling, if the user types not a float number (i.e. abc instead of 1.234).
Here you get more information: http://java.sun.com/docs/books/tutorial/essential/exceptions/
0
 
LVL 22

Author Closing Comment

by:Joseph Moody
ID: 31508293
Thank you so much for all of the help! I did not even see that I had those spaces!
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22767883
You're welcome :-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 84
network + 7 73
strCount chalenge 3 51
what is the difference between "sudo su" and "su - root" 6 76
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now