how to split first and last name in string array in java










0















I have an array contains 15 full names, how do I break it into 2 arrays(first and last name) below is the method for my full name array



public static void readData(String file) throws FileNotFoundException 
x = new Scanner(new File(file));
//count number of names in the array
int n = 0;
while(x.hasNextLine())
n++;
x.nextLine();

//open another scanner to avoid null
Scanner x1 = new Scanner(new File(file));
name = new String[n];
//get the array and print
for(int i = 0; i < name.length; i++ )
name[i] = x1.nextLine();
System.out.println(Arrays.toString(name));










share|improve this question






















  • Create two arrays. When you read the String from the file, split on the delimiter, I assume which is a "space", then add each element to the corresponding arrays

    – MadProgrammer
    Nov 15 '18 at 4:11







  • 2





    Just out of curiosity, why are you opening "another scanner to avoid null"? Seems entirely unnecessary.

    – Carcigenicate
    Nov 15 '18 at 4:11






  • 1





    Why are you reading this into an array at all? Just split and print in one pass. Also you leak not one but two file handles!

    – Elliott Frisch
    Nov 15 '18 at 4:11












  • @Carcigenicate Looking at the code, they are trying to determine the number of lines in the file ahead of time ... They'd need to reset the Scanner to the start of the file, which is why I assume they're using two scanners, but not closing either ... and generally making a mess (and I doubt it would be a NPE)

    – MadProgrammer
    Nov 15 '18 at 4:14
















0















I have an array contains 15 full names, how do I break it into 2 arrays(first and last name) below is the method for my full name array



public static void readData(String file) throws FileNotFoundException 
x = new Scanner(new File(file));
//count number of names in the array
int n = 0;
while(x.hasNextLine())
n++;
x.nextLine();

//open another scanner to avoid null
Scanner x1 = new Scanner(new File(file));
name = new String[n];
//get the array and print
for(int i = 0; i < name.length; i++ )
name[i] = x1.nextLine();
System.out.println(Arrays.toString(name));










share|improve this question






















  • Create two arrays. When you read the String from the file, split on the delimiter, I assume which is a "space", then add each element to the corresponding arrays

    – MadProgrammer
    Nov 15 '18 at 4:11







  • 2





    Just out of curiosity, why are you opening "another scanner to avoid null"? Seems entirely unnecessary.

    – Carcigenicate
    Nov 15 '18 at 4:11






  • 1





    Why are you reading this into an array at all? Just split and print in one pass. Also you leak not one but two file handles!

    – Elliott Frisch
    Nov 15 '18 at 4:11












  • @Carcigenicate Looking at the code, they are trying to determine the number of lines in the file ahead of time ... They'd need to reset the Scanner to the start of the file, which is why I assume they're using two scanners, but not closing either ... and generally making a mess (and I doubt it would be a NPE)

    – MadProgrammer
    Nov 15 '18 at 4:14














0












0








0








I have an array contains 15 full names, how do I break it into 2 arrays(first and last name) below is the method for my full name array



public static void readData(String file) throws FileNotFoundException 
x = new Scanner(new File(file));
//count number of names in the array
int n = 0;
while(x.hasNextLine())
n++;
x.nextLine();

//open another scanner to avoid null
Scanner x1 = new Scanner(new File(file));
name = new String[n];
//get the array and print
for(int i = 0; i < name.length; i++ )
name[i] = x1.nextLine();
System.out.println(Arrays.toString(name));










share|improve this question














I have an array contains 15 full names, how do I break it into 2 arrays(first and last name) below is the method for my full name array



public static void readData(String file) throws FileNotFoundException 
x = new Scanner(new File(file));
//count number of names in the array
int n = 0;
while(x.hasNextLine())
n++;
x.nextLine();

//open another scanner to avoid null
Scanner x1 = new Scanner(new File(file));
name = new String[n];
//get the array and print
for(int i = 0; i < name.length; i++ )
name[i] = x1.nextLine();
System.out.println(Arrays.toString(name));







java arrays string split






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 4:08









James PJames P

102




