Write java array to file.txt in one line [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
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]+" ");

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

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.

reading next lines from a file after particular pattern [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 5 years ago.
Improve this question
I have to read a file if some pattern occurs that file i have to read next lines of the text file in java
while (sc1.hasNextLine()){
String a = sc1.nextLine();
String b = sc1.nextLine();
if(a.contains("pattern")){
//read next lines
}
}
To simply read the file to find the line you want, just use a loop until you reach the end or the pattern your want like this :
while(sc1.hastNextLine() && !sc.nextLine().contains("pattern")){
//skip
}
then you just have to check if there is still a line to read
if (sc1.hastNextLine()){
//read the line
}

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"

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