Checks Whether First String Is A Substring Of Second
Mar 22, 2014
(Check substrings) You can check whether a string is a substring of another string by using the indexOf method in the String class. Write your own method for this function. Write a program that prompts the user to enter two strings, and checks whether the first string is a substring of the second.
What the function should return
public class Ex2 {
public static void main(String[] args) {
System.out.println("Input the first string:");
Scanner input = new Scanner(System.in);
String firstString = input.nextLine();
System.out.println("Input the second string:");
String secondString = input.nextLine();
check(firstString,secondString);
I am trying for a logic that i have some emp ids as a string seperated by commas and i need the substring of emp ids as below. splitting the string as below.
public static void main(String args[]) { String empId = "1,2,3,4,5,6,7,8,9,10"; int i; int count = 9; for (i = 0; i <= count; i = i + 3) { System.out.println("Emp IDs are : " +empId); }}
Result is:
Emp IDs are : 1,2,3,4,5,6,7,8,9,10 Emp IDs are : 1,2,3,4,5,6,7,8,9,10 Emp IDs are : 1,2,3,4,5,6,7,8,9,10 Emp IDs are : 1,2,3,4,5,6,7,8,9,10
But I want the result like:
Emp IDs are : 1,2,3 Emp IDs are : 4,5,6 Emp IDs are : 7,8,9 Emp IDs are : 10
I am writing a program that checks if a number is prime or not. Code below.
for(int i = 2; i < P; i++){ if (P % i == 0) { System.out.println(P + " Can also be divided: " + i); return; } } System.out.println("Prime number.");
It works but, if a number is not a prime. I need to print out all the numbers that it can be divided with. For example if a number would be 8: it can also be divided with 1, 2, 4, 8.
Write a program that reads number between 1,000 and 999,999 from the user and prints it without a comma separating the thousands. Read the input as string. Find the length of the string and use the substring to break it into two pieces. Do not include the comma. You can then concatenate using the + symbol to reassemble without the comma. For example if the number is 123,456. First would equal 123 and second would equal 456. noCommaNumber - 123456.
I do not think it is required to only allow numbers in that range in the code. My question is how to get the first half of the number before the comma in a string to make the last line work. What I think is not doing what I need is the line String first = numberIn.substring(numberIn.indexOf(",", 0)); This is just the last thing I tried.Getting the second half is done using IndexLastOf method in the substring class javaforumquest.jpg
I have to alter my Sentence class to find the index of the substring "sip" in Mississippi but I'm really not sure where to begin. This is what I have...
Sentence.java public class Sentence { private boolean outcome; private String sentence; public Sentence(String aSentence) { sentence = aSentence;
[Code] ....
I know that I need to change public boolean find(String t) to public int indexOf(String t) but I'm not sure what to start doing to get the index of "sip".
I'm still working with the singlylinkedlist data structure and trying to return a string of individual characters stored in each node. ( head--('J')---('A')---('V')---('A')---tail ) Hopefully this beautifully executed depiction to the left will clarify.
This is what I came up with after designing the algorithm w/ pen and paper. I know that I'm not accounting for the OutOfBound errors, an empty list, or an index < 0.... I will get to that.
Right now, I'm not sure why my assignment to the character array, ' chars[i] = cursor.getLink(getElement()); ' , is not working. The two methods getLink and getElement, type Node and T, respectively, exist in my Node class which is a private nested class in MySLList. Why would I be getting the following error: "The method getElement() is undefined for the type StringX" ? Is this a good design and implementation of the substring method?
public String substring(int index) { char[] chars = new char[(getSize() - index)]; //getSize() defines the size of list in MySLList Node cursor = null; //Set the cursor to the node = index if(cursor == head) {
How can I write a method that takes a string with duplicates letters and returns the same string which does not contain duplicates. For example, if you pass it radar, it will return rad. Also i would like to know how can I Write a method that takes as parameters the secret word and the good guesses and returns a string that is the secretword but has dashes in the places where the player has not yet guessed that letter. For example, if the secret word is radar and the player has already guessed the good guesses letters r and d, the method will return r-d-r.
I am currently trying to make a calculator in Java. I want to use the String split method to tokenize the string of characters inputted. I thought I was using the String split method wrongly, because I had surrounded the characters I wanted to delimit with square brackets. However, when I removed the square brackets, the code threw an exception when I pressed the equal button. The exception was a PatternSyntaxException exception. Am I using the String split method wrongly? And why is the exception thrown? Here is my code:
import javax.swing.*;//import the packages needed for gui import java.awt.*; import java.awt.event.*; import java.util.Arrays; import java.util.List; import static java.lang.Math.*; public class CalculatorCopy { public static void main(String[] args) {
i am trying to write a class method which will take in a string and returns a string which is the reversed version of that string. it compiles fine but when i try to run it it states Main method not found in class StringReverse,please define the main method as public static void main(String[]args). I am new to java and cannot figure out
import javax.swing.JOptionPane; public class StringReverse { public String reverseString(String str){ JOptionPane.showInputDialog(null,"Please enter word"); char c = str.charAt(str.length()-1); if(str.length() == 1) return Character.toString(c); return c + reverseString(str.substring(0,str.length()-1));}}
I'm having trouble with the last few lines of the code. It's supposed to take a replacement string entered by the user and print out the new string. For some reason it's now allowing me to enter a replacement string
import java.util.Scanner; public class Project02 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter a long string: "); String lString = keyboard.nextLine();
[Code] ....
Output:
Enter a long string: the quick brown fox jumped over the lazy dog Enter a substring: jumped Length of your string: 44 Length of your substring: 6 Starting position of your substring in string: 20 String before your substring: the quick brown fox String after your substring: over the lazy dog Enter a position between 0 and 43: 18 The character at position 18 is x
Enter a replacement string: Your new string is: the quick brown fox over the lazy dog <------ isn't taking user input
I have a method for a button so when a user inputs something it then will get the string value and check it against the string value within the properties file to see if it exists.
The properties file is called GitCommands.properties that contains -- > key = value <-- in it
I realised I have not used it correctly hence why I keep getting errors - I am lost on how to use it, I think perhaps that may be the issue here? I need to reference the file but I am doing it wrong? When I do use that piece of code I get null pointer exception too...
JButton btnSearch = new JButton("Search"); btnSearch.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { FindSelectedKey();
[code] .....
I understand I am missing my piece of code where it states "//determine whether the string is equal to the property file key string" I understand the logic fine but not actually coding it.
I am trying to split a string based on length(example length 5) of the string. But I am having a issues with this substring(start, end) method. I get all substring which are of length 5. But if the last substring is less than 5 then I am not getting that last substring. But I need the last substring even if it is less than 5.
Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.
public class test { public static void main(String[] args) { Printhelloworld(); String[] verbs = {"go", "do", "some", "homework"}; printArrays(verbs);
I'm having trouble to compare two string from my LinkedList. I took me 2 days now trying figure out how to compare the current string to previous string in the linkedlist. Here is my code.
public int compareTo(LinkedListNode n){ //Compare two string String myHead = data.toLowerCase(); String comparableHead = data.toLowerCase();
How do I compare a String to each element of a string array?
For example:
int headscount = 0; if (coins[i].equals("heads")){ headscount++; System.out.println("b" + headscount); }
This doesn't give me the right value because the IDE says that equals() is an incompatible type. I also tried changing the "heads" to an variable, but the results remains the same.
So I'm creating a class which when given three inputs uses them as sides of a triangle and tells ther user what type of triangle it is, or if the input is invalid, tells them why it is invalid. I'm readin the input as a string and then trying to split it into a string array, from there checking to see if it has 3 elements.. in which the data is good at that point, and then converting them to ints and checking to see if they're negative ansd finally checking to see if they can work as sides of a triangle ie a+b >c, a+c >b , b+c >a.
I'm trying to split it into an array of strings but am getting an error, and can't seem to figure out why as this should be working from what I've read of the string.split method online.
import java.util.*; public class TriangleTest{ private int sideA; private int sideB; private int sideC; public static void main(String[] args){ TriangleTest triangle = new TriangleTest("3 4 5");
I have a string value returned from a background tool that will range from 0 to possibly terabytes as a full number. I want to format that number to use commas and to reduce the character count using an appropriate size modifier (KiB, MiB, GiB, etc). I've tried converting the string number to a Double value using Double.parseDouble() and then performing the math based on the size of the value with this code:
I have errors in the "if" and both "else if" ... The compiler says "cannot convert from String to boolean and int to String ...
instructions:
1. Add two private instance variables, String courseName and char grade to this class.
2. Add accessor and mutator methods for these instance variables.
3. Add a method register which receives an integer data type and returns String data type according to the argument passed to it ("Math" for 1, "English" for 2, "No course" for any other input)
What I have so far:
package assignment9; public class BannerUser { private int userId; public int getUserId() { return this.userId; } public void setUserId(int userId)
I have the the string value similar to the one which i have to split based on the delimited "," String SampleString=" 'ab,c', 'xyz', lm,n,o "
I know I can easily call split function which will eventually split the above string. But in my case the delimiter "," , is also a part of the string. If I call the function SampleString.split(',') I will get the output as listed below
ab c xyz lm n o but the expected output is abc xyz lmno
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); } }