₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,701 members, 8,423,282 topics. Date: Tuesday, 09 June 2026 at 02:17 PM

Toggle theme

[Code Review Help] Explanation In Java Needed - Programming - Nairaland

Nairaland ForumScience/TechnologyProgramming[Code Review Help] Explanation In Java Needed (998 Views)

1 Reply (Go Down)

[Code Review Help] Explanation In Java Needed by paranorman(op): 1:21pm On Oct 11, 2019
A semi-colon delimited file exists, I just wanna output its content to the console after splitting it like this using PHP:

try {
if (($fp = fopen("output.txt", "r" ) ) ) {
while (!feof($fp)) {
$line = explode(";", fgets($fp, 1024)) ;
list($firstname, $lastname, $othername) = $line ;
print "First name: $firstname; Last name: $lastname; Other names: $othername <br /> " ;
}
fclose($fp);
} else {
throw new Exception("Error: File is not available" ) ;
}
} catch(Exception $e) {
//implement control logic here ;
}

The above code works as expected

Java's output of this code is funny:

import java.io.*;
import java.util.*;

public class Test2 {

public static void main(String[] args) {
try {
File file = new File("output.txt" ) ;
Scanner in = new Scanner(file) ;

int count = 1;
while (in.hasNextLine()) {
if (in.nextLine().contains( " ; " ) ) {
String names[] = in.nextLine().split( " ; " ) ;
System.out.printf("(%d) Your name is %s %s %s\n", count, names[0], names[1], names[2] ) ;
} else {
throw new Exception("String " + in.nextLine() + " does not contain ; " ) ;
}
count++;
}
} catch (Exception e) {
System.out.println(e.getMessage()) ;
}

}


}

output:
(1) Your name is jacob zuma zoomzoom
(2) Your name is abbey jackson teme


This java code works just fine, as expected:
import java.io.* ;
import java.util.* ;
import java.util.regex.* ;

public class Test {

public static void main(String[] args) {

try {
File file = new File("output.txt" );
Scanner in = new Scanner(file) ;

int count = 1;
while (in.hasNextLine()) {
String names[] = in.nextLine().split(Pattern.quote( " ; " ) ) ;
System.out.printf("(%d) First name: %s; last name: %s; other names: %s \n", count, names[0], names[1], names[2]) ;
count += 1;
}
in.close() ;

} catch (Exception e) {
System.out.print("An error has occured" ) ;
System.out.println(e.getMessage() ) ;
}

}

}

output:
(1) First name: john; last name: doe; other names: wick
(2) First name: jacob; last name: zuma; other names: zoomzoom
(3) First name: optimus; last name: primus; other names: autobots
(4) First name: abbey; last name: jackson; other names: teme



The text file is (output.txt)
john;doe;wick
jacob;zuma;zoomzoom
optimus;primus;autobots
abbey;jackson;teme


question:
Why is the console output of the first java program omitting the odd rows of the text file?!
NL smileys are getting in the way, so I had to space some characters oddly out.
Re: [Code Review Help] Explanation In Java Needed by spoilerx: 5:30pm On Oct 11, 2019
what is the content of the output.txt file
Re: [Code Review Help] Explanation In Java Needed by paranorman(op): 7:40pm On Oct 11, 2019
spoilerx:
what is the content of the output.txt file
I typed it out in my post above, in the "in text file is" session.
Re: [Code Review Help] Explanation In Java Needed by leoaso(m):
This is the part causing problems.
paranorman:

while (in.hasNextLine()) {
if (in.nextLine().contains( " ; " ) ) {
String names[] = in.nextLine().split( " ; " ) ;
After calling hasNextLine you should only call nextLine once. If you call it twice, it will read a line, and then read another line, which discards the first one you read.
The solution is to call nextLine once inside the loop and store it in a variable. Then use that variable instead of calling nextLine repeatedly before the loop body has repeated.
1 Reply

Need Help In Java[Code Review Help] Laravel Experts Please HELP!Question On Regular Expressions In Java234

I Want Teacher For C++Quarantine Challenge {40 Days Challenge }PHP Password_verify() Function Fails To Work. Help!