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


ADVERTISEMENT

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

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 Number Of Occurrences Of A String In Array List?

Apr 6, 2015

I am trying to count the number of occurrences of a string in an array list. I used the following code:

int count = Collections.frequency(strings, search);

strings is the name of the array list and search is the string that I am trying to count the number of occurrences of. My program compiles correctly, but when I enter the string to search for, I get the following error: "Exception in thread "main" java.lang.NullPointerException at java.util.Collections.frquency(Collections.java:37 18)

Why am I getting this error message and how do I fix it?

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

Repetition - Print Number Of Occurrences

Apr 11, 2015

find whether it is a repetition free number or not. If it is, print the same number(x), else if it has repetitions then print the number of occurrences of the first repetitive digit you encounter.

View Replies View Related

Returning All Occurrences Of C In String

May 13, 2014

Write a method that accepts an array of Strings and a character c. The method must return the count of all cs in the strings of the array.

Here's what I have written:

[
public int countC(String[] strings, char c) {
int count = 0;
for(int i = 0; i < strings.length; i++) {
for(int j = 0; j < strings[i].length(); j++) {
if(strings[i].charAt(j) == c) {
count++;
}
}
}
return count;
}]

Does this look correct?

View Replies View Related

Removes All Occurrences Of Specified String From Text File?

Apr 30, 2014

Write a program that removes all the occurrences of a specified string from a text file. For example, invoking java Exercise14_11 John filename removes the string John from the specified file. Your program should get the arguments from the command line.

I have a problem.

Text document is Match.txt
Match.txt contains a string: "Open the door John"

I tried to create a new file named doc.txt where to put the text from the Match.txt without John word

//to read from document
Scanner output = new Scanner("Match.text");
//to write to document
PrintWriter write = new PrintWriter("doc.txt");
//copy from original document
String copy = "";
while(output.hasNextLine())

[code]...

Error :NoSuchElementException

View Replies View Related

Accept String And Number Of Repetitions As Parameters And Print String Given Number Of Times

Oct 3, 2014

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);
}
}

View Replies View Related

Character Converting Into Number In 2D Char Array?

Mar 29, 2014

I'm trying to convert all letters in a 2D char array into number. As results, I want a = 0, b=1, c=2, ... z=25.

Then, I tried this as part of my code:

char [][] letters = {{'i', 'u'}, {'a', 'g'}, {'e', 'k'}};
for (int i = 0; i < letters.length; i++) {
for (int j = 0; j < letters[i].length; j++) {
if (letters[i][j] >= 'a' && letters[i][j] <= 'z') {
letters[i][j] = (char) ((letters[i][j] - 'a') + '0');
}
}
}

The result of my code is not what I expected before.

From 'a' to 'j', it worked well. But, from 'k' until 'z' it didn't print expected number.

What's wrong with my code and what is the correct way to fix it?

View Replies View Related

How To Print A Character Random Number Of Times

Oct 26, 2011

I'm trying to write a method that will print a string of a random number of X's (from 5 to 20) on a line, then another number of X's on the next line, etc. until the random number = 16.

I have to use while loops and probably fencepost algorithms. I'm confused with how to print the number of x's the random number of times over and over.

Right now, I'm pretty sure my for loop is wrong or unnecessary.

So far, this is my code:

public static void randomX() {
int x = 0;
while (x != 16) {
x = (int)(Math.random()*20) + 5;
for (int i = 0; i <= x; i++);
{
System.out.print("x");
}
}
}

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

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 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

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 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







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