Return Integer That Is The Number Of Strings That Starts With S
Apr 29, 2014
Write a Java code of the method startsWithCount that takes an array of strings words and a String S. The method should return an integer that is the number of strings that starts with S.
For example if:words = { "All", "Arab", "size", "Almond", "Allowed", "here"} and S= "All", then the method should return 2
PHP Code:
public class StringwithCount {
public static void main (String[]args) {
String strings[] = { "All", "Arab", "size", "Almond", "Allowed", "here"};
String output= "";
for ( int i = 0; i < words.length; i++) {
if (words[i].startsWith("s"))
c + +;
}
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) { int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 }; int num = 3; int count = numbers(a, num); for (int i = 0; i < a.length; i++) {
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth. at the moment my code looks like this:
// File1: public class date { public int day; public int month; public int year; }
// File 2: public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
Recently we updated out database and we changed our tables' and columns' names, I'm developing an adf application with EJB3.0 the question is that the new column names start with a number (eg. 001U_ID), its a kind of encoding the names of the tables so no one can modify the values.
When I create a query on the table it is throwing an exception:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'U_ID'.
I think the problem is that the EJB3.0 doesn't recognize column names starting with a number!!! What can I do to solve this issue?
I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is even return 1, if it is not even return 0. How would you write that in a Code?
I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.
mlong should return an int depending on the X.moth.
At the moment my code looks like this:
// File1:
public class date { public int day; public int month; public int year; }
// File 2:
public class monthlength { public int mlong(date X) { int t; t = X.month; if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12) { return 31; } if(t == 4 || t == 6 || t == 9 || t == 11) {return 30;} } }
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
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 -
1. Create a program that will return the maximum and minimum numbers in the elements of ONE-dimensional integer array. Save it as MaxMin_OneDim.java
2. Create a program that will return the maximum and minimum numbers in the elements of each row in a TWO-dimensional integer array. Save it as MaxMin_TwoDim.java
3. Write a program PrintPattern which prompt a user to enter a number and prints the following patterns using nested loops (assumed user entered number is 8 output is:)
1 .... 87654321
12 .... 7654321
123 .... 654321
1234 .... 54321
12345 .... 4321
123456 .... 321
1234567 .... 21
12345678 .... 1
(Without the dots, i just put them to give spaces)
Write a function that accepts an array of non-negative integers and returns the second largest integer in the array.
Return -1 if there is no second largest.
The signature of the function is int f(int[ ] a)
Examples:
if the input array isreturn{1, 2, 3, 4}3{{4, 1, 2, 3}}3{1, 1, 2, 2}1{1, 1}-1{1}-1{}-1
In the signature what I understood is, I should write my function with the given signature,
The return type is "int"
method name is "f"
parameter is "a" right ?
Writing my doubts beside the particular line in the code
public static void main() // In the answer why they didn't use the class ?
In main method why they didn't use parameters ?(String[] args)
{ a1(new int[]{1, 2, 3, 4}); // what is "a1" here is it array name ? this line initializing the array ? a1(new int[]{4, 1, 2, 3}); a1(new int[]{1, 1, 2, 2}); a1(new int[]{1, 1}); a1(new int[]{1}); a1(new int[]{}); }
static int a1(int[] a) // what is "a" here parameter ? and "a1" is method name ? why they used the array name and method name same ?
{ int max1 = -1; int max2 = -1; for (int i=0; i<a.length; i++)
I am having a problem with the following code. It compiles and runs fine however my output is wrong.
public class SplitString { public static void main(String[] args) { String[] string1 = split("ab#12#453", "#"); String[] string2 = split("a?b?gf#e", "[?#]"); for (int i = 0; i < string1.length; i++) { System.out.print(string1[i] + ",");
[code]....
The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters.public static String[] split(String s, String regex)For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, b, ?, b, gf, #, and e in an array of String.
I am practicing some basic recursion and I was trying to solve this problem
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words:
sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n
For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should throw an IllegalArgumentException if passed a value less than 0.
This is my attempt to do it , however my output is always a 0.0 , and i do not understand why :
public static double sumTo(int n ){ if(n<0){ throw new IllegalArgumentException(); } else if (n==0){ return 0.0;
I've to return some value as a string and some as a int, how is this possible? Here's my code:
public class Card { public void start(){ String [] suit = {"Spade","Club","Diamond","Heart"}; int [] number = {1,2,3,4,5,6,7,8,9,10,11,12,13};} public String getColour(){ String [] suit = {"Spade","Club","Diamond","Heart"};
[Code] .....
So at the top, i've set it to return value as string because of the King Jack Queen and Ace, but i also have to return as numbers(int). And also i'm using a loop to read all the numbers, is there any other way?
One of our assignments this week was to make a program that when given a number it will return the biggest prime factor of that number. So for example if the program is given the number 15, the output is 3.
144 gives you 3 also. 17 gives you 17 21 gives you 7.
Anyways, after some time trying out various things and combining stuff i googled with my textbook I somehow stumbled upon a code that works. But I cannot for the life of me understand how it works. I think it has something to do with the inner loop and the outer loop. But I feel like i cant go on without understanding how it works.
Here is the code:
public class BiggestFactor { public static void main( String[] args ) { int N; N = StdIn.readInt(); int n = N;
public static void main(String args[]) { Scanner in=new Scanner(System.in); System.out.println("Enter the Total Number of Integers:"); int totalInteger=in.nextByte(); long largest=0; for(int i=1;i<=totalInteger;i++) { System.out.println("Enter Integer-"+i+":");
[code]...
I want to get Index of that particular number in which Value is largest.
I have been working on this for hours and cannot get it to work properly. The program is to count the number of characters contained in both strings and display the results. It is also to display the final statement, but only once. This version has a complete statement for each matching character.
import java.util.Scanner; public class CountMatches { public static void main(String[] args) { String firstStr = (""); String s1 = firstStr;
lines 7, 8, &12 "primes" are underline in red (prime cannot be resolved) is what pops up when i hover over the x's.
i don't get why that is.
package assignment7; public class Exercise3 { public static void main(String[] args) { Prime.setSize(1000); for (int p = Primes.next(); p < 30; p = Primes.next())
Lines 7, 8, &12 "primes" are underline in red (prime cannot be resolved) is what pops up when i hover over the x's.
I don't get why that is.
Java Code :
public class Exercise3 { public static void main(String[] args) { Prime.setSize(1000); for (int p = Primes.next(); p < 30; p = Primes.next()) { int n = (int)Math.round(Math.pow(2,p)) - 1; System.out.printf("%d 2^%d-1%d", p, p, n); if (Primes.isPrime(n))
Write a recursive Java method countDigitx that takes two integer numbers as input (a number n and digit x) and returns the number of times digits x occurs in number n:
For example if n=23242 and x=2 then the method should return 3 that is the number of times the number 2 found in n.
I am trying to get a user to enter an integer (registration number) between 500 and 5000. if it is below 500 and over 5000 the user should get a prompt "invalid number" and "non-numeric character". See my code below. it is not working when i enter invalid numbers.
import java.util.*; public class Infomation{ public static void main(String args []) { Scanner in = new Scanner(System.in); System.out.print("Student Number:"); int sn=in.nextInt();
Write a method named hopscotch that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that many hops. A "hop" is defined as the split into two numbers and then back together again into one.For example, hopscotch(3); should print:
I have to write a program that inputs a 5 digit integer from the keyboard and prints if the input number is a palindrome or not. I got it to work, but when my result prints it says "0 is a palindrome" or "0 is not a palindrome". How can I get rid of the 0 and replace it with the number input by the user?
import java.util.Scanner; public class Palindrome { public static void main(String[] args) { int number; int digit; int temp;