Computer Applications
Write a program by using scanner class to accept a set of positive and negative numbers randomly. Print all the negative numbers first and then all the positive numbers without changing the order of the numbers.
Answer
import java.util.Scanner;
public class KboatNumbers
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type DONE to Stop Entering Numbers");
System.out.println("Enter Positive & Negative Numbers Randomly:");
String posStr = "", negStr = "";
while(in.hasNextInt()) {
int i = in.nextInt();
if (i < 0)
negStr += i + " ";
else
posStr += i + " ";
}
System.out.println(negStr + posStr);
}
}Variable Description Table
Program Explanation
Output
Related Questions
Write a program by using scanner class to input a sentence. Display the longest word along with the number of characters in it.
Sample Input:
We are learning scanner class in JavaSample Output:
The longest word: learning
Number of characters: 8Write a program by using scanner class to input a sentence. Print each word of the sentence along with the sum of the ASCII codes of its characters.
Write a program to generate random numbers between 1 to N by using scanner class taking N from the console.
Consider a String:
THE COLD WATER AND THE HOT WATER GLASSES ARE KEPT ON THE TABLEWrite a program by using scanner class to enter the string and display the new string after removing repeating token 'THE'. The new string is:
COLD WATER AND HOT WATER GLASSES ARE KEPT ON TABLE