Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split long sting in new lines and check each line first word in java

I have a string that I split based on delimiter on new lines. I’m wondering now how to check the first word index[0] what word is but can’t find a way to actually go trough the elements and check.

May be my approach is totally wrong.

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String line = scanner.nextLine();
    String[] stringArr = line.split(">>");
  int ask = 0;
    for (int i = 0; i < stringArr.length; i++) {

        if (stringArr[0].equals("radio")) {
            ask = 10;
        } else if (Objects.equals(stringArr[0], "tv")) {
            ask = 15;
        } else {
            System.out.println("Invalid media.");
        }

    }
  System.out.println(ask);
}  

Then when I input radio 3 7210>>tv 4 2345>>radio 9 31000>>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The output should be:

10
15
10

Instead – got nothing. Empty line and the program ends.

>Solution :

Is something like this what you want:

Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String[] stringArr = line.split(">>");
for (int i = 0; i < stringArr.length; i++) {
  int ask = 0;
  String[] words = stringArr[i].split(" ");
  if (words[0].equals("radio")) {
      ask = 10;
      System.out.println(ask);
  } else if (words[0].equals("tv")) {
      ask = 15;
      System.out.println(ask);
  } else {
      System.out.println("Invalid media.");
  }
}

Input:

radio 3 7210>>tv 4 2345>>radio 9 31000>>

Output:

10
15
10

First of all, I defined the scanner, not sure if you did that but pretty sure you did.

The elements of stringArr will include the random numbers between each ">>". That means, in each element, we should create a new list split by " " to isolate the "radio" and "tv" as the first element.

Additionally, I just rewrote the else-if statement that checks if the first word of the phrases separated by ">>" is "tv" by using the .equals() method as your original if statement did.

Finally, since you are printing out a number EACH time the code encounters a ">>", we should print out ask inside of the for loop.

EDIT:

Moved the System.out.println(ask) inside of the if and else-if statements so it will only run with valid media.

Other than that your code worked perfectly :> , let me know if you have any further questions or clarifications!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading