0

I have a string which is :

1|name|lastname|email|tel \n
2|name|lastname|email|tel \n

I know that I have to use a loop to display all lines but the problem is that in my assignment
I can't use arrays or other classes than String and System.

Also I would like to sort names by ascending order without using sort method or arrays.

Do I have to use compareTo method to compare two names ?

If that's the case, how do I use compareTo method to sort names.

For example, if compareTo returns 1, that means that the name is greater than the other one. In that case how do I manage the return to sort name properly in the string ?

Andreas
  • 154,647
  • 11
  • 152
  • 247
AlexCorDev
  • 55
  • 6
  • If you can't use `List` or array, does that mean you have to swap lines in-place in the string? E.g. if string is `C\nB\nA\n`, and you use [bubble sort](https://en.wikipedia.org/wiki/Bubble_sort), you need to build strings while sorting like this? `C\nB\nA\n` → `B\nC\nA\n` → `B\nA\nC\n` → `A\nB\nC\n` – Andreas Nov 15 '20 at 21:14
  • How would I use bubble sort to swap names if I can't use array ? – AlexCorDev Nov 15 '20 at 21:45

2 Answers2

2

To display all substrings of the string as in the example, you can just go through all characters one by one and store them in a string. Whenever you hit a delimiter (e.g. | or \n), print the last string.

Here's a thread on iterating through characters of a string in Java: What is the easiest/best/most correct way to iterate through the characters of a string in Java?

If you also need to sort the names in ascending order without an array, you will need to scan the input many times - sorting N strings takes at least N*log(N) steps. If this is a data structure question, PriorityQueue should do the trick for you - insert all substrings and then pop them out in a sorted fashion :)

StoneyKeys
  • 138
  • 8
1

building on the previous answer by StoneyKeys, since i do not have the privilege to comment, you can use a simple if statement that when the char is a delimiter, System.out.println() your previous scanned string. Then you can reset the string to an empty string in preparation for scanning the next string.

In java, there are special .equals() operators for strings and chars so when you won't be using == to check strings or char. Do look into that. To reset the value of string just assign it a new value. This is because the original variable points at a certain string ie "YHStan", by making it point at "", we are effectively "resetting" the string. ie scannedstr = "";

Please read the code and understand what each line of code does. The sample code and comments is only for your understanding, not a complete solution.

String str ="";
    String value = "YH\nStan";
    
    for (int i=0; i <value.length(); i++) {
        char c = value.charAt(i);
        String strc = Character.toString(c);
        //check if its a delimiter, using a string or char .equals(), if it is print it out and reset the string
        if (strc.equals("\n")) {
            System.out.println(str);
            str ="";
            continue;  // go to next iteration (you can instead use a else if to replace this)
        }
        //if its not delimiter append to str
        str = str +strc;
        //this is to show you how the str is changing as we go through the loop.
        System.out.println(str);
            
    }
    System.out.println(str);     //print out final string result

This gives a result of:

Y YH YH S St Sta Stan Stan

YHStan
  • 227
  • 3
  • 12