Write a method that prints characters using the following: public static void printChars(char ch1, char ch2, int numberPerLine). This method prints the characters between ch1 and ch2 with specific numbers per line. Characters are separated by exactly one space.Test your method with the following main method:
public static void main(String[] args) { printChars(‘A’,’z’,10); printChars(‘0’,’9’,5); }
I want to write an application that inputs a sentence, tokenizes the words with method split and reverse the words (not the letters)...I am stuck at the last part: how to reverse them...should I use the method reverse(); ?
Here is my code..
import java.util.Scanner; import java.util.StringTokenizer; public class ReversedWords { //execute application public static void main( String [] args) { //get sentence
To delete "the" from string and display the new string my input------ the dog required output--------- dog my output-------------------thedog
Code :
import java.util.*; class the { public static void main(String args[]) { Scanner in=new Scanner(System.in); System.out.println("Enter a sentence"); String s=in.nextLine();
I am trying to write a simple program that checks if a user's input has a specific letter using the ".contains" method but its not doing what i wanted to do. here is my code below.
import java.util.Scanner; public class secret { public static void main(String args[]) { Scanner sc; char hidden='a'; String guess;
So I have to design a program that takes a word and reverses the order of it. The exact directions are:Write a recursive method void reverse() that reverses a sentence. For example:
[code = Java] Sentence greeting = new Sentence("Hello!"); greeting.reverse(); System.out.println(greeting.getText()); [/code] prints the string "!olleH".
Implement a recursive solution by removing the first character, reversing a sentence consisting of the remaining text, and combining the two. So basically my biggest issue is my method to actually reverse the word.
public class Sentance { private String Word; public Sentance (String aWord) { Word = aWord;
I am not very comfortable with Strings in Java. The problem I am facing is, taking a string that contains a sentence and reversing the words. Example would be, "Hi I am Bob" and changing it to "Bob am I Hi". Then returning the String.
My initial thoughts were to change the string into a character array and then manually doing the work with loops and tedious comparison statements. I quickly realized that there must be a better way but I am not very familiar with strings in Java to know what a more sufficient way would be.
i am new to programming skills it may be silly question for experience but for me it's new thing. actually i tried a lot but i am facing problem when i trying to take input through Scanner. if i will take input a sentence directly as a string it's working . but when i am trying with Scanner the first word is showing next are not showing
public class Demo2 { public static void main(String[] args) { String s1="hi how are you"; s1=s1.replace('a', 'A'); s1 =s1.replace('e', 'E'); s1 =s1.replace('i', 'I');
[Code]...
this is working properly.
but when i trying with Scanner i am facing problem.
public class Demo2 { public static void main(String[] args) { java.util.Scanner scn= new java.util.Scanner(System.in) String s1=scn.next(); s1=s1.replace('a', 'A');
Ok, so I have to make a program that tells the user to type a sentence and at the end of the sentence a "." must be put to end the program. If a "." period is not entered, it must re-prompt the user to enter a period. On top of all this, the program has to count the number of letters in the sentence. I cant seem to get the while loop working and I cant find anywhere how to make a counter count the number of letters.
import java.io.*; class Sentence //The purpose of this program is to
The purpose of this program is to translate a user inputed sentence into morseCode. It seems like everything is right to me, but I'm simply not getting output! What am I doing wrong, or what should I add/change?
Here is the main class:
public class MorseCode { public static String myInput; public static String[] myMorse; public static String myUserInput; public static char[] myAlph = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; public MorseCode()
My assignment is to write a program that will encrypt and decrypt a sentence entered by a user but the encryption is to be random using an array. Can I convert my sentence(string) from char to int then create a random array to encrypt?
import java.util.Scanner; import java.util.Random; /*SentenceEncryptionProgram */ public class SentenceEncryption { string sentence; //sentence entered by user
I've been given an assignment for my Programming Fundamentals class that requires the user to enter a word/sentence to encrypt or decipher, as well as requiring the user to enter a key in which to encrpyt/decrypt the input by.
So a = no shift, b = a shift by 1 and so on.
But I'm completely lost as to what to do, and after searching for Vigenere Cipher topics, they look completely different to mine and (hardly) include prompts?This is my code:
/** * @(#)VigenereCipher.java */ import java.util.Scanner; public class VigenereCipher { public VigenereCipher()
Basically the requirements are to take a sentence (string of text) and check to see how many times specific words come up and then add to the counter depending on the word.
But I can not seem to get it to add the instances of the goodwords and badwords.
package Strings; import java.io.*; public class SentimentAnalyser { private static String analyse(String text) { int pw = 0; int nw = 0; String[] searchword = { "bad", "terrible", "good", "awesome" };
This is a program to input a sentence and print the words that start with a vowel.......There is no syntax error but on executing and typing the sentence and pressing enter,I get a error saying :
java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) at sentence.main(sentence.java:22)
You are given 2 files - "testcase.txt" and "validwords.txt".In testcase.txt, the sentences are written without any space inbetween/for ex: this is a sentence which looks ambigous.You are to insert a space at the proper place by breaking the sentence into correct words, ex o/p: this is a sentence which looks ambigous.validwords.txt contains all valid words (as 1 word per line). If you find a word that does not match, replace it with ####. After correcting
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