Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA swaping values // Swap.java - This program determines the minimum and maximum of three values input by // the user and performs necessary swaps.

JAVA swaping values

// Swap.java - This program determines the minimum and maximum of three values input by // the user and performs necessary swaps. // Input: Three int values. // Output: The numbers in numerical order.

import java.util.Scanner;

public class Swap { public static void main(String args[]) throws Exception { // Declare variables. String firstNumber; // String version of first number. String secondNumber; // String version of second number. String thirdNumber; // String version of third number. int first = 0; // First number. int second = 0; // Second number. int third = 0; // Third number. int temp; // Used to swap numbers. Scanner input = new Scanner(System.in);

// Get user input. System.out.print("Enter first number: "); if((firstNumber = input.nextLine())) != null) { System.out.print("Enter second number: "); secondNumber = input.nextLine(); System.out.print("Enter third number: "); thirdNumber = input.nextLine(); // Convert Strings to int. first = Integer.parseInt(firstNumber); second = Integer.parseInt(secondNumber); third = Integer.parseInt(thirdNumber); } // Test to see if the first number is greater than the second number. if(firstNumber.compareTo(secondNumber)>0) { String temp = secondNumber; secondNumber = firstNumber; firstNumber = temp; } // Test to see if the second number is greater than the third number. if(secondNumber.compareTo(thirdNumber)>0) { String temp = thirdNumber; thirdNumber = secondNumber; secondNumber = temp; } // Test to see if the first number is greater than the second number again. if(firstNumber.compareTo(secondNumber)>0) { String temp = secondNumber; secondNumber = firstNumber; firstNumber = temp; } // Print values in numerical order. System.out.println("Smallest: " + first); System.out.println("Next largest: " + second); System.out.println("Largest: " + third);

// Exit the program. System.exit(0);

} // End of main() method. } // End of Swap class.

What is wrong with my swap? it is saying illegal start on line 25, which is this line:

if((firstNumber = input.nextLine())) != null)

it also says that this line above needs a ;

but it is an if statement so it does not need one correct?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

Which are non projected Teaching aids in advance learning system?

Answered: 1 week ago