Character shifts [Java] [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to know the logic behind this java question:
Identify the character shifts to for the following entered text to
become "aptech" (only Aptech will be entered with character shifts).
Input: "bqufdi"
Output: "Character Shift = 1"
Does anyone help me out on this java question, please? Thanks.

String s = "aptech";
String in = "bqufdi";
You could write a simple for statement like
for(int i = 0; i < in.length(); ++i)
System.out.print(in.charAt(i) - s.charAt(i)); // shift for each letter
That outputs 111111 and means that all letters in "bqufdi" are shifted to one position on the right.

Related

Permutations of array with at minimum 1 element [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 17 days ago.
Improve this question
I am trying to produce all permutations of an array of size N (i.e. N = 9) with possible elements Z (i.e. Z = [0,1,2,3,4])
Duplicates are allowed, but there needs to be at least one. I am able to write the algorithm to go through all possibilities but the minimum of at least one of each of Z is the part I'm having trouble with.
Looking to do this algorithm in java. Can someone help?

Find all the occurences of an array that contain a substring in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Input:
[the, and, land, wander, dreams]
Substring: "and"
Output: 3
I need to find all the occurrences of an array that contain a certain substring but all I found was the word itself. For example, I want it to count the "and" in the word "land" and "wander" as well. I don't know how to do that. Please help!
EDIT: Updated the code.
What about:
int cnt=0;
String[] input = {"the", "and", "land", "wander", "dreams"};
for (String str : input){
if (str.contains("and")){
cnt++;
}
}
System.out.println(cnt);
This code should work for you. But keep in mind following - Exbow is right, next time your question might be considered as useless and will be closed.

Java Printf Help in formating [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
void catalog()
{
System.out.printf("\n%-5s%-5s%-15s%-15s%-6s%-15s%-5d\n",
"Sno.","B.No.","BOOK-NAME","AUTHOR-NAME","COPIES","PUBLISHER","PRICE");
for(int i=1;i<53;i++)
System.out.print("-");
System.out.println();
for(int i=0;i<nob;i++)
System.out.printf("%-5d%-5d%-15s%-15s%-5d%-10s%-10d\n",
(i+1),bno[i],bname[i],author[i],availcopies[i],publisher[i],price[i]);
for(int i=1;i<53;i++)
System.out.print("-");
System.out.println();
}
i have this question with using printf in java, so this is the error i get with printf
java.util.MissingFormatArgumentException: Format specifier '%-15s'
at java.util.Formatter.format(Formatter.java:2519)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Library.catalog(LibrarySystem.java:162)
at LibrarySystem.main(LibrarySystem.java:218)
Change the line
System.out.printf("\n%-5s%-5s%-15s%-15s%-6s%-15s%-5d\n",
"Sno.","B.No.","BOOK-NAME","AUTHOR-NAME","COPIES","PUBLISHER","PRICE");
to
System.out.printf("\n%-5s%-5s%-15s%-15s%-6s%-15s%-5s\n",
"Sno.","B.No.","BOOK-NAME","AUTHOR-NAME","COPIES","PUBLISHER","PRICE");
because the last part %-5d\n" will try to get int value but you want to print a string i.e. "PRICE"
Rest is fine.
Simple mistake
This
System.out.printf("\n%-5s%-5s%-15s%-15s%-6s%-15s%-5d\n",
"Sno.","B.No.","BOOK-NAME","AUTHOR-NAME","COPIES","PUBLISHER","PRICE");
will not work because the last is a number and you pass it the string "PRICE".
You should use different format string for the columns names.
Column names : "\n%-5s%-5s%-15s%-15s%-6s%-15s%-5s\n"

Remove all chars in java string, except '0' and '1' [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I remove all characters being neither '0' nor '1' in a string, so that the final string consists of only 0s and 1s? I'm using Java 8.
My solution with a for loop (I think it's not the most efficient way):
String before = "012940124810";
String after = new String();
for(int i = 0; i < before.length(); i++) {
if(before.charAt(i) == '0' || before.charAt(i) == '1') {
after += before.charAt(i);
}
}
you can use regex and replaceAll method
String out = str.replaceAll("[^01]", "");
To make this you just take a index variable starting from 0 and delete character by charcter if it is not 0 or 1 if you found just move index to next location without deleting it. If you need more help attach some code so that i will tell you perfect solution

Changing the Lengths of Words in a String - Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am currently writing a game in Java, where words in a string will have to be all changed to equal 5 characters a word.
eg. I am writing in Java
Iamwr iting inJav a
I wonder if anyone knows how I would do this?
First remove spaces between words.
Then split that resulting String to length with 5.
Add a space after every 5 character.

Categories

Resources