102












  • Create two arrays. When you read the String from the file, split on the delimiter, I assume which is a "space", then add each element to the corresponding arrays

    – MadProgrammer
    Nov 15 '18 at 4:11







  • 2





    Just out of curiosity, why are you opening "another scanner to avoid null"? Seems entirely unnecessary.

    – Carcigenicate
    Nov 15 '18 at 4:11






  • 1





    Why are you reading this into an array at all? Just split and print in one pass. Also you leak not one but two file handles!

    – Elliott Frisch
    Nov 15 '18 at 4:11












  • @Carcigenicate Looking at the code, they are trying to determine the number of lines in the file ahead of time ... They'd need to reset the Scanner to the start of the file, which is why I assume they're using two scanners, but not closing either ... and generally making a mess (and I doubt it would be a NPE)

    – MadProgrammer
    Nov 15 '18 at 4:14


















  • Create two arrays. When you read the String from the file, split on the delimiter, I assume which is a "space", then add each element to the corresponding arrays

    – MadProgrammer
    Nov 15 '18 at 4:11







  • 2





    Just out of curiosity, why are you opening "another scanner to avoid null"? Seems entirely unnecessary.

    – Carcigenicate
    Nov 15 '18 at 4:11






  • 1





    Why are you reading this into an array at all? Just split and print in one pass. Also you leak not one but two file handles!

    – Elliott Frisch
    Nov 15 '18 at 4:11












  • @Carcigenicate Looking at the code, they are trying to determine the number of lines in the file ahead of time ... They'd need to reset the Scanner to the start of the file, which is why I assume they're using two scanners, but not closing either ... and generally making a mess (and I doubt it would be a NPE)

    – MadProgrammer
    Nov 15 '18 at 4:14

















Create two arrays. When you read the String from the file, split on the delimiter, I assume which is a "space", then add each element to the corresponding arrays

– MadProgrammer
Nov 15 '18 at 4:11






Create two arrays. When you read the String from the file, split on the delimiter, I assume which is a "space", then add each element to the corresponding arrays

– MadProgrammer
Nov 15 '18 at 4:11





2




2





Just out of curiosity, why are you opening "another scanner to avoid null"? Seems entirely unnecessary.

– Carcigenicate
Nov 15 '18 at 4:11





Just out of curiosity, why are you opening "another scanner to avoid null"? Seems entirely unnecessary.

– Carcigenicate
Nov 15 '18 at 4:11




1




1





Why are you reading this into an array at all? Just split and print in one pass. Also you leak not one but two file handles!

– Elliott Frisch
Nov 15 '18 at 4:11






Why are you reading this into an array at all? Just split and print in one pass. Also you leak not one but two file handles!

– Elliott Frisch
Nov 15 '18 at 4:11














@Carcigenicate Looking at the code, they are trying to determine the number of lines in the file ahead of time ... They'd need to reset the Scanner to the start of the file, which is why I assume they're using two scanners, but not closing either ... and generally making a mess (and I doubt it would be a NPE)

– MadProgrammer
Nov 15 '18 at 4:14






@Carcigenicate Looking at the code, they are trying to determine the number of lines in the file ahead of time ... They'd need to reset the Scanner to the start of the file, which is why I assume they're using two scanners, but not closing either ... and generally making a mess (and I doubt it would be a NPE)

– MadProgrammer
Nov 15 '18 at 4:14













1 Answer
1






active

oldest

votes


















0














File contains 15 full names like this:



Max Frei
Stephen King
Agatha Christie


How to read:



final int total = 15;
String firstNames = new String[total];
String lastNames = new String[total];

try (Scanner scan = new Scanner(new File("file")))
for (int i = 0; i < total; i++)
firstNames[i] = scan.next();
lastNames[i] = scan.next();



// firstNames: Max, Stephen, Agatha
// lastNames: Frei, King, Christie





share|improve this answer























  • I also want to make sure that I understood this correctly. The key is to use scan,next() instead of nextLine(). When you do scan.next() it will assign the first name to firstName, then the next one to lastName, then repeat the cycle in for-loop, so my String name is actually irrelevant in this case. Is that correct?

    – James P
    Nov 15 '18 at 5:33











  • nextLine() to read whole line, next() to read until next delimiter

    – oleg.cherednik
    Nov 15 '18 at 5:40










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312296%2fhow-to-split-first-and-last-name-in-string-array-in-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














File contains 15 full names like this:



Max Frei
Stephen King
Agatha Christie


How to read:



final int total = 15;
String firstNames = new String[total];
String lastNames = new String[total];

try (Scanner scan = new Scanner(new File("file")))
for (int i = 0; i < total; i++)
firstNames[i] = scan.next();
lastNames[i] = scan.next();



// firstNames: Max, Stephen, Agatha
// lastNames: Frei, King, Christie





share|improve this answer























  • I also want to make sure that I understood this correctly. The key is to use scan,next() instead of nextLine(). When you do scan.next() it will assign the first name to firstName, then the next one to lastName, then repeat the cycle in for-loop, so my String name is actually irrelevant in this case. Is that correct?

    – James P
    Nov 15 '18 at 5:33











  • nextLine() to read whole line, next() to read until next delimiter

    – oleg.cherednik
    Nov 15 '18 at 5:40















0














File contains 15 full names like this:



Max Frei
Stephen King
Agatha Christie


How to read:



final int total = 15;
String firstNames = new String[total];
String lastNames = new String[total];

try (Scanner scan = new Scanner(new File("file")))
for (int i = 0; i < total; i++)
firstNames[i] = scan.next();
lastNames[i] = scan.next();



// firstNames: Max, Stephen, Agatha
// lastNames: Frei, King, Christie





share|improve this answer























  • I also want to make sure that I understood this correctly. The key is to use scan,next() instead of nextLine(). When you do scan.next() it will assign the first name to firstName, then the next one to lastName, then repeat the cycle in for-loop, so my String name is actually irrelevant in this case. Is that correct?

    – James P
    Nov 15 '18 at 5:33











  • nextLine() to read whole line, next() to read until next delimiter

    – oleg.cherednik
    Nov 15 '18 at 5:40













0












0








0







File contains 15 full names like this:



Max Frei
Stephen King
Agatha Christie


How to read:



final int total = 15;
String firstNames = new String[total];
String lastNames = new String[total];

try (Scanner scan = new Scanner(new File("file")))
for (int i = 0; i < total; i++)
firstNames[i] = scan.next();
lastNames[i] = scan.next();



// firstNames: Max, Stephen, Agatha
// lastNames: Frei, King, Christie





share|improve this answer













File contains 15 full names like this:



Max Frei
Stephen King
Agatha Christie


How to read:



final int total = 15;
String firstNames = new String[total];
String lastNames = new String[total];

try (Scanner scan = new Scanner(new File("file")))
for (int i = 0; i < total; i++)
firstNames[i] = scan.next();
lastNames[i] = scan.next();



// firstNames: Max, Stephen, Agatha
// lastNames: Frei, King, Christie






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 4:55









oleg.cherednikoleg.cherednik

7,10021119




7,10021119












  • I also want to make sure that I understood this correctly. The key is to use scan,next() instead of nextLine(). When you do scan.next() it will assign the first name to firstName, then the next one to lastName, then repeat the cycle in for-loop, so my String name is actually irrelevant in this case. Is that correct?

    – James P
    Nov 15 '18 at 5:33











  • nextLine() to read whole line, next() to read until next delimiter

    – oleg.cherednik
    Nov 15 '18 at 5:40

















  • I also want to make sure that I understood this correctly. The key is to use scan,next() instead of nextLine(). When you do scan.next() it will assign the first name to firstName, then the next one to lastName, then repeat the cycle in for-loop, so my String name is actually irrelevant in this case. Is that correct?

    – James P
    Nov 15 '18 at 5:33











  • nextLine() to read whole line, next() to read until next delimiter

    – oleg.cherednik
    Nov 15 '18 at 5:40
















I also want to make sure that I understood this correctly. The key is to use scan,next() instead of nextLine(). When you do scan.next() it will assign the first name to firstName, then the next one to lastName, then repeat the cycle in for-loop, so my String name is actually irrelevant in this case. Is that correct?

– James P
Nov 15 '18 at 5:33





I also want to make sure that I understood this correctly. The key is to use scan,next() instead of nextLine(). When you do scan.next() it will assign the first name to firstName, then the next one to lastName, then repeat the cycle in for-loop, so my String name is actually irrelevant in this case. Is that correct?

– James P
Nov 15 '18 at 5:33













nextLine() to read whole line, next() to read until next delimiter

– oleg.cherednik
Nov 15 '18 at 5:40





nextLine() to read whole line, next() to read until next delimiter

– oleg.cherednik
Nov 15 '18 at 5:40



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312296%2fhow-to-split-first-and-last-name-in-string-array-in-java%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto