#How do i remove this last line

1 messages ยท Page 1 of 1 (latest)

steady junco
#

Hello, I have pretty much completed this assignment and coded everything right, just for the completion it cant end on a new empty line and I can not figure out how to not have it.

My code:
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class FunWithFiles {

/**
* Displays the file given by fname to the screen
*
* @param fname file to be displayed
*/
public static void displayFile(String fileName) {
try {
File bFile = new File(fileName);
Scanner fscnr = new Scanner(bFile);
while(fscnr.hasNextLine()) {
if(fscnr.hasNextLine()) {
System.out.println(fscnr.nextLine());
}
}
fscnr.close();
} catch (FileNotFoundException e) {
System.out.println("ERROR - File " + fileName + " not found!");

    }

}

public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);
  System.out.print("Enter a filename: ");
  String fileName = scnr.nextLine();
  displayFile(fileName);

  scnr.close();
}

}

low swiftBOT
#

<@&987246399047479336> please have a look, thanks.

low swiftBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

fleet swan
#

i don't get what is your problem exactly

#

would you like to give us a friendly description about your question or issue please ?

steady junco
#

so like in this image my output is the first part but I need it to look like the second part and the only thing that is off is that its outputting an extra line and I can not figure out how to get it to not do that

fleet swan
#

first of all your code is ugly

#

second, you dnt need that if inside the while ()

#

maybe that check is making the issue

steady junco
#

Oh i know i dont need that If, i was trying to fix the problem with that lol

#

ill send updated code

fleet swan
#

try to do

try {
    File bFile = new File(fileName);

    Scanner fscnr = new Scanner(bFile);

    while (fscnr.hasNextLine()) {
        System.out.println(fscnr.nextLine());
    }

    fscnr.close();

} catch (FileNotFoundException e) {
    System.out.println("ERROR - File " + fileName + " not found!");
}
steady junco
#

Yeah i got that

fleet swan
steady junco
#

No its still doing it

fleet swan
steady junco
#

Yes

fleet swan
#

alright, in that case

#

try to check if the line is NOT empty

mint stream
#

..

#

println prints a newline character at the end

#

that includes the last line as well

steady junco
#

So how would I do it without println?

fleet swan
#
while (fscnr.hasNextLine()) {
  String line = fscnr.nextLine();
  if(!line.empty){
    println()
  }
}
mint stream
#

uh no

#

that's not the issue @fleet swan

#

the last line isn't empty

#

it has text, but println prints a newline character after it

#

So basically, you gotta somehow use println for every line except the last

steady junco
#

Yeah

fleet swan
#

hmm in that case, maybe he should switch to for

#

but i didn't use it with scanner

mint stream
#

you could use a for loop and check for the last

#

uhh looking for other options rq

steady junco
#

thank you guys ๐Ÿ˜…

mint stream
#

oh a simpler option is using hasNextLine again

#

if you use hasNextLine after the nextLine statement, you will know if the loop will run again

#

hasNextLine will be false if this was the last iteration, since that's the condition of the while loop

#

so- use println if hasNextLine is true, and use print if it's false

steady junco
#

Okay ill try it

#

thank you

mint stream
#

From to this stackoverflow answer

steady junco
#

It worked thank you!

mint stream
#

๐ŸŽ‰

#

Have fun coding ^-^