Repetition - Print Number Of Occurrences
Apr 11, 2015
find whether it is a repetition free number or not. If it is, print the same number(x), else if it has repetitions then print the number of occurrences of the first repetitive digit you encounter.
View Replies
ADVERTISEMENT
Oct 16, 2014
the number of occurrences of a specified character in a string...i tried to do the program occurrences in a given string and i tried the code as below.
code:
import java.util.*;
public class Occurrence
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
Apr 6, 2015
I am trying to count the number of occurrences of a string in an array list. I used the following code:
int count = Collections.frequency(strings, search);
strings is the name of the array list and search is the string that I am trying to count the number of occurrences of. My program compiles correctly, but when I enter the string to search for, I get the following error: "Exception in thread "main" java.lang.NullPointerException at java.util.Collections.frquency(Collections.java:37 18)
Why am I getting this error message and how do I fix it?
View Replies
View Related
Oct 29, 2014
I have problem with sorting my numbers from smallest to biggest and i need to not have repeating numbers, I am stuck. I tried using Arrays.sorts(lottery) but didn't work. and i don't know to but make the numbers not repeat in the same line.
package lottonumbers;
public class LottoNumbers {
public static void main(String[] args) {
int[] lottery = new int[6];
for (int numoftimes=0;numoftimes<5;numoftimes++){
[Code] ....
View Replies
View Related
Oct 3, 2014
I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...
This is the question:
Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:
printStrings("abc", 5);
will print the following output: abcabcabcabcabc
This is what I attempted:
public class printStringsproject {
public static void printStrings(int abc, int number) {
for (int i = 1; i <= number; i++) {
System.out.print("abc");
}
}
public static void main(String[] args) {
printStrings(1, 5);
}
}
View Replies
View Related
Oct 26, 2014
My homework assignment is: Using a do-while statement, write a Java program that continuously requests a grade to be entered. If the grade is less than 0 or greater than 100, your program should display an appropriate message informing the user that an invalid grade has been entered; a valid grade should be added to a total. When a grade of 999 is entered, the program should exit the repetition loop and compute and display the average of the valid grades entered. Run the program on your computer and verify the program using appropriate test data.
When I run it, I don't get the correct average. I know that i'm supposed to enter 999 to exit the loop and to display the average, but 999 is being added with the loop. I'm not sure how to make it not do that.
//stephanie
import java.util.Scanner;
public class gradeAverage
{
public static void main(String[] args)
[code]....
View Replies
View Related
Oct 14, 2014
I can't print the number that is supposed to come out
public class Skateboard {
private int wheels = 4;
private int trucks = 2;
private int bearings = 8;
private int gripTape = 1;
[code]....
How come sb1.assembly() doesn't return the values? :/
View Replies
View Related
Apr 1, 2014
Thought process : Sort the array and print the last 2 element of any given array.
Note /| Should not use any inbuilt Array.sort()
Java Code:
//Write a code to print the 2 largest numbers from the given Array {2,8,10,5,9}
package arrays;
public class biggest2numbers {
public static void main(String[] args) {
int[] A = {2,8,10,5,9};
//Declaring 2 variable
int Max1,
Max2;
[Code] .....
View Replies
View Related
Mar 27, 2014
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;
[Code] .....
View Replies
View Related
Nov 2, 2014
My assignment is to write some code that will multiply every number in an array by 2 and print it out. This is using a site zyante which is a interactive online book kind of thing.
I have tried For (i=0; I < 8; i++) with like userVals = userVals * 2) }
And it doesn't like that so i'm guessing i am no where close to right. The chapter doesn't give me any example of doing anything close to this so i am completely lost on what i have to do.
This is the program :
import java.util.Scanner;
public class NegativeToZero {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
final int NUM_ELEMENTS = 8; // Number of elements
[Code] .....
View Replies
View Related
Oct 26, 2011
I'm trying to write a method that will print a string of a random number of X's (from 5 to 20) on a line, then another number of X's on the next line, etc. until the random number = 16.
I have to use while loops and probably fencepost algorithms. I'm confused with how to print the number of x's the random number of times over and over.
Right now, I'm pretty sure my for loop is wrong or unnecessary.
So far, this is my code:
public static void randomX() {
int x = 0;
while (x != 16) {
x = (int)(Math.random()*20) + 5;
for (int i = 0; i <= x; i++);
{
System.out.print("x");
}
}
}
View Replies
View Related
Oct 1, 2014
Description of the program: read a sequence of positive integers from terminal. When user types
-1, the program will print out the largest number and exit.
Program mid.java
import java.util.Scanner;
public class test
{
public static int num;
public static void main (String[] args)
{
Scanner in = new Scanner(System.in);
num = in.nextInt();
int large = num;
while(num > 0);
{
if(0 > num && num > large)
large = num;
}
System.out.println("The largest number is : " + large);
}
}
View Replies
View Related
Dec 9, 2014
In a text file, I have individual titles (starting with ">" character), and under those titles, I have calculated values for a Counter. I want to search the entire text file for the highest counter under every title. After I get the highest counter, I would like to print it's title, name of the counter, and counter value. Example of the code I already have:
int letterCounter;
String line;
if (line.startsWith (">")) {
bw.write ("");
bw.write (line);
} else {
//code that counts how many total letters (letterCounter) under title
}
I am just trying to use basic concepts.
Sample of my text file:
>gi 91206428 ref NP_001035160.1 seven transmembrane helix receptor precursor [Homo sapiens]
letterCounter is 43
>gi 157412240 ref NP_001094800.1 C1GALT1-specific chaperone 1-like [Homo sapiens]
letterCounter is 7
>gi 117938299 ref NP_001071105.1 RAB6C-like [Homo sapiens]
letterCounter is 109
Sample of desired output:
>gi 117938299 ref NP_001071105.1 RAB6C-like [Homo sapiens]
letterCounter is 109
View Replies
View Related
Apr 21, 2015
I have to write a program that inputs a 5 digit integer from the keyboard and prints if the input number is a palindrome or not. I got it to work, but when my result prints it says "0 is a palindrome" or "0 is not a palindrome". How can I get rid of the 0 and replace it with the number input by the user?
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
int number;
int digit;
int temp;
[code]....
View Replies
View Related
May 13, 2014
Write a method that accepts an array of Strings and a character c. The method must return the count of all cs in the strings of the array.
Here's what I have written:
[
public int countC(String[] strings, char c) {
int count = 0;
for(int i = 0; i < strings.length; i++) {
for(int j = 0; j < strings[i].length(); j++) {
if(strings[i].charAt(j) == c) {
count++;
}
}
}
return count;
}]
Does this look correct?
View Replies
View Related
Nov 11, 2014
I'm trying to print out the results of a program that calculates the number of seats the parties will get in an election.I have to print the partial results and the national results.
I can print te number of seats per party in each constituency, but how can i sum all seats per party in each constituency and print the national results?I'm working with vectors, which I know it might not be the best option, but everything is working, except the fact that I can't loop throuhg the vector and retrieve the total sum per party.Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.Is it possible, or doIhave to create a method in Parties class to solve the problem?
This is what I have now.
for (Parties p : h.geral) {
show += String.format("Constituency - %5s - %5s - %d%n",
p.getConstituency(), p.getParty(), p.getNum_seats());
}
View Replies
View Related
Dec 19, 2014
I have to write a program that will read a picture and then print out the number of blocks inside it.
I have to read the picture as a binary matrix of the size r - c (number of rows times number of columns). The blocks are groups of one or more adjacent elements with the value 1.
- Blocks are built exclusively of elements with value 1
- Each element with value 1 is a part of some block
- Adjacent elements with value 1 belong to the same molecule.
We only take into account the horizontal and vertical adjacency but not diagonal.
INPUT:
In the first line of the input we have the integers r and c, separated with one space.
Then we have the r lines, where each contains s 0's and 1's.
The numbers inside the individual lines are NOT separated by spaces.
The OUTPUT only print the number of blocks in the picture.
Example:
INPUT:
7 5
01000
00010
00000
10000
01000
00001
00100
OUTPUT:
6
THIS IS WHAT I CAME UP SO FAR:
import java.util.Scanner;
class Blocks{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char ch[][];
int rowNum=sc.nextInt();
int columnNum=sc.nextInt();
[Code] ....
View Replies
View Related
Dec 4, 2014
/*
Purpose: To write the methods and the rest program. The program should fill a 4 X 4 2 dimensional array with random numbers between 100 and 200. The program should then determine and print the largest and smallest values within the array using two Methods Largest and Smallest. The program should then determine and print the number of values within the array that are even using a function called Even. The program should also enter a loop that will only terminate when the user inputs a -1 as a choice. The loop should continue to ask the user to guess a number that was randomly generated. The program should call the Findit function to determine if the number was in the array. The program should print out the values in the array when the user selects a -1 and then terminate.
*/
import java.util.Scanner;
import java.util.Random;
public class LNFI_2DArray
{
public static void main(String[] args) {
int guess;
int[] array = new int[4];
[Code] ....
I just had this code working, then all of a sudden i was hit with a 'keyboard leak' error code.
View Replies
View Related
Apr 30, 2014
Write a program that removes all the occurrences of a specified string from a text file. For example, invoking java Exercise14_11 John filename removes the string John from the specified file. Your program should get the arguments from the command line.
I have a problem.
Text document is Match.txt
Match.txt contains a string: "Open the door John"
I tried to create a new file named doc.txt where to put the text from the Match.txt without John word
//to read from document
Scanner output = new Scanner("Match.text");
//to write to document
PrintWriter write = new PrintWriter("doc.txt");
//copy from original document
String copy = "";
while(output.hasNextLine())
[code]...
Error :NoSuchElementException
View Replies
View Related
Oct 20, 2014
I have a simple Word Program here called PuzzleGame.java. It works perfectly but I wanted to add some bells and whistles to it. So I want to be able that if the user selects the same letter more than once a message comes up letting the user know that "That letter has already been selected, try another letter."
I am certain that if would go in the 'else' of the 'if( )' that I am providing.
import java.util.Scanner;
public class PuzzleGame {
public static void main(String[] args){
String secretPhrase = "BIG JAVA";
String guesses = " ";
[Code] .....
I am thinking maybe after the else in this if statement.
if(guesses.indexOf(secretLetter)== -1) {
System.out.print("-");
notDone = true;
} else {
//something here. I am not sure how to go about it.
System.out.print(secretLetter);
}
View Replies
View Related
Nov 21, 2014
I wrote this code which print a black image depends on the number of rows and columns you give it
public class BlackImg {
private Mycolor[][] colorArr; //Mycolor is a class i wrote that represents colors.
// no need for showing the class here.
// so i created here an array of type Mycolor, for example:
// { {(255,255,255), {(127,127,0)} }
[Code] .....
my problem is that my output comes good except the last line ,
Output:
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
(0,0,0) (0,0,0) (0,0,0) (0,0,0)
BlackImg@1db9742 //what is this line , why does it showing up ?
View Replies
View Related
May 3, 2014
Here is what I'm trying to do using arrays.
gather sales amount for the week
display the total of sales
display the average of sale
display the highest sale amount
display the lowest sale amount
using occurrences determine how many times the amount occurred for the week
make a sales main
make a sales data
This is what i have so far but I'm confused on how to start the occurrence and where it would be placed in order to get the information from the array
public class SalesData {
private double[] sales; // The sales data
/**
The constructor copies the elements in an array to the sales array.
@param s The array to copy.
*/
public SalesData(double[] s) {
[Code] .....
View Replies
View Related
Apr 26, 2014
I have this program to see the occurrences of a character in a string stored in a text file. My text file has the following contents (as shown below):
P-P-N-N-L-V
N-R-U-S-R-Q
K-Q-E-L-E-A
A-J-B-G-E-F
F-E-L-Q-R-R
U-F-J-V-I-M
P-I-Q-K-B-K
U-N-L-F-O-B
M-A-N-M-H-O
P-Q-J-D-N-I
G-U-O-D-F-I
Q-M-M-B-C-Q
N-B-I-L-E-J
S-A-T-Q-H-U
C-L-G-U-J-M
R-P-V-M-A-A
C-R-A-V-L-B
M-U-Q-K-M-Q
E-I-C-H-V-J
J-N-N-K-R-P
As you notice, each character is seperated by a hyphen (serving as its delimiter). I want to only go through 10 lines in the text, instead of all 20 of them. So, I wrote the program in such a way that I intend to reach into the indices of each character in the text file. Since there are six characters in each line so that means there are 6 indices - and there are 10 lines I only want to go through. So, 6 indices times 10 lines equals 60 indices. Hence, there are only 60 indices I need to go through. In that manner, it's like I have gone through only 10 lines through that way.
It compiled perfectly fine but upon running it, I ran through an error in the black DOS screen that says
"java.lang.ArrayIndexOutofBoundsException: 6 ".
How do I work around that?
The code I wrote is shown below...
import java.io.*;
import java.util.*;
public class hotandcoldclusterdeterminer_test {
public static void main (String args [])throws IOException {
[Code]....
View Replies
View Related
Jan 12, 2014
Question - Given an specific integer and an array of integers, find and remove all occurrences of the given integer and return an amended array. I solved it. Here is my solution -
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 8, 8, 8, 7, 7, 9};
int input = 8;
int newLen = nums.length;
for(int i=0; i<newLen; i++){
if(nums[i] == input){
[Code] ....
View Replies
View Related
Apr 26, 2014
I have this program to see the occurrences of a character in a string stored in a text file. My text file has the following contents (as shown below):
Quote
P-P-N-N-L-V
N-R-U-S-R-Q
K-Q-E-L-E-A
A-J-B-G-E-F
F-E-L-Q-R-R
U-F-J-V-I-M
P-I-Q-K-B-K
U-N-L-F-O-B
M-A-N-M-H-O
P-Q-J-D-N-I
G-U-O-D-F-I
Q-M-M-B-C-Q
N-B-I-L-E-J
S-A-T-Q-H-U
C-L-G-U-J-M
R-P-V-M-A-A
C-R-A-V-L-B
M-U-Q-K-M-Q
E-I-C-H-V-J
J-N-N-K-R-P
View Replies
View Related
Sep 10, 2014
Write a program that accepts the line number from the user and prints only that particular line from the Floyd triangle.
Example:
Input: 2
Output: 2 3
Input: 3
Output: 4 5 6
View Replies
View Related