Okay, so this is the program I've written thus far. The biggest issue I'm having is counting the wins and losses from the rpsls method. I know that the line
int output = rpsls(retVal);
isn't valid, but can't figure out a way to call the retVal from rpsls in. I've been at this all day and still can't make it work.
import java.util.Scanner;
public class Rpsls
{
public static void main (String [] args) {
//while loop
Scanner input = new Scanner (System.in);
int x = input.nextInt();
while (x > 0 && x <= 5)
I have to take a user's input and count the number of vowels in a String. If I start with a lowercase vowel it gets counted, but if I start with an uppercase or different letter I get nothing. Either way, I can not get the counter to go higher than 1.
import java.util.Scanner; public class countVowels { public static void main(String[] args) { Scanner kb=new Scanner(System.in); System.out.println("Enter a sequence of letters:"); String letters=kb.next();
How would I program a counter that will return the total number of search, failed searches and correct searches. The second thing is how can a return the price of the search methods with out calling for it specifically. Here is my current code.
package stu.paston.program7; import java.util.Scanner; public class MainClass { public static void main(String[] args) { InventoryClass productData= new InventoryClass();
This code is for a tic tac toe game. I have a action listener set up and if the user entered a move that would win the game of tic tac toe it would call on a restart() method to clear the board, but every time some one wins the GUI freezes.
class ListenerCA implements ActionListener { public void actionPerformed(ActionEvent event) { Board.clear(); if (turn == 0) { start.answer = "CA";
[Code] .....
The code below this is the normal execution flow.
static void start_game(String board[][]){ final int one = 1; final int two = 2; int x = 0; play game = new play();
[Code] .....
In the above code gui == true always, I just kept an option if i ever wanted to run the program in console. The code below is the restart method.
I have written a piece of code that takes a desired input file and calculates things such as words, characters, digits etc. I would like to make the program look better by counting palindromes.what I could add to my current code to count palindromes.My current code for counting other things that I would like to add plaindromes to.
// Loops through the file calculating the outcome. while (input.hasNextLine()) { lines++; String line = input.nextLine(); chars += line.length();
I'm having trouble creating a highly efficient algorithm for counting within a custom scale. This problem applies to futures trading, specifically treasuries contracts.
One specific treasury contract has 32 units before rolling over to the next whole number. So, the price scale looks something like this ...
If I pick a number (price) at random, let's say 1 28, and I want to add 8 units to that value, I should end up with 2 4. I can do this using brute force, calculating remainders, etc, etc....
Write a method compressDuplicates that accepts a stack of integers as a parameter and that replaces each sequence of duplicates with a pair of values: a count of the number of duplicates, followed by the actual duplicated number. For example, suppose a variable called s stores the following sequence of values:
This new stack indicates that the original had 5 occurrences of 2 at the bottom of the stack followed by 2 occurrences of -5 followed by 4 occurrences of 3, and so on. This process works best when there are many duplicates in a row. For example, if the stack instead had stored:
bottom [10, 20, 10, 20, 20, 10] top
Then the resulting stack after the call ends up being longer than the original:
bottom [1, 10, 1, 20, 1, 10, 2, 20, 1, 10] top
If the stack is empty, your method should not change it. You may use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem. For full credit your code must run in O(n) time where n is the number of elements of the original stack.
I wrote a code but still having a problem with it , am I allowed to use 3 while loops ?
public void compressDuplicates(Stack<Integer> s ){ Stack<Integer> backup= new Stack<Integer>(); int count = 1; while(!s.isEmpty()){ int temp = s.pop();
It's supposed to count all of the duplicates in an array and print out how many occurrences of the value starting at whatever index, or if there are no duplicates state that. Basically:
No duplicates with value 1 beyond Index 0
There are 3 more occurrences of value 2 starting at index 1
There are 2 more occurrences of value 2 starting at index 2....
This is what I've got so far:
Java Code:
public static void main(String[] args) { int[] arr = {1, 2, 2, 3, 4, 2, 4, 3, 0, 5, 3, 2}; for(int i = 0; i<arr.length; i++){ int count = 0; for(int j = i+1; j<arr.length; j++){ if((arr[j] == arr[i]) && (i!=j)){ count++; System.out.print("There are " + count + " more occurrences of "); System.out.println(arr[i] + " starting at index " + i); } } } } mh_sh_highlight_all('java');
I was browsing around and I found a question asking to find how many times a word occurred in a sentence. I am using a hashtable, over kill yes but I need to use them more. However my counter is not working, not really sure why.You can see in the main method I have two repeating names but it returns 0.
package frequency; import java.util.Hashtable; public class CheckFrequency { hashtable<String, Word> words = new Hashtable<String, Word>();
I curious for tips for moving forward within my current code for my counting cards GUI interface. I need two labels one for the card deck, and the other for the randomized card face. When the card face shows, in the count value text field the value of the card is added. For each card this is to happen through until the whole deck. There is also a card count text field to count how many cards have been dealt out. Now with that being said, I am being held back by getting the two labels to show in my GUI, the buttons show, but I need getting the cards and its value to show and initialize, with the card count in the card count text field.
import java.lang.Math; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class CardsGui extends JFrame
Complete the body of the following method. Use a CharQueue to store the input line as it is being read. The parameter is an EasyReader from Appendix B of the text. Use the method in.charInput( ) to read and return the next character of the EasyReader, and use in.isEOLN( ) to determine whether the next input character is the end-of-line.
public static int counter(EasyReader in) // Precondition: There is a line of input waiting to be read from in. // Postcondition: A line of input has been read from in, up to but not // including the newline character. The return value of the method // is the number of times that the LAST character of the line appeared // somewhere in this line.
[EXAMPLE Input: ABBXDXXZX - The value returned by counter would be 4 for this input since there are 4 X's in the input line.]
***When I look at this I understand that I'm being asked to finish the method, that I have 1 input, which is an "EasyReader" object called in and that I use a CharQueue object along with the method isEOLN( ) to grab characters from in while looking for an end of line and then sticking the values in the queue and then I can go through the queue and figure out the number of times that last character shows up in the queue. I just am at a loss on the "how".
I have this code but I can't seem to get it to work. It keeps saying that "count" cat be found and that it cannot return a value whose type is void.
Java Code: public class Cuantos { static int getPosition(double listOfValues[], double targetValue ) { int i,count, position = -1; for (i=0; i < listOfValues.length; i++) { if (listOfValues[i] == targetValue)
working on assignment to count the total integers and have a seperate system.out if a non integer.
import java.util.Scanner; public class CountInteger { public static void main(String[] args) { int correctCount = -1 ; // count number of integers int data; int sum = 0;
The method public static int steps(int posts, int stride) calculates how many strides can be taken to get back to posts. Let's say if the method is (12, 4), it takes only three steps. Now let's say the method has parameters (12,5), so it should be (5, 10, 3, 8, 1, 6, 11, 4, 9, 2, 7, 12). My method works for such examplse as (12, 4) or (12,3) or (6,2)... but how can I figure out (12,5)?
Java Code:
public static int steps(int posts, int stride) { int countSteps = 0; int result = 0; do { result += stride;
Max has a row of N boxes (1 <= N <= 1,000). Each box weighs some value W (1 <= W <= 10,000), which will be unique among all boxes. Max would like to sort his boxes from lightest to heaviest, and must decide between Insertion Sort and Selection Sort to do so. Moving a box any distance D requires (W * D) units of work. Max determine which of the two sorting methods will require the least work.
Both of the sort methods work and counting the work in the sequential sort works fine. For some reason that I can't figure out, I am not counting the insertion sort right.
import java.util.Arrays; public class LeastWork { import java.util.Arrays; public class LeastWork { public int leastWork(int[] boxes) { int low; int temp; int sWork = 0;
I am currently trying to roll 5 dice 100,000 times and output the number of times each possible total (of dots/pips) occurs. It seems to output the incorrect thing and isnt counting the occurrences properly.
public class lab_DiceRollTest { public static void main(String[] args) { int total; int total2; String valid; String isDifferent; int rolls = 100000; int d = 0;
I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:
hello world how are you There are 0 words of length 0 There are 0 words of length 1 There are 0 words of length 2 There are 3 words of length 3 There are 0 words of length 4 There are 2 words of length 5
This is my code so far it sort of does the job but not the way i want it too
import java.util.Scanner; import java.util.StringTokenizer; public class Brown_Matthew_13117002{ public static int count(String s, int len){ int result=0; StringTokenizer st=new StringTokenizer(s,"[ ,;]");
[Code] ....
The output would end up being
hello There are 0 words of lenghth 0 world There are 0 words of lenghth 1 how There are 0 words of lenghth 2 are There are 3 words of lenghth 3 you There are 0 words of lenghth 4
I think I need to use string.split instead of stringtokenizer and the while loop is incorrect it needs to loop equal to the number of letters in the longest word. But how to do either of these ?
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.
I have to take user input and then count how many times each number that the user input and print each one out. For some reason, I can't even get the for loop statement to print and it's pretty much the same as my other program except for the loop which is a little different.
//User inputs numbers between 1 and 100, program counts how many of each integer is and ends with a 0
import java.util.Scanner; public class occurrence { public static void main(String[] args) { //scanner/values Scanner input = new Scanner(System.in); int number = 0; int c = 0; //array count