Replacing And Checking Character In String?

Jul 24, 2014

so my task is to write a code which would ask user to input the year as integer and first three letters of the month with first being an upper case letter. Than the program would determine the number of days for given year and month.

Currently I have a brain crash on how to check, if the user has provided the month with first character being upper Case. If not, than the program would automatically correct the character. Problem starts at line 17.

import java.util.Scanner;

public class DaysOfMonth4_17 {
public static void main (String[] args) {
//Initiate scanner
Scanner input = new Scanner (System.in);
//Ask for year input and use is as INT
System.out.println("Enter the year");

[code]...

View Replies


ADVERTISEMENT

Regex Special Character Checking

Oct 4, 2014

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: *, -, _, ^, !, %

View Replies View Related

Replacing String In Case Of Exact Match Only

Oct 15, 2014

I have 2 strings

String value ="/abc_12_1/abc234/abc/filename.txt";
String src="abc"
Sring tgt=xyz

Now I have to replace the string in variable value in case of exact match; I am trying to do the following :

if(value.contains(src)
{
value.replaceall(src,tgt);
 }

Now the problem here is it replaces all the occurrence of abc in the string value and I get the below output as :

value=""/xyz_12_1/xyz234/xyz/filename.txt";

However my requirement is only in the case the value exactly matches with source the replacement shd happen. I am expecting the output like this :

String value ="/abc_12_1/abc234/xyz/filename.txt";

Also the above code is in a function which will be called multiple times and the values will keep  on changing. However the target and source will remain the same always.

View Replies View Related

Checking Values In A String

Apr 15, 2015

Suppose i have a string which has certain file names, S = "a.png, b.gif, c.xlsx, d.docx, e.xlsx, f.gif";I need to check if the string has more than one .xlsx file names,

View Replies View Related

Checking ArrayList For Certain String

Feb 16, 2014

The objective of my program is to read a file called input_data.txt and to calculate an invoice in which my program will print the results in a formatted text file. I haven't advanced to this step yet because I'm having trouble checking my ArrayList for a certain string. The reason I am searching for "duration" is so that my program will know when the heading is complete.

Here is the sample input file given:

account_number start_time_of_call duration
10011A 21:50 20.2
10011A 13:23 12.3
10033C 23:00 34.0
00101B 10:23 45.1

Eventually, I must separate the fields and calculate payment prices based on the duration and time of the calls.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class spInputOutput {
 
[Code] .....

View Replies View Related

Checking For Regex Matches At The End Of A String

Jan 31, 2014

I have a regular expression that I created and tested on some regex site, [URL] .... It worked on the test site but I can't get it to work in Java.

The code is:

String s = "MyString.ext_CAL3";
assertTrue("Didn't Match", s.matches("_CAL[0-9]+$"));

This test always fails. I cannot figure out what is wrong with it. In fact, I can't get any match no matter what regex I use.

View Replies View Related

Reading Input With Scanner And Checking If User Entered Empty String?

Apr 7, 2015

The project is a program that allows the user to enter students and enter grades for each student. One of the requirements is that if there is already a grade stored for the student that it will display the previous grade. IF the user then enters a new grade the new grade will be stored. IF the user simply presses enter (enters an empty string) nothing is done. I have everything working except for the requirement of doing nothing if the user enters an empty string. If I just press enter at this point I get a NumberFormatException.

The below code is a method "setTestGrades" from the student class. This method iterates through each student object stored in an array list and then checks if there is a previous grade (Requirement# unset grades have to default to -1) before allowing the user to set a new grade.

public void setTestGrades(int testNumber) { //Sets the grade for the specified test number for each student in the arraylist.
testNumber -= 1;
Scanner input = new Scanner(System.in);
for (int i = 0; i < studentList.size(); i++) {
System.out.println("Please enter the grade for Test #" + (testNumber + 1) + " For Student " + studentList.get(i).getStudentName());

[code]....

View Replies View Related

Splitting A String By Character

Feb 14, 2014

I'm trying to use the split method to split a string for a calculator, but it keeps throwing this Exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+

public class exp {
private static String ques="88+12";
private static String [] splitCalc;
public static void main(String[] args) {
splitCalc = ques.split("+");
for(String s : splitCalc){
System.out.println(s);
}
}
}

View Replies View Related

Removing A Character From String?

Sep 26, 2014

I am trying to remove a character from string as below

public String reomveChar(String str, int n) {
if(0<=n & n<=(str.length()-1)){
//str.charAt(n)
char o=str.charAt(n);
return str.replace("o","");
//a.replace("o","");
}
return null;
}'

what is the best way to remove it.

If i call like

removeChar("hello", 1) i should see hllo.

When there are more than one way to do a challenge in java which one to choose as best way.

What is the criteria to decide one particular way of writing code is best among say 'n' different number of ways.

View Replies View Related

How To Retrieve A Character From String

Mar 7, 2014

String charAt() method returns a particular Character at the specified index in the String.

How is this function implemented internally?. How a particular character can be retrieved from String?.

View Replies View Related

Swapping String First And Last Character?

Sep 28, 2014

I am trying to swap given string first and last character. If I pass world i should get 'dorlw'

I found link and code [URL]....

String str="hello";
char[] charArray=str.toCharArray();
int indexOfLastChar=charArray.length-1;
for(int i=0;i<charArray.length/2;i++){
char temp=chatArray[i];
charArray[1]=charArray[indexOfLastChar-i];
charArray[indexOfLastChar-i]=temp;
}
String reversedStr=new String(charArray)
System.out.println("reversed string"+str+reversedStr);

I wonder Is there is any easy simple way to do it.

View Replies View Related

Number Of Occurrences Of Specified Character In A String

Oct 16, 2014

the number of occurrences of a specified character in a string...i tried to do the program occurrences in a given string and i tried the code as below.

code:

import java.util.*;
public class Occurrence
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Uppercase Character Counter In A String

Jun 11, 2014

I have to create a code that can calculate the number of upper case letters in a string that is entered by the user (below.)

Java Code:

import javax.swing.JOptionPane;
public class mainClass {
public static void main (String [] args) {
String userInput;
userInput = JOptionPane.showInputDialog("Enter a string.");

[Code] ....

My issue is that I would like the program to be able to function properly when spaces are entered into the string. As it is right now, I believe it is only processing the first string entered into the input box.

View Replies View Related

String To Character Array Conversion

Jul 17, 2014

How I can convert a string into a character array without using toCharArray???

View Replies View Related

Unique Method To Search A Character In String

Oct 14, 2014

Which string method to search a particular character in a string whether a particular character is there or not . Suppose my String is like............

String s = "Java.";

How I can find the character " . " in above string "Java." is present or not . What is the string method to search that "."

View Replies View Related

Text Fields - Input String Character S

Aug 24, 2014

What should be the code if i want to input a different string in case of the typed string. The case is : I have a predefined string S = "Peter,please answer my question" and now when i input another string inside the text field character by character i want characters from the string S to enter instead of the input string. In short, the input string should be disguised as string S.

View Replies View Related

Print String From Starting Character Given By User

Jul 14, 2014

Now I am doing dictionary app using files..

Here i want to print the String from Starting character which is given by user..Here i given below the image like my concept..here we put .(dot and enter R)eclipse will print the method name which is starting from R..

View Replies View Related

Finding Most Common Character In A String Using For Loop

Feb 7, 2007

import javax.swing.*;
public class mostFrequent{
public static void main(String args[]){
char index;
String s;
s = JOptionPane.showInputDialog("Enter String here");
int currFrequency = 0;
for(index = 0; index<s.length(); index = index++){
int i = 0;
for(i = 'A'; i<='Z'; i++){
if(i==s.charAt(index)){
currFrequency = currFrequency + 1;
}
}
}
System.out.println("end");
}
}
//my code so far

View Replies View Related

Get Character Count And Amount Of Vowels In String Array?

Jan 29, 2015

I have to write some code to take names from the user and then order them in alphabetical order and then print them out which i have managed to do. However, i can't get it to count the characters in the names that the user enters or count the amount of vowels in the names.

This is the code ive written:

import javax.swing.JOptionPane;
import java.util.Arrays;
String[] names = new String[9];
int i;
names[0] = JOptionPane.showInputDialog("Please enter a name");
names[1] = JOptionPane.showInputDialog("Please enter a name");

[code]....

View Replies View Related

See Occurrences Of A Character In A String Stored In Text File

Apr 26, 2014

I have this program to see the occurrences of a character in a string stored in a text file. My text file has the following contents (as shown below):

P-P-N-N-L-V
N-R-U-S-R-Q
K-Q-E-L-E-A
A-J-B-G-E-F
F-E-L-Q-R-R
U-F-J-V-I-M
P-I-Q-K-B-K
U-N-L-F-O-B
M-A-N-M-H-O
P-Q-J-D-N-I
G-U-O-D-F-I
Q-M-M-B-C-Q
N-B-I-L-E-J
S-A-T-Q-H-U
C-L-G-U-J-M
R-P-V-M-A-A
C-R-A-V-L-B
M-U-Q-K-M-Q
E-I-C-H-V-J
J-N-N-K-R-P

As you notice, each character is seperated by a hyphen (serving as its delimiter). I want to only go through 10 lines in the text, instead of all 20 of them. So, I wrote the program in such a way that I intend to reach into the indices of each character in the text file. Since there are six characters in each line so that means there are 6 indices - and there are 10 lines I only want to go through. So, 6 indices times 10 lines equals 60 indices. Hence, there are only 60 indices I need to go through. In that manner, it's like I have gone through only 10 lines through that way.

It compiled perfectly fine but upon running it, I ran through an error in the black DOS screen that says

"java.lang.ArrayIndexOutofBoundsException: 6 ".

How do I work around that?

The code I wrote is shown below...

import java.io.*;
import java.util.*;
public class hotandcoldclusterdeterminer_test {
public static void main (String args [])throws IOException {

[Code]....

View Replies View Related

String Class Private Final Character Length

Feb 27, 2015

The String class stores the characters of the string internally as a private char[] and calling someString.length() results in getting the length field from the character array. I am looking to get the details on how the length is implemented. I understand it is a field, but in the original question I provide sample code and really want to know if/how the resulting byte code may differ when compiled, perhaps I am just not seeing the simple answer through my confusion.

Link ....

View Replies View Related

Validate String Whether It Is Combination Of Lowercase And Uppercase And Has Character

Jun 22, 2014

I have to validate a string whether it is a combination lowercase and uppercase and has the character '_'.

I m having trouble in framing the expression for these conditions in the matches function.

How it can be done..?? How to frame a the correct expression.

View Replies View Related

Program To See Occurrences Of A Character In String Stored In Text File

Apr 26, 2014

I have this program to see the occurrences of a character in a string stored in a text file. My text file has the following contents (as shown below):

Quote
P-P-N-N-L-V
N-R-U-S-R-Q
K-Q-E-L-E-A
A-J-B-G-E-F
F-E-L-Q-R-R
U-F-J-V-I-M
P-I-Q-K-B-K
U-N-L-F-O-B
M-A-N-M-H-O
P-Q-J-D-N-I
G-U-O-D-F-I
Q-M-M-B-C-Q
N-B-I-L-E-J
S-A-T-Q-H-U
C-L-G-U-J-M
R-P-V-M-A-A
C-R-A-V-L-B
M-U-Q-K-M-Q
E-I-C-H-V-J
J-N-N-K-R-P

View Replies View Related

Count And Display Number Of Times Specified Character Appears In String

Mar 7, 2014

Write a program using a while-loop (and a for-loop) that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. (So, you will have two separate program codes, one using a while-loop and the other one using a for-loop.)

Example:
Enter a string: "Hello, JAVA is my favorite programming language."
Enter a character: e
The number of times the specified character appears in the string: 3

I don't even know where to begin I've only got this

import java.util.Scanner;
 public class letterCounter {
 public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String myString = sc.nextLine();
System.out.println("Enter a letter");
String letter = sc.nextLine();
}
}

View Replies View Related

Program That Read TXT File And Count Character Occurrence In String

Jan 12, 2014

I wrote a program to read a .txt file and return how many times a, e, s, and t occur in the .txt file. I am getting an error that I do not know how to fix. It says Error: FileNotFoundException cannot be resolved to a type...

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.BufferedReader;
public class Count
{
public static void main (String[] args) throws FileNotFoundException {
String phrase; // a string of characters

[Code] ....

View Replies View Related

Swing/AWT/SWT :: JLabel That Robotically Type Words Character By Character

Feb 2, 2015

i want to have a JLabel that could "robotically" type words character by character.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved