Printing Distinct Numbers

Feb 8, 2015

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

[code]...

View Replies


ADVERTISEMENT

User To Enter 10 Numbers And At The End Prints Out Distinct And Non-repeated Numbers

Nov 23, 2014

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];

[Code] ....

View Replies View Related

How To Hold And Print Distinct Numbers

Sep 29, 2014

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

View Replies View Related

Alphabets And Numbers - Printing Out In A Row

Dec 1, 2014

The code below gives me an alphabet and the numbers 0-9 printed out in column, the job is to print it in line (in a row).
 
/**
* Alphabet and numbers
*/
public class Alphabet {
public static void main() {
char letter;
char number;

[Code] ....

View Replies View Related

Printing Number Of Prime Numbers In A Range?

Feb 13, 2015

The assignment is to make a program that prints the number of prime numbers in a range. This is what i have so far. The output is a list of 2s. I created the for loop to cycle through the range of 17-53 and nested a while loop within to test each incident of the for loop to check for divisors starting with 2 until the modulus result is 0 resulting in a false for being a prime number. Then the loop should increment to the next i value. The last part is an if statement that i had intended to add counters to the k variable that would keep track of the number of prime numbers.

boolean isPrime = true;
int j = 2;
int k = 1;
for (int i = 17; i <= 53; i++){
{
while (i % j == 0){
isPrime = false;

[Code] .....

View Replies View Related

Printing Highest And Lowest Of Three Numbers Using Java?

Feb 23, 2014

I am working on an assignment but I am not getting any out put and I couldn't fix it?The class HighLow below asks for three integers and prints the highest and lowest of them on screen. Your task is to write the missing methods high and low, which receives the integers user inputs as parameters and return the highest and lowest integers respectively.

import java.util.Scanner;
public class HighLow {
public static void main(String[] args) {
int number1, number 2, number 3, high, low;
Scanner reader = new Scanner(System.in);

[code]......

View Replies View Related

Printing Highest And Lowest Of Three Numbers Using Java

Feb 23, 2014

The class HighLow below asks for three integers and prints the highest and lowest of them on screen. Your task is to write the missing methods high and low, which receives the integers user inputs as parameters and return the highest and lowest integers respectively.

import java.util.Scanner;
public class HighLow {
public static void main(String[] args) {
int number1, number 2, number 3, high, low;
Scanner reader = new Scanner(System.in);

[code]....

View Replies View Related

Rational Numbers - Class Not Printing Properly

Nov 10, 2014

I am creating a program for rational numbers but it does not work properly when I enter a negative number in the denominator. It works with every other number.

import java.io.*;
import java.util.Scanner;
public class RationalNumber {
private int numerator;
private int denominator;

[Code] ....

View Replies View Related

Printing Fibonacci Numbers - Illegal Start Of Expression

Nov 1, 2014

Here is my program and results and I am having a error on line 38 which is "public static long fib01(long paramLong)" The program does give me my results but I am trying to correct there error.

public static void main(String[] args) {
{
System.out.print("

" + String.format("%-10s", new Object[] { "Index" }));
System.out.print(String.format("%-15s", new Object[] { "Fibonacci01" }));
System.out.print(String.format("%-15s", new Object[] { "Fibonacci02" }));
System.out.println(String.format("%-15s", new Object[] { "Fibonacci01" }));

[Code] ....

Index Fibonacci01 Fibonacci02 Fibonacci01
--------------------------------------------------
0 0 0 0
1 1 1 1
2 1 1 1
3 2 2 2
4 3 3 3
5 5 5 5
6 8 8 8
7 13 13 13
8 21 21 21
9 34 34 34
10 55 55 55
11 89 89 89
12 144 144 144
13 233 233 233
14 377 377 377
15 610 610 610
16 987 987 987
17 1597 1597 1597
18 2584 2584 2584
19 4181 4181 4181
20 6765 6765 6765
21 10946 10946 10946
22 17711 17711 17711
23 28657 28657 28657
24 46368 46368 46368
25 75025 75025 75025
26 121393 121393 121393
27 196418 196418 196418
28 317811 317811 317811
29 514229 514229 514229
30 832040 832040 832040

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
at fibonacci.Fibonacci.main(Fibonacci.java:38)
--------------------------------------------------Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

View Replies View Related

Determine Number Of Subsets Of K Items Chosen From A Set Of N Distinct Objects

Sep 22, 2014

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);
}

View Replies View Related

Long Type Output File - Print Prime Numbers Between Given Range Of Numbers

Aug 22, 2014

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);

[Code] ....

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.

[ public class Array1
{
public static void main(String[] args)
{
int array [ ] = new int[100];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 26);

[Code] .....

View Replies View Related

Print 2 Lists - 20 Random Numbers And Another List Without Duplicate Numbers

Feb 1, 2015

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];

[Code] ....

View Replies View Related

Generate Two Arrays - One With All Positive Numbers And Another With Negative Numbers

Mar 10, 2015

Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.

public class problem3 {
public static void main(String[]args){
int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5};
for (int i = 0; i<numbers.length;){
if(i>0){
System.out.println(numbers);
}
else
System.out.println(numbers);
}
}
}

View Replies View Related

Generate Random Numbers Without Certain Numbers In Range

Jul 31, 2014

I tried out doing number (generated randomly) != (another number) but that does not work. If I for example want a number between 1 and 10, but I do not want the number 5, what can I do in order to make this happen?

View Replies View Related

List All Prime Numbers Between Two Entered Numbers

Oct 17, 2014

Program is to list all prime numbers between two entered numbers.

import java.util.Scanner;
public class question6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter lower int:");
int x = input.nextInt();

[Code] .....

View Replies View Related

Variable Value Not Printing

Nov 6, 2014

I'm making a program where the user enters an ID number and can submit the amount of sales that person made for a specific product and write to a file. One of the things I have to do is making a running sum of the sales for each product for all people combined as well as an accumulation of the a persons sales every time he enters data. Everything seems to be working execpt my values like person1Total, person2Total...etx and product2Total.

When I try to write them to the file, they always come out at 0.0 even though they should be the sum of the indexes of the array sales.

import java.io.*;
import java.util.Scanner;
public class SalesReport {
// private static double p1Total, p2Total, p3Total, p4Total;
private static double product1Total, product2Total, product3Total, product4Total, product5Total;
private static double person1Total, person2Total, person3Total, person4Total;
public static void main(String[] args) throws IOException

[code]....

View Replies View Related

Printing Out Usernames

Mar 11, 2014

I am trying to print out a list of usernames for my chat program. It currently works except for the username portion. The Server sends a specific line that tells the client to print it as a username. That line is name//name. For example, if the name of the client was Eric, then the server would send Eric//Eric to the client. The client will then receive the line through a buffered reader. However, my current code keeps getting an error at the i=input.substring... line. Its an IndexOutOfBoundsException: String index out of range: -1.

class ChatThread extends Thread {
ChatServer cServer;
String[] sentence;
int i;
public void run() {
String input = "Eric";
try {

[code]....

View Replies View Related

Printing Array To TXT

Apr 15, 2014

So for fun I've decided to write a program that can keep track of a simple card game my friends and I designed. I've built a class for an array, that stores the basic stats of each card. I need to be able to access this array from another class, that will print the array information to a file, so we can easily keep track of who's cards have leveled up and their stats. The main class will also be using a random number generator to determine how much damage the attacker/defender deals and takes respectively. So far I have the random number generator built as well. I'm just having issues creating a print to file class and what I need to change in my array class to make it accessible by the other class that prints it to a file.

public class StartingStats {
public static void main(String[] args)
{
//Variable to use for how many spots in a stat array there are
final int statArray = 2;

[code]...

what I need to change so that a print to file class can access EACH array in this array folder. Also if any cookie cutter printToFile class I could use/borrow/change that'd be really useful, as I've never done any printing to file before. Also, the levels of each stat and exp will be changing so that's why I need a separate class to print so that I can call it when it's been updated so we always have the most up to date stats saved.

View Replies View Related

Array Isn't Printing?

Nov 17, 2014

My array isn't printing. I have tried everything. I have tried putting a nested for loop in different methods but still no luck. I have also tried System.out.print but that prints random characters/numbers.

import java.util.Scanner;
public class Square
{
public static void main(String[] args)

[Code].....

^For some reason the site doesn't show the spaces between the *. It's supposed to be a 5x5 square.

View Replies View Related

Printing A Key And A Value From HashMap

Mar 28, 2014

I have a hashmap of the form HashMap <String, Set<String>>I am trying to create a method with one argument. The argument is a key for the hashmap, if it exists it should print out the key and the associated values. I'm falling over at even getting it to print the key, it keeps printing all the keys from within the hashmap as I don't know how to load the argument into it. I have this so far

Java Code:

public void printValue(String club)
{
boolean result = clubMap.containsKey(club);
if (result)
{
String key = clubMap.keySet(club).toString();
System.out.println(key );

[code]....

View Replies View Related

Printing Same Line Twice?

Sep 24, 2014

I've been working on the below code for a couple days in my spare time. Just a simple little text-based RPG. I'm brand new at programming and Java, so excuse the bad syntax. There's probably easier ways to go about what I'm trying to accomplish, but all we've learned so far in class is while loops and if statements. Anyways, the problem is that the statement "Really? Ten health points[...]yes or no?" in my code is printing twice.

int health = 0;
boolean enterHealth = false;
String verifyHealth = "";
boolean test6 = false;
boolean test7 = false;
boolean test8 = false;

[code]....

View Replies View Related

I/O Printing From Two Methods

May 3, 2015

Lets say in method 1 I want to print the numbers 1 - 10. Easy stuff. I then print that to the

PrintStream outFile = new PrintStream(new File ("output5_01")); I declared.

Now I'm in another methods, How do I print lets say 11- 20 without erasing the first methods printed results?

so my desired results would be

outFile
-------------------------------
(From 1st method) 1- 10
(From 2nd method) 11-20

As of now the second method always over writes the first. None of my books cover this and its very frustrating when the entire code is done but this won't let me write to the file

View Replies View Related

Strings Are Not Printing As They Should?

Jun 12, 2014

Im trying to make a question game, much like a spin off from Trivial Pursuit. In this code, I call classes to get a random number. This number determines what category the question will be from. Coinciding with this number, the "if" statements go and pull the questions and answers from an alternate class. My problem is that when I try and output what should be the question and the 3 answers, its outputs "null" for each String?

This is my first class, which is just the class for the player.

Java Code: /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

[Code].....

View Replies View Related

Printing Out Value Of Each Number

Jan 10, 2015

I finally got this to work but only by printing out the value of each number to debug as such, was there an easier way to do it that I missed?

public class Statistics { 
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// variables used
double num1;
double num2;
double num3;
double num4;
double num5;
double num6;
Scanner input = new Scanner(System.in);
//receive number and add it separately

[Code] ....

View Replies View Related

Printing Pattern When Name Is Given

Mar 10, 2014

I want to know how to print this type of pattern when a name is given ???

Example:

name given TEJA

Capture.PNG

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved