Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,499 members, 7,823,167 topics. Date: Friday, 10 May 2024 at 05:32 AM

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

Nairaland Forum / Science/Technology / Programming / [Code Review Help] Explanation In Java Needed (900 Views)

[Code Review Help] Laravel Experts Please HELP! / Question On Regular Expressions In Java / Network Programming In Java (UDP And TCP) (2) (3) (4)

(1) (Reply) (Go Down)

[Code Review Help] Explanation In Java Needed by paranorman(m): 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(m): 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): 12:28pm On Oct 12, 2019
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.

3 Likes 2 Shares

(1) (Reply)

A New Open Box Allienware Classic System A.k.a D Beast / Hire An Experienced Full Stack Developer / School Result Checking Portal Script

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 16
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.