-1

I read the information in a .txt file and now I would like to store the lines of information from the text into a String Array or a variable.

The information in the .txt file is as given:

Onesimus, Andrea
BAYV
Twendi, Meghan
RHHS
Threesten, Heidi
MDHS

I want to store BAYV, RHHS, MDHS into a different array from the names.

import java.io.File;
import java.util.Scanner;

class testing2 {
 public static void main(String [] args) throws Exception {


File Bayviewcamp = new File ("H:\\Profile\\Desktop\\ICS3U\\Bayviewland Camp\\Studentinfo.txt");
Scanner scanner = new Scanner (Bayviewcamp);

while (scanner.hasNextLine())
  System.out.println(scanner.nextLine());
halfer
  • 19,824
  • 17
  • 99
  • 186
Christian
  • 9
  • 1
  • 3
    What have you tried so far? Also, your requirements are not very clear, do you want to store only the uppercase words? – Gabriel Prá Dec 13 '18 at 14:57
  • 2
    Please do not use the word *urgent*. – Jens Dec 13 '18 at 14:57
  • @GabrielPrá No, I want to store the names (e.g Onesimus, Andrea) into an array and the upper case letters into another array – Christian Dec 13 '18 at 15:00
  • They are always in that format : Names and next line is value? – Rafał Sokalski Dec 13 '18 at 15:02
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 13 '18 at 19:35

2 Answers2

0

Below code has few restrictions like:

  1. There must be format that you said (name and next line value)
  2. Array size is 100 by default but you can change as you want
  3. By name I mean one line: (Onesimus, Andrea) it's under first index in names array.

    private static final int ARRAY_LENGTH = 100;
    
    public static void main(String[] args) throws FileNotFoundException {
    
      boolean isValue = false;
      File txt = new File("file.txt");
      Scanner scanner = new Scanner(txt);
    
      String[] names = new String[ARRAY_LENGTH];
      String[] values = new String[ARRAY_LENGTH];
      int lineNumber = 0;
      while (scanner.hasNextLine()) {
        if (isValue) {
            values[lineNumber / 2] = scanner.nextLine();
        } else {
            names[lineNumber / 2] = scanner.nextLine();
        }
        isValue = !isValue;
        lineNumber++;
      }
      for (int i = 0; i < ARRAY_LENGTH; i++) {
        System.out.println(names[i]);
        System.out.println(values[i]);
      }
    }
    

Below code return separated names:

private static final int ARRAY_LENGTH = 100;

public static void main(String[] args) throws FileNotFoundException {
    boolean isValue = false;
    File txt = new File("file.txt");
    Scanner scanner = new Scanner(txt);

    String[] names = new String[ARRAY_LENGTH];
    String[] values = new String[ARRAY_LENGTH];
    int namesNumber = 0;
    int valuesNumber = 0;
    while (scanner.hasNextLine()) {
        if (!isValue) {
            String tempArrayNames[] = scanner.nextLine().split(",");
            values[valuesNumber++] = tempArrayNames[0].trim();
            values[valuesNumber++] = tempArrayNames[1].trim();
        } else {
            names[namesNumber++] = scanner.nextLine();
        }
        isValue = !isValue;
    }
}
Rafał Sokalski
  • 1,817
  • 2
  • 17
  • 29
0

Check whether names matches with the regex "[A-Z]+"

        List<String> upperCaseList = new ArrayList<>();
        List<String> lowerCaseList = new ArrayList<>();

        while (scanner.hasNextLine()) {
              String[] names = scanner.nextLine().split(",");
              for(String name:names) {
                  if(name.matches("[A-Z]+")) {
                      upperCaseList.add(name);
                  }
                  else {
                      lowerCaseList.add(name);
                  }
              }
        }

As per your example, some of the names has leading spaces. you may have to trim those spaces before you compare with the regex

              for(String name:names) {
                  if(name.trim().matches("[A-Z]+")) {
                      upperCaseList.add(name.trim());
                  }
                  else {
                      lowerCaseList.add(name.trim());
                  }
              }
Naghaveer R
  • 2,890
  • 4
  • 30
  • 52