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


ADVERTISEMENT

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

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

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

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

Returning A String To The Main?

Oct 12, 2014

I am writing a code where in the first method the question will ask whats your favorite website. for example www.javaprogrammingforums.com...when it outputs it will read just "javaprogrammingforums" without the www. and the .com.

Because the program will ask a series of questions in the main, I would like website question to be returned to the main. Here is my code, and what can I do?

import java.util.Scanner;
public class chapter3 {
public static String website(Scanner kb) {
String website;
System.out.println("What is your favorite website?");
website = kb.next();

[Code] ......

View Replies View Related

Returning Text From A String Object?

Feb 19, 2015

I run a bash command and save the output as a string object

String str1 = "" +getLastOutput();

str1 would look like this: abcd efgh lkmn xsds

In this string ( str1) i need to check three separate words are there.

If i have an if statement with str1.contains("xyz") && str1.contains("lkmn") &&str1.contains("zzzzzzzzzzzz")

How can i get the one that is not there returned to the screen? so i can use elsewhere?

View Replies View Related

Changing String To Int - Returning Unwanted 0

Oct 17, 2014

public class Lab07 {
public static void main (String[] args) {
System.out.println(convertToInt("123"));
} public static int convertToInt(String str) {
int conversion = 0;
int i;

[Code] ....

The only methods I am aloud to use is length and charAt, which I have done. I see why it is returning a 0 at the end of the value returned, its because I set my int conversion = 0. If I just declare it I get a compiler error that it must be initialized.

View Replies View Related

EJB / EE :: Create Stateless Bean That Instead Of Returning A String Returns Object

Jul 31, 2014

I'm training myself with the EJB 3 technology. I would like to create a stateless bean that instead of returning a String, it returns an object. I tried in the same way I did with the first exercise, but I'm getting several errors.

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

Word Puzzle Game - Add Extra No Occurrences

Oct 20, 2014

I have a simple Word Program here called PuzzleGame.java. It works perfectly but I wanted to add some bells and whistles to it. So I want to be able that if the user selects the same letter more than once a message comes up letting the user know that "That letter has already been selected, try another letter."

I am certain that if would go in the 'else' of the 'if( )' that I am providing.

import java.util.Scanner;
public class PuzzleGame {
public static void main(String[] args){
String secretPhrase = "BIG JAVA";
String guesses = " ";

[Code] .....

I am thinking maybe after the else in this if statement.

if(guesses.indexOf(secretLetter)== -1) {
System.out.print("-");
notDone = true;
} else {
//something here. I am not sure how to go about it.
System.out.print(secretLetter);
}

View Replies View Related

Array With Occurrences - Gather Sales Amount For The Week

May 3, 2014

Here is what I'm trying to do using arrays.

gather sales amount for the week
display the total of sales
display the average of sale
display the highest sale amount
display the lowest sale amount

using occurrences determine how many times the amount occurred for the week

make a sales main
make a sales data

This is what i have so far but I'm confused on how to start the occurrence and where it would be placed in order to get the information from the array

public class SalesData {
private double[] sales; // The sales data
/**
The constructor copies the elements in an array to the sales array.
@param s The array to copy.
*/
public SalesData(double[] s) {

[Code] .....

View Replies View Related

Find And Remove All Occurrences Of Given Integer And Return Amended Array

Jan 12, 2014

Question - Given an specific integer and an array of integers, find and remove all occurrences of the given integer and return an amended array. I solved it. Here is my solution -

public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 4, 4, 5, 6, 7, 8, 8, 8, 8, 7, 7, 9};
int input = 8;
int newLen = nums.length;
for(int i=0; i<newLen; i++){
if(nums[i] == input){

[Code] ....

View Replies View Related

Why Method Is Not Returning A Value

Apr 29, 2014

I'm new to Java and I have a problem with a method, I can't see the code of the method, I just have a jar, but it should return a boolean, something like this:

boolean band = false;
band = TestClass.testMethod("blabla");
// band = false

The problem is that the method seems that is returning nothing (band remain false), and if I initialize band to true:

boolean band = false;
band = TestClass.testMethod("blabla");
// band = true

band remain true, in other words, the value of band is never modified, the question is, how is this possible? because it should return the same value on both calls, true or false, no matter the initial value of the variable that is receiving the returning value of the method.

View Replies View Related

Returning Values From Table?

Apr 6, 2015

This simple program should build a new table with random values and on the getValue method (perhaps called from othe TestClass) return back content of particular cell. why my getValue method doesn't work. Nay! It returns error while compilation.

import java.util.Random;
class TablicaIntowa{
public int getValue(int a){
return tabl[a];
} //getValue method end

[Code]....

View Replies View Related

Returning Values From Arrays

Nov 22, 2014

I have created a class and a matrix of doubles (or at least, I think I have, that's partly what I want to verify).I need to return the values of the array,Here is my class:

public class example{
double[][] Position=new double[2][11];
double calculate(){
for (int time=0;time<=10;time=time+1){
Position[1][time]=time;
Position[2][time]=time+1;
double A=Position[2][time];
return A;
}
}
}

I am getting the error: "This method must return a result of type double", though to me it looks like I am returning double (A).

View Replies View Related

Returning A Result And Then Using It In Main

Jan 4, 2015

I've made a class called Car with a method which will tell me a category for the engine size regarding the actual size (which I've included in the main just so I could see if it works) but everytime I test it I get an error.

public class Car {
public String b;
public String c;
public double es;
public double cs; 
public String getCategory() {
if (es < 1.3)

[Code] ....

Figured it out. Was missing parenthesis on audiCar.getCategory();

View Replies View Related

Why Terminal Not Returning Results To IDE

May 14, 2014

i am trying to run a command in terminal the code is below if i run the command in terminal it works fine however when i run it from netbeans with code below nothing gets printed. however if i run a different command such as (ip addr) it works fine?

public static void a() throws IOException{
ArrayList lister=new ArrayList();
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ps -ef | grep firefox");// the command i am trying to run to get pid of application
InputStream stderr = proc.getInputStream();

[Code] ....

View Replies View Related

Returning Data From A Constructor?

Sep 1, 2014

I'm having an issue returning data from a constructor. This is an assignment, and the specifications were that two classes are to be used, one for the variables and assigning methods, and one for the main method and the printing. This app compiles, but returns "0" for the isbn number, and I'm sure it's because I'm not doing something right with my constructors. My code is below

public class Book {
/* Declare Variables */
public static int isbn;
/* Constructor */
public Book (int isbn) {
isbn = 454545;

[code]....

There are other variables to add, but if I can get one working, I can get the rest working.

View Replies View Related

Returning Values With Methods?

Sep 24, 2014

The code is below. The program runs a series of calculations based on data input by the user. My problem is that for the most important thing I'm looking for, total kg CO2 emissions, I continually get an answer of 0.0. What I need is a sum of the individual total emissions as calculated in each method, i.e. the values which are printed with the following: System.out.println(trans); System.out.println(elec); and System.out.println(food);

The total should be something like 25040 or whatever, depending on the value of the inputs provided by the user, but I'm constantly getting a total of 0.0., which is obviously false. Could have something to do with the way I've initialized my variables, or something to do with the limitations of returning values from methods.

import java.util.Scanner;
 public class CarbonCalc {
 public static void main(String[] args) {
double trans = 0;
double elec = 0;
double food = 0; 
giveIntro();
 determineTransportationEmission(null);

[code]....

View Replies View Related

EJB / EE :: Returning Multiple Values In QL

Mar 27, 2014

I want to return multiple values in my EJB query. The query goes something like this:

SELECT SUM(s.sales) SUM(s.cancels) FROM accounts.

To implement this if I write as below:

Query query = _em.createNamedQuery("Accounts.findAccountData");

Now if I do query.getSingleResult(); how do i retrieve both the values since the ejb finder returns either a single object or a collection?.

View Replies View Related

SOP Returning Memory Location?

Jul 15, 2014

For some reason my code returns the memory address of the array when its a print statement with a string, but it works fine when its in a separate print statement all by itself. Why? Do I need to create a toString method that converts a char array to a String to something? The reason why I ask that is becuase on Eclipse line 10 has a warning stating "Must explicitly convert char[] to a String".

public class Ex {
private String word;
public Ex(String word) {
this.word = word;
}
public char[] Display(){
char[] wordChars = this.word.toCharArray();
return wordChars;

[Code] .....

Result:

Hello world
The word is: [C@1db9742

I also tried this, knowing that it's a long shot, but that didnt do anything...

public String toString(){
Ex ex = new Ex(this.word);
char[] word = ex.Display();
String updated = word.toString();//counter intuitive?
return updated;
}

View Replies View Related

Returning Total From Loop

Jan 7, 2014

I have a for each loop that outputs the Mechanics salary for the first team in the arraylist, how can I add these figures together instead of having separate value for each?The code in my testing class to get the salary for mechanics:

Java Code: for (Mechanic str1: formula1.get(0)){
System.out.println(str1.getSalary());
} mh_sh_highlight_all('java');

How can I also get the salary for the driver 1 + 2 in the same team and add their salary to this? I have attaached an image showing the classes/fields created

View Replies View Related

Returning Int Array Using Generics

Apr 10, 2014

public int[] allIndicesOf(E itemSought) {
ArrayList<Integer> toUse = new ArrayList<>();
for (E anArray : container) {
if (anArray.equals(itemSought)) {
toUse.add(container.indexOf(itemSought));

[Code] ....

I have an array list of strings. I want to be able to return an array of integers telling me which indexes in the string array list contain the itemSought object.

View Replies View Related

Why Is Method Returning Null

Sep 25, 2014

I am working on a project which manages an airport's airplanes and flights based on user input. The method printFlights() - lines 133-134 - is returning null and I can't figure out why. The method is supposed to print information about each flight. The logic is identical to the printPlanes() method which is working successfully.

View Replies View Related







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