Taking String That Contain Sentence And Reversing The Words
Sep 5, 2014
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.
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" };
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 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
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
This is the code that I have so far, It will only print out the digit if it is entered first..How to I get it to print out all digits? and I am getting an error that the c is not initialized
import java.util.*; //Write a program that prompts the user for some text. Output only the digits in that text. Hint: Use a loop and the Character.isDigit method. public class Q1 {
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();
Aeparated by blanks in a string. For simplicity, use strings without punctuation or other white space characters(tabs, newlines etc). Use a JTextArea to allow the user to enter the text and allow the text area to scroll if necessary. when the user clicks a button to count the words , the total number of words counted is displayed in a textbox that cannot be modified by the user.
now my problem is that i am not getting the counted number to display in the un-editable textbox. i also have the problem where the cusrsor is showing in the middle of the input screen instead of at the top.
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.text.*; import java.util.*; public class WordCounter extends JFrame implements ActionListener
Working on my java skills. The is Delimiter function is suppose to return true if there is a delimiter in the string and false otherwise. The main is suppose to call this function and count the number of words in the string:
Here is what I came up with:
import java.util.*; public class NumberOfWordsInString { public static void main (String[] args){ Scanner in= new Scanner (System.in); String s;
[Code] ....
Somehow my method isn't being successfully called by my main and the following error msg was received:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
length cannot be resolved or is not a field
The method isDelimiter(char) in the type NumberOfWordsInString is not applicable for the arguments (char[])
at javaimprovements.NumberOfWordsInString.main(Number OfWordsInString.java:10)
line 10: char ch [] = new char [s.length];
and the main doesn't call the isDelimiter function
The program is to accept a string and display all the palindrome words(the words that are same even if they are reversed-------->mom,madam,malayalam etc.)in the string.
I need to solve this program urgently.
There are no syntax errors but after typing in the sentence,no output is displayed. This is my program....
import java.util.*; class palindrome_test { public static void main(String args[]) { Scanner in=new Scanner(System.in); System.out.println("Enter a sentence"); String usersInput=in.nextLine();
Write a menu driven program that either accepts words and their meanings, or displays the list of words in lexicographical order (i.e. as in a dictionary). When an entry is to be added to the dictionary you must first enter the word as one string, and then enter the meaning as separate string. Another requirement - from time to time words become obsolete. When this happens, such word must be removed from the dictionary.
Use the JOptionPane class to enter the information.
Use the concept of linked list to carryout this exercise. You will need at minimum the following classes:
- A WordMeaning class that hold the name of a word and its meaning. - A WordMeaningNode class that creates the node of information and its link field. - A WordList class that creates and maintain a linked list of words and their meanings. - A Dictionary class that test your classes.
For the output, the program should produce two scrollable lists:
- The current list of words and their meanings. - The list of the deleted words. You need not list the meanings, just the words.
So far, I have everything coded except for the remove method, and I am not sure how to code that. I coded the add method already, but now I don't know where to begin with the remove method in my WordList class. My classes are below.
WordMeaning Class:
public class WordMeaning { String name; String definition; WordMeaning(String t, String d) { name = t; definition = d;
How to calculate the minimum and the maximum in the same program.
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int integers; int[] numbers; numbers = new int[10]; int max; int min;
[Code] ....
My result when I input numbers are always the following:
"Numbers in reverse order are: 9, Numbers in reverse order are: 8, Numbers in reverse order are: 7, Numbers in reverse order are: 6 Numbers in reverse order are: 5, Numbers in reverse order are: 4, Numbers in reverse order are: 3, Numbers in reverse order are: 2, Numbers in reverse order are: 1, Numbers in reverse order are: 0."
I am writing a program to reverse a user inputted number (Example - 54321 is 12345) The code works great minus the fact that if I input a number that begins with zero, the output will drop the zero. Would it make more sense to just set this up using strings?
import java.util.*; public class Week7_Programming_Problem { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int inputNum, outputNum;
My program is supposed to use recursion and iteration to sum and reverse elements in an array of Longs. The array is supposed to be 1000000 cells and I am supposed to compute the average times of 99999 trials. When I try to run the program, it keeps saying running.... but doesnt ever do anything.
public class PA2Delegate { private long[] start; private long[] end; private Long reversal; private Long sum = (long)(int)0;
This piece of code is giving me the most trouble as I cannot convert the self defined class (Fraction) into int in order to store the elements in the variable (max). What is the best way to go about grabbing the next element and storing it in max and compare it to the previous to get the highest and lowest value?
System.out.println("Enter numerator / denominator"); frak[0] = new Fraction(keyb.nextInt(), keyb.nextInt()); frak[0] = max; for (int i = 1; i < frak.length; i++){ // starting from array element 1 System.out.println("Enter numerator / denominator"); frak[i] = new Fraction(keyb.nextInt(), keyb.nextInt()); if (max > frak[i]){ }
I have written a java app that's been working for a few years now and the client is very satisfied. However, a certain user has been experiencing a problem where the app runs fine BUT whenever it interacts with the OS (Win7 in this case), it takes ~10 mins to respond. Actions include, for example, exporting the currently displayed JTable to a CSV file or making an OS call to open up MS Word, etc.
I copied the users version of the app onto my platform (win 8.1) and tested it on winXP too with no problems which points towards an issue on the users particular PC. Furthermore, it used to run on the users PC fine until something (??) happened.
I've tried the following so far:-
1. looked at System restore to check anything new installed - nothing obvious 2. disabling anti virus - problem still occurs 3. monitoring JVM process time using Task Manager - little CPU time seems to be utilised (=> no loop etc)
I'll try :-
1. using JvisualVM - although I don't think this will show me what's going on with the OS 2. excluding the JVM and the app from all anti malware
Any app or something that shows interaction with the OS so I can trace what might be intercepting the call from my app to the OS?
I have a small bug in my program. The user is asked what person(s) information they want to access but lets say they want captain they must enter "captain" twice. I think it will make more sense to you with the code. I have searched all over to see what is causing the bug but still have found no resolution. I even tried making two different scanners but that didn't work either.
I know the while loop (line 16) I am using is causing the bug because it works fine without that but then I cannot validate the input.
package myproject; import java.util.Scanner; public class Enterprise { public static void main (String[]args){ String userInput;
What I have so far are two classes, Movie class and MovieTestDrive class. I've to get the title, genre and ratings for movies and then put them into an array and call the playIt() method.
I would like to create a for loop to iterate three times for three instances of the Movie class but I don't know how to do it.
When I try to run the code, it won't work and I don't know if this is because I have Scanner running incorrectly whilst encapsulating my data. What I have so far:
class Movie { // Create instance variables for the Movie class. private String title; private String genre; private int rating; // Use getters and setters to set and display the variables.
I am working on a web-application where i have a functionality which generates reports based on the data from DB. for small amount of data its working nice if the data is huge its taking more time. to avoid this problem i developed a standalone batch where i generated the jar file for the sources in web-application and made a call to the appropriate methods.
But my problem is if web-application takes 10 secs to generate the report but in the case of standalone its taking nearly 3-5 mins to generate the same report. i didn't do much of the changes in code just using the same code as web-application
I am using tomcat 6 application server to run the web-application for standalone using batch commands.....