I am able to get the integer in the array printed but I am lost on how to hold and print the distinct numbers.
This program will read in 10 numbers from the user via the console. It will display on the console only the distinct numbers, i.e. if a number appears multiple times in the input it is displayed only once.
Here is a sample test run:
Enter an integer: 2
Enter an integer: 6
Enter an integer: 1
Enter an integer: 8
Enter an integer: 6
Enter an integer: 1
Enter an integer: 2
Enter an integer: 4
Enter an integer: 7
Enter an integer: 5
The number of distinct values is 7
2 6 1 8 4 7 5
I have to make a program that prompts the user to enter 10 numbers and at the end it prints out the distinct numbers and then the other numbers that weren't repeated...
I have the part where it prints out the distinct numbers but I stuck on how to make it print out the other numbers that didn't repeat...
import javax.swing.JOptionPane; public class DistinctNumbers { public static void main(String[] args) { String getInput; int input; int[] numbers = new int[10];
so for class I have an assignment that involves printing distinct numbers. the assignment is as follows: Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly on space. Here is what the input and the output are supposed to look like:
Input: Enter Ten Numbers: 1 2 3 2 1 6 3 4 5 2 Output: The Number of distinct numbers is 6 The distinct numbers are: 1 2 3 6 4 5
So far I have the entire code but right now it only prints out the distinct numbers, not how many distinct numbers there are. what that part of the code would look like?Here is my current code:
public class exer75 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); //create scanner system System.out.println(""); System.out.print("Enter Ten Numbers: "); //prompt the user to enter 10 numbers int[] values = new int[10]; //create an array to hold the numbers
I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000
public class primenumber { public static void main(String[] args) { long start = 5000000; long end = 10000000; System.out.println("List of prime numbers between " + start + " and " + end); for (long i = start; i <= end; i++) { if (isPrime(i)) { System.out.println(i);
I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m
y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.
public void randomNum(){ System.out.println("Twenty random integers: "); int max = 20; // max value for range int min = 1; // min value for range Random rand = new Random(); int[] all = new int[20];
Am having trouble understanding how to print my results (I have 50 of them) , only 10 per line.
Im using an array that is 1-50. The first 25 I raise to the power of 2 and print it.
The next 25, I multiple by 3 and print it
Public class KDowling { /** * @param args the command line arguments */ public static void main(String[] args) { double [] alpha = new double[50]; //declare an array 50 indexes int num=1; // declare variable num int counter=1;
I've almost got this to work. Right now it prints out one comma for the first set of 3 numbers. For exmaple, if the user input is 12345678 the code will print out 12345,678. I'm trying to figure out how to get it to print out commas for the rest of the numbers. My first thought was to use a loop but I can't get it to work....
private static void printWithCommas(NaturalNumber n, SimpleWriter out) { assert n != null : "Violation of: n1 is not null"; assert out != null : "Violation of: out is not null"; assert out.isOpen() : "Violation of: out.is_open"; if (n.compareTo(THOUSAND) < 0) { out.print(n);
Write a program to print the even numbers and the odd numbers between 0 and 30 using a single thread and then again using multiple threads.
I already finished the single-threaded program but am having trouble with the multi-threaded one. I have three classes; one class for odd numbers, one for even numbers, and one to execute the code. Here is my code so far:
Even Numbers:
Java Code:
public class EvenNumbers extends Thread { public void run() { for (int i=1; i<=30; i++) { if (i%2 == 0) { System.out.println("Even number " + i);
[Code] ....
Unfortunately, my output ends up looking a little weird:
Java Code:
C:UsersREDACTEDDropboxSchoolworkREDACTEDJava ProgrammingUnit 5 - Exception Handling, AssertionsProgram - Thread>java MultiThread Even Numbers: Odd Numbers: Even number 2
Part of my program requires writing a function that determines the number of subsets of k items that can be chosen from a set of n distinct objects ("n choose k" or n!/((n-k)! n!)). The only errors I am getting are from the first line of the function:
// n choose k: distinct subsets of k items chosen from n items public static int choose (int n, int k) { if (k <= 0) return 1; else return choose(n--, k--) * (n/k); }
package com.arraydemo; import java.util.TimeZone; import java.util.concurrent.TimeUnit; public class ArrayStructures { public long[] theArray; public int arraySize; public ArrayStructures(int size)
I am trying to cast an integer into a double. it works but I need two decimal number after the dot and for example I can print 7.51 but how can I get it to print 7.50 and not 7.5
public int compareTo(Money o){ int d1 = (this.dollars * 100) + this.cents; int d2 = (o.dollars * 100) + o.cents; return d1 - d2; }
On my main method I have
public static void main(String[] args){ Money money1 = new Money(10, 49); Money money2 = new Money(2, 99); System.out.println("result: " + "$" + (double)money1.compareTo(money2)/100); }
It prints 7.5 but I want it to print 7.50, how do I do this .....
class shape{ public static void main(String [] args){ for(int x =0 ; x<=6;x++){ for(int y =0 ; x > y ; y++){ System.out.print(x); } System.out.print(" "); } } }
Where I am doing mistake to print the numbers of Armstromg numbers requested.
import java.util.Scanner; public class ArmStrongNumber { public static void main(String[] args) { // TODO Auto-generated method stub int x, y, z=0, temp, temp1=1;
I created a random number generator for my course and it works perfectly. I now need to print only the top 15 highest numbers of occurrence from the RNG output. How i can do this?
Here is my block of code.
package section4; import java.util.Random; public class Lottery { public static void main(String[] args) { Random rand = new Random(); int freq[] = new int[51];
Prompt the user to enter two ints, firstNum and secondNum. Then print all odd numbers between firstNum and secondNum.
Here's my code-- my error is in this line: for(firstNum;firstNum<=secondNum;firstNum++)
The error is "not a statement."
import java.io.*; import java.util.*; public class ListOddNumbers { public static void main(String[] args) { Scanner keyboard= new Scanner(System.in); System.out.println("Enter your first number."); int firstNum= keyboard.nextInt();
2.Check the Vehicle...Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3, inclusive. The numbers represent the type of vehicle as stated below:
NumberVehicle Category 1 Car 2 Motor bikes 3 Trucks
Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program.
I read on how to use the continue statement, but I'm failing in how to use it properly, mostly because it's not working. I'm supposed to print out what numbers are showing up and how many times for each. Plus, I have to print out 'times' instead of 'time' if there's more than one of a certain number. Right now, it's printing out all the numbers including the ones that don't get inputted.
import java.util.Scanner; public class occurrence { public static void main(String[] args) {
//scanner/values Scanner input = new Scanner(System.in); int number = 101;
The question is Write a program that displays all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6. and separated by exactly a space.
My assignment requirements are to display this in Dialog Box / message box . I have written this code so far
import javax.swing.JOptionPane; public class Exercise04_10 { public static void main(String[] args) { int count = 1; for (int i = 100; i <= 1000; i++) if (i % 5 == 0 && i % 6 == 0)
Write a Java program that asks the user to store 10 numbers in an array. You should then print the array in the order entered and then print in reverse order. However, instead of putting all the information in the main, you should use a class called PrintIt. The PrintIt class should have an instance variable that is an array to hold the numbers. You should have a method that will print the array in the order entered. You will also need a method to print in reverse oder. Then create a tester class that asks the user to enter 10 numbers and puts them in an array.
So far I've got this.
public class printIt { int i; int [] numArr = new int [10]; public printIt () { i = -1;
[Code] .....
Output:
Enter a number:
8 34
Forward:
8 34
Reverse:
34 8
end
what i want as an number is being able to print out 10 numbers but this only lets me do two numbers. Why is that so?
I am trying to do this assignment but I can't get the needed output.
Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.
Program is written to a class called ReverseNumbers.
Example output
How many floating point numbers do you want to type: 5
Type in 1. number: 5,4 Type in 2. number: 6 Type in 3. number: 7,2 Type in 4. number: -5 Type in 5. number: 2
Given numbers in reverse order:
2.0 -5.0 7.2 6.0 5.4
Java Code:
import java.util.Scanner; public class apples { public static void main(String[] args) { Scanner reader = new Scanner(System.in); double[] numbers;
At the end of my main method, I want it to print out the input given that should have been stored in my sandwich class fields, but it didn't.
import java.util.*; import java.lang.*; public class SandwichBuilder { public static void main(String[] args) { Scanner inputDevice = new Scanner(System.in);
I am in the process of creating a Tic Tac Toe game with a GUI using JButtons that will hold the X and O pictures. I have the picture appearing when i click it once but it jumbles up all the other buttons which is why i have to run the method resetAllButton() to set everything back. Once i click the x again it puts everything back how it is supposed to be and works normal. How can i get this to work in one click.
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class TicTacToeGui extends JFrame implements ActionListener {