(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);
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 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'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) {
I need to create a regexp, that will do the following:
a,a,a,a,c - matches c,a,a,a,a - matches a,a,a,a,a - doesn't match
I will be using it in Java. In the place of 'a', can be 'b' - they are equal. Also, in the place there can be any other character. This is what i have came up with:
It fails because it matches the 5 a's. I'm quite new to regexp, so I'm not aware of all the possibilities. It matches the 5 a's, because the first if fails, but the second does not. Maybe there is a simpler way to accomplish this? (Also why are the .* necessary in the middle?)
I would like to test whether the first word in a line is is a valid var name (e.g sequence of letters, digits and "_", it may not start with a digit, if "_" is the first char it must be followed by a letter). I tried this simple code:
String namePattern = "_[a-zA-Z0-9]+"; String text = "__c"; Pattern pattern = Pattern.compile(namePattern); Matcher matcher = pattern.matcher(text); "__c" is an illegal var name.
But it returns the string "_c", where it is supposed to return an empty matcher.
I am trying to write a regular expression for a text which has both String and number... like abc1234, xyz987, gh1052 etc. And the string usually contains 2 or 3 characters.
What I need is two Strings one containing the text (abc, xyz, gh etc) and other containing number (1234, 987, 1052, etc.). Have written the code below. but doesn't seem to work.
private static int getStrength(String pw) { int strength = 0; if(pw.length() >= 8){ strength++;
[Code] .....
This function doesn't seem to work for me. I believe the issue lies in the special character matching. It seems like it always returns true and adds to the strength. But I only want it to add to strength if at least one the following are in the password: *, -, _, ^, !, %
I am using the following regex - [a-zA-Z0-9]{9,18} which means I can use alphabets and numbers with minimum length as 9 and maximum length as 18.It should not take special characters.
It takes values like ADV0098890 etc. but it is also taking ADV0098890[] which is wrong.
I have a table which contains list of regular expression and its corresponding value.I have to fetch those value and put it HASHMAP where regex as key.I have to compare the each key with the given string(input) and If matches I have to get the corresponding Value for the regex.
With the code below, I am trying to replace all regex matches for visa cards within a given text file.
My first test was with a text "new3.txt" exclusively containing the visa test card 4111111111111111. My objective was to replace the card with "xxxx-xxxx-xxxx-xxxx". This was successful.
However, when modifying the text file to include other characters and text before and after (ex: " qwerty 4111111111111111 adsf zxcv"), it gives mixed results. Although it successfully validates the match, it replaces the whole text in the file, rather than replacing solely the match.
When trying this search and replace with words (rather than a regex match), it does not have this behavior. What am I missing?
import java.io.*; import java.util.regex.*; public class BTest { //VISA Test private final static String PATTERN = "(?s).*4[0-9]{12}(?:[0-9]{3})?.*"; public static void main(String args[]) { try
how can I extract data from two different files but produce one output. For example, the first three columns are from text_file_1 and the last column (the last foruth column of the output) is from text_file_2.
I'm trying to make my first servlet, however since I have only installed the Java SE, the javac compiler is complaining about missing packages (javax.servlet.http...)
I've realized now that I need javax.servlet jar or class files from Java EE, but after downloading java_ee_sdk-7u1.zip from Oracle here I'm not seeing any .sh or java_ee_sdk-7-jdk7-macosx-x64.sh installer (which I saw mentioned elsewhere). The oracle page only instructs to unzip the downloaded file, and searching for javax.servlet only reveals a few files which I'm not sure what to do with:
I'm looking to get two values from this string. I have tried re formatting and with little success. I read this in from a text file and then tried the below code to extract it. I have also tried
{Deposit=100.00, Fees Paid=5.00}
I am just looking to get the 100.00 and the 5.00. I tried using this code below but it separated it into Comma and put it in an array which is probably what im not looking for.
[code] final Pattern pattern = Pattern.compile("[=st]"); final String[] result = pattern.split(st2); System.out.println(Arrays.toString(result)) [code]
I have a program in which I am prompting users for integer values to display in a JFrame. I call the method below to load an array with their input:
Java Code:
public String inputAssembly(){ if (!jtfInput.getText().matches("d")){ JOptionPane.showMessageDialog(null, "Input must be of integer value."); } if (jrbFar.isSelected()){ return jtfInput.getText() + jrbFar.getText();
[Code] ....
Regardless of the input, both messages display (invalid input / got it). I've tried debugging so I know that the values are getting entered and read correct, at least to my knowledge. It is a very simple regular expression, only checking to be sure an integer was entered.