Java Printf Help in formating [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 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"

Related

last two occurance of semicolon(;) in given String using 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
How do I get the after last two semicolon's occurrences of a string ?
Example:
Mobiles;Students;Test;Yes;1234
The output should be
Yes;1234
Using a regex replacement, we can try:
String input = "Mobiles;Students;Test;Yes;1234";
String output = input.replaceAll("^.*;([^;]+;[^;]+)$", "$1");
System.out.println(output); // Yes;1234

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.

Need to add a default value in a string inside a method 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 3 years ago.
Improve this question
Suppose we have a string BE231231.
I need an output like this : BE0231231
need to append zero at a this fixed location in java inside a method
public String appendAtIndex(String base, String toAppend, int index){
return base.substring(0,index) + toAppend + base.substring(index);
}
You can implement the logic for restrictions on index accordingly so that base.substring does not give errors.

Character shifts [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 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.

Write java array to file.txt in one line [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
What should I change in my code to write my array into text file in a single line instead of a different line per vale of the array?
PrintWriter zapis = new PrintWriter("wyniki.txt");
for (int g=1; g<801; g++)
{
zapis.println(g+":"+wynik[g]+" ");
}
zapis.close();
As the documentation states, println "Prints a String and then terminates the line."
If you don't want to terminate the line, use print instead.
Change the line inside for loop like this :
zapis.print(g+":"+wynik[g]+" ");

Categories

Resources