STIW3054-A191 / Main-Issues

3 stars 2 forks source link

Convert Strings #5

Open zhamri opened 4 years ago

zhamri commented 4 years ago

Instruction

Write a Java program using TWO (2) threads. The first thread will read the strings below:

String1 = "universiti utara malsysia" String2 = "satu dua tiga"

String3 = "satu dua tiga empat" String4 = "one two three four"

The second thread will display the output as shown below:

Example of output:

universiti satu utara dua malaysia tiga

satu one dua two tiga three empat four

Submission

  1. Java source code
  2. Screenshot of output
TanChengYi commented 4 years ago
  1. Java source code
    
    import java.util.Scanner;

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

    readThread t1=new readThread();

    t1.start();
    t1.join();

    outThread t2=new outThread(t1.getStr1(),t1.getStr2(),t1.getStr3(),t1.getStr4());
    t2.start();

}

}

class readThread extends Thread{ Scanner sc =new Scanner(System.in); String str1; String str2; String str3; String str4;

@Override
public void run() {
    System.out.print("Enter String 1: ");
    str1 = sc.nextLine();
    System.out.print("Enter String 2: ");
    str2 = sc.nextLine();
    System.out.print("Enter String 3: ");
    str3 = sc.nextLine();
    System.out.print("Enter String 4: ");
    str4 = sc.nextLine();
}

public String getStr1(){
    return str1;
}
public String getStr2(){
    return str2;
}
public String getStr3(){
    return str3;
}
public String getStr4(){
    return str4;
}

} class outThread extends Thread{ String str1; String str2; String str3; String str4; outThread(String s1,String s2,String s3,String s4){ this.str1=s1; this.str2=s2; this.str3=s3; this.str4=s4; } @Override public void run() { String[] arrOfStr1 = str1.split("\s"); String[] arrOfStr2 = str2.split("\s"); String[] arrOfStr3 = str3.split("\s"); String[] arrOfStr4 = str4.split("\s"); for(int i=0;i<arrOfStr1.length;i++){ System.out.println(arrOfStr1[i]+" "+arrOfStr2[i]); } System.out.println(); for(int j=0;j<arrOfStr3.length;j++){ System.out.println(arrOfStr3[j]+" "+arrOfStr4[j]); } } }


2.Screenshot of output
![image](https://user-images.githubusercontent.com/46247836/66022337-40c6df00-e520-11e9-8f54-ba111aa251d5.png)
weiditan commented 4 years ago

TAN WEI DI 259296

import java.util.stream.Stream;

public class test2 {
    public static void main(String[] args) {

        String text1 = "universiti utara malsysia";
        String text2 = "satu dua tiga";
        String text3 = "satu dua tiga empat";
        String text4 = "one two three four";

        // Create Thread 1
        Thread thread1 = new Thread(() -> {

            String[] splitted1 = text1.split(" ");

            for(int j = 0; j< Stream.of(splitted1).count(); j++) {
                System.out.print(splitted1[j]+" ");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread.yield();
            }

            System.out.print("\n");

            String[] splitted3 = text3.split(" ");

            for(int j = 0; j< Stream.of(splitted3).count(); j++) {
                System.out.print(splitted3[j]+" ");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread.yield();
            }

        });

        // Create Thread 2
        Thread thread2 = new Thread(() -> {

            String[] splitted2 = text2.split(" ");

            for(int j = 0; j< Stream.of(splitted2).count(); j++) {
                System.out.println(splitted2[j]);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread.yield();
            }

            String[] splitted4 = text4.split(" ");

            for(int j = 0; j< Stream.of(splitted4).count(); j++) {
                System.out.println(splitted4[j]);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread.yield();
            }
        });

        thread1.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread2.start();
    }
}

Capture

WwLuo-1024 commented 4 years ago
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Thread04;
import java.util.Scanner;
/**
 *
 * @author WANG LUO
 */
public class read extends Thread{
    String first;
    String second;
    String third;
    String fourth;
    Scanner sc = new Scanner(System.in);
    @Override
    public void run(){
        System.out.print("String1 = ");
        first = sc.nextLine();
        System.out.print("String2 = ");
        second = sc.nextLine();
        System.out.print("String3 = ");
        third = sc.nextLine();
        System.out.print("String4 = ");
        fourth = sc.nextLine();
    }
    public String getfirst(){
        return first;
    }
    public String getsecond(){
        return second;
    }
    public String getthird(){
        return third;
    }
    public String getfourth(){
        return fourth;
    }
}

class print extends Thread{
    String first;
    String second;
    String third;
    String fourth;
    print(String fir,String sec,String thi,String fou){
        this.first = fir;
        this.second = sec;
        this.third = thi;
        this.fourth = fou;
    }
    public void printFirst(){
        String[] list01 = first.split(" ");
        String[] list02 = second.split(" ");
        for(int i = 0;i<list01.length;i++){
            System.out.print(list01[i]+" ");
            System.out.println(list02[i]);
        }
        String[] list03 = third.split(" ");
        String[] list04 = fourth.split(" ");
        for(int a = 0;a<list03.length;a++){
            System.out.print(list03[a]+" ");
            System.out.println(list04[a]);
    }
    }

    @Override
    public void run(){
        synchronized(this){
            try{
                printFirst();
                Thread.sleep(1000);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

class testApp{
    public static void main(String[] args) throws InterruptedException{
        read re = new read();
        Thread t1 = new Thread(re,"FirstThread");
        t1.start();
        t1.join();
        print pr = new print(re.getfirst(),re.getsecond(),re.getthird(),re.getfourth());
        Thread t2 = new Thread(pr,"SecondThread");
        t2.start();    
    }
}

微信截图_20191002155732

sohcheefung commented 4 years ago
package threads;

//259521 soh chee fung
class string extends Thread{
        String String1, String3;
        string(String String1, String String3){
         this.String1 = String1;
         this.String3 = String3;
        }
        public void run(){
           String[] split1 = String1.split(" ");

            for(int i = 0; i < split1.length; i++){

               System.out.print(split1[i]);
             try{

                Thread.sleep(1000);

            }catch(Exception e){}

                Thread.yield();
            }

             System.out.println(" ");

           String[] split3 = String3.split(" ");

            for(int i = 0; i < split3.length; i++){

               System.out.print(split3[i]);

              try{

                 Thread.sleep(1000);

             }catch(Exception e){}

                 Thread.yield();
           } 

      }
}

class character extends Thread{
     String String2, String4;
     character(String String2, String String4){
      this.String2 = String2;
      this.String4 = String4;
        }
        public void run(){

           String[] split2 = String2.split(" ");

           for(int i = 0; i < split2.length; i++){

             System.out.println(split2[i]);

            try{

                Thread.sleep(1000);

            }catch(Exception e){}

                Thread.yield();
            }

           String[] split4 = String4.split(" ");

           for(int i = 0; i < split4.length; i++){

             System.out.println(split4[i]);

             try{

                 Thread.sleep(1000);

            }catch(Exception e){}

                Thread.yield();
            }  

      }

}

public class Convert {

    public static void main(String[] args) throws InterruptedException{
    String String1 = "universiti utara malaysia";
    String String2 = "satu dua tiga";
    String String3 = "satu dua tiga empat";
    String String4 = "one two three four";

    string sintok = new string(String1, String3);
    character kedah = new character(String2, String4);

    sintok.setPriority(1);
    kedah.setPriority(10);

    sintok.start();
    try{
       Thread.sleep(500);
   }catch(Exception e){}
    kedah.start();
  }
}

Screenshot (542)

PhuahMeiQi commented 4 years ago

import java.util.stream.Stream;

public class issuefive {
    public static void main(String[] args) {
        String s1 = "universiti utara malsysia";
        String s2 = "satu dua tiga";

        String s3 = "satu dua tiga empat";
        String s4 = "one two three four";

        String[] split = s1.split(" ");
        String[] split1 = s2.split(" ");
        String[] split2 = s3.split(" ");
        String[] split3 = s4.split(" ");

        Thread t1 = new Thread(()->{

            for(int i=0;i<Stream.of(split).count();i++){
                System.out.print(split[i]+" ");
                try {
                    Thread.sleep(500);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                Thread.yield();
            }
            System.out.println();

            for (int j=0;j<Stream.of(split2).count();j++){
                System.out.print(split2[j]+" ");
                try {
                    Thread.sleep(500);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                Thread.yield();
            }
            });

        t1.start();

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread t2 = new Thread(()->{
        for(int k=0; k<Stream.of(split1).count(); k++) {
            System.out.print(split1[k]+" ");
            System.out.println();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
        }

        for(int l=0; l<Stream.of(split3).count(); l++) {
            System.out.print(split3[l]+" ");
            System.out.println();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
        }
    });
        t2.start();
    }
}

image

jasonway96 commented 4 years ago

package Exercise;

import java.util.stream.Stream;

public class Testing extends Thread
{
    String string1 = "University Utara Malaysia ";
    String string3 = "satu dua tiga empat";

    String[] split1 = string1.split(" ");

    String[] split3 = string3.split(" ");

    public void run() {
        for (int i = 0; i < Stream.of(split1).count(); i++) {
            System.out.print(split1[i] + " ");
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Thread.yield();
        }

        System.out.println();

        for (int i2 = 0; i2 < Stream.of(split3).count(); i2++) {
            System.out.print(split3[i2] + " ");
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
        }

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Exercise2 extends Thread
{
    String string2 = "satu dua tiga";
    String string4 = "one two three four";
    String[] split2 = string2.split(" ");
    String[] split4 = string4.split(" ");

    public void run()
    {
        for (int i3 = 0; i3 < Stream.of(split2).count(); i3++)
        {
            System.out.print(split2[i3] + " ");
            System.out.println();

            try
            {
                Thread.sleep(200);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            Thread.yield();
        }

        for (int i4 = 0; i4 < Stream.of(split4).count(); i4++)
        {
            System.out.print(split4[i4] + " ");
            System.out.println();

            try
            {
                Thread.sleep(200);
            }

            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            Thread.yield();
        }

    }

}

class Exercise3
{
    public static void main(String args[])
    {
        Testing t1 = new Testing();
        Exercise2 t2 = new Exercise2();

        t1.start();
        t2.start();
    }

}

1

liviniesh commented 4 years ago
class thread1 extends Thread{
      @Override
    public void run(){
      String s1 = "universiti utara malaysia";
      String s2 = "satu dua tiga";

      String s3 = "satu dua tiga empat";
      String s4 = "one two three four";

    System.out.println("S1= "+s1);  
    System.out.println("S2= "+s2);  
    System.out.println();
    System.out.println("S3= "+s3);  
    System.out.println("S4= "+s4);  
    System.out.println();
        }}

class thread2 extends Thread{
   @Override
    public void run(){

      String s1 = "universiti utara malaysia";
      String s2 = "satu dua tiga";

      String s3 = "satu dua tiga empat";
      String s4 = "one two three four";

        String[] string1 = s1.split("\\s");
        String[] string2 = s2.split("\\s");
        String[] string3 = s3.split("\\s");
        String[] string4 = s4.split("\\s");
        for(int i=0;i<string1.length;i++){
            System.out.println(string1[i]+" "+string2[i]);
        }
        System.out.println();
        for(int k=0;k<string3.length;k++){
            System.out.println(string3[k]+" "+string4[k]);

      }}}

class Main {
  public static void main(String[] args) {
    thread1 t1=new thread1();  
       thread2 t2=new thread2();  

       t1.start();  
       t2.start();
  }
}

3

peipei28 commented 4 years ago

import java.util.stream.Stream;

public class test { public static void main(String[] args) { String s1 = "universiti utara malsysia"; String s2 = "satu dua tiga"; String s3 = "satu dua tiga empat"; String s4 = "one two three four";

    Thread t1 = new Thread(()->{

        String[] split1 = s1.split(" ");
        String[] split3 = s3.split(" ");

        for(int a=0;a<Stream.of(split1).count();a++){
            System.out.print(split1[a]+" ");
            try {
                Thread.sleep(1000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            Thread.yield();
        }
        System.out.println();

        for (int b=0;b<Stream.of(split3).count();b++){
            System.out.print(split3[b]+" ");
            try {
                Thread.sleep(1000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            Thread.yield();
        }
    });

    t1.start();

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    Thread t2 = new Thread(()->{

        String[] split2 = s2.split(" ");
        String[] split4 = s4.split(" ");

        for(int c=0; c<Stream.of(split2).count(); c++) {
            System.out.print(split2[c]+" ");
            System.out.println();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
        }

        for(int d=0; d<Stream.of(split4).count(); d++) {
            System.out.print(split4[d]+" ");
            System.out.println();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
        }
    });
    t2.start();
}

} 3

Gv3N commented 4 years ago
**First Class**
public class Main extends Thread {
    public static void main(String[] args) {

        Thread1 T1 = new Thread1();
        Thread2 T2 = new Thread2();

        T1.setPriority(Thread.MAX_PRIORITY);
        T2.setPriority(Thread.MIN_PRIORITY);

        T1.start();
        T1.join();
        T2.start();

    }//main
}//class

**Second Class**
public class Thread1 extends Thread {
    //creating a global variable to be used among classes
     String s1 = "universiti utara malsysia";
     String s2 = "satu dua tiga";
     String s3 = "satu dua tiga empat";
     String s4 = "one two three four";

    public void run() {
        try {
            //to print display
            System.out.println("String 1 = "+s1);
            System.out.println("String 2 = "+s2);
            System.out.println("");
            System.out.println("String 3 = "+s3);
            System.out.println("String 4 = "+s4);
            System.out.println("");
            sleep(1000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }//catch
        }//run
    }//Thread1

/* referneces from:
https://www.w3schools.com/java/java_user_input.asp
 */

**Third Class**
public class Thread2 extends Thread {
    public void run(){
        try{
            String str1 = "universiti utara malsysia";
            String str2 = "satu dua tiga";
            String str3 = "satu dua tiga empat";
            String str4 = "one two three four";

            String[] words1 = str1.split(" "); //to split into individual word.
            String[] words2 = str2.split(" ");
            String[] words3 = str3.split(" ");
            String[] words4 = str4.split(" ");

            //for the first and second string output
            for(int i =0;i<words1.length;i++){
                System.out.println(words1[i]+" "+words2[i]);
                sleep(100);

            }//for i
            System.out.println("");
            //for the third and fourth string output
            for(int j = 0;j<words3.length;j++){
                System.out.println(words3[j]+" "+words4[j]);
                sleep(100);
            }//for j
        }catch (InterruptedException e){
            e.printStackTrace();
        }//catch
    }//run
}

/*reference from:
https://www.w3schools.com/java/java_arrays.asp
mostly from w3schools.com on constructors and methods
 */

Output s3

yyjmax commented 4 years ago
import java.util.Scanner;

class readThread extends Thread{
    Scanner sc = new Scanner(System.in);
    String s1;
    String s2;
    String s3;
    String s4;

    @Override
    public void run(){
        System.out.println("Enter String1: ");
        s1 = sc.nextLine();
        System.out.println("Enter String2: ");
        s2 = sc.nextLine();
        System.out.println("Enter String3: ");
        s3 = sc.nextLine();
        System.out.println("Enter String4: ");
        s4 = sc.nextLine();
    }
    public String getS1(){
        return s1;
    }
    public String getS2(){
        return s2;
    }
    public String getS3(){
        return s3;
    }
    public String getS4(){
        return s4;
    }
}

class TreadTest2 extends Thread {
    String s1;
    String s2;
    String s3;
    String s4;
    TreadTest2(String str1, String str2, String str3, String str4){
        this.s1 = str1;
        this.s2 = str2;
        this.s3 = str3;
        this.s4 = str4;
    }
    @Override
    public void run(){
        String[] array1 = s1.split(" ");
        String[] array2 = s2.split(" ");
        String[] array3 = s3.split(" ");
        String[] array4 = s4.split(" ");
        for (int i = 0;i<array1.length;i++){
            System.out.println(array1[i]+"  "+array2[i]);
        }
        System.out.println("");
        for (int j=0;j < array3.length;j++){
            System.out.println(array3[j]+"  "+array4[j]);
        }
    }

}
class main{
    public static void main(String[] args) throws InterruptedException {
     readThread t1 = new readThread();
     t1.start();

     t1.join();
     TreadTest2 t2 = new TreadTest2(t1.getS1(),t1.getS2(),t1.getS3(),t1.getS4());
     t2.start();

    }
}

QQ截图20191004172313

ychian234 commented 4 years ago
public class Real_Time_Programming {
    public static void main(String[] args) throws InterruptedException {
        Thread1 thd1 = new Thread1();
        thd1.start();
    }  
}
class Thread1 extends Thread{
    @Override
    public void run(){
        String stg1 = "Universiti Utara Malaysia";
        String stg2 = "Satu Dua Tiga";
        String stg3 = "Satu Dua Tiga Empat";
        String stg4 = "One Two Three Four";
        System.out.println("String 1 = "+stg1);
        System.out.println("String 2 = "+stg2);
        System.out.println("String 3 = "+stg3);
        System.out.println("String 4 = "+stg4);
        System.out.println();
        Thread2 thd2 = new Thread2();
        thd2.start();
    }
}
class Thread2 extends Thread{
    @Override
    public void run(){
        String stg1 = "Universiti Utara Malaysia";
        String stg2 = "Satu Dua Tiga";
        String stg3 = "Satu Dua Tiga Empat";
        String stg4 = "One Two Three Four";
        String[] word1 = stg1.split(" ");
        String[] word2 = stg2.split(" ");
        String[] word3 = stg3.split(" ");
        String[] word4 = stg4.split(" ");
        for(int i=0; i<word1.length; i++){
            System.out.print(word1[i]+" ");
            System.out.print(word2[i]+" ");
            System.out.println();
        }
        for(int i=0; i<word3.length; i++){
            System.out.print(word3[i]+" ");
            System.out.print(word4[i]+" ");
            System.out.println();
        }
    }
}

thread1

muhdhariz commented 4 years ago
package Issue5;
import java.util.*;

public class Issue5 {
    private static Thread Scanner = new scanner();
    private static Thread Output = new output();

    public static void main(String[] args) throws InterruptedException {
        Scanner.start();
        Scanner.join();
        System.out.println();
        Output.start();
    }
}

class scanner extends Thread{
    private static Scanner is = new Scanner(System.in);
    static String[] string = new String[4];

    public void run() {
        for (int i = 0; i < string.length; i++) {
            System.out.print("Enter String" + (i+1) + ": ");
            string[i] = is.nextLine();
        }
    }
}

public class output extends Thread{
    private static String[][] sSplit3 = new String[2][3];
    private static String[][] sSplit4 = new String[2][4];

    public void run() {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                sSplit3[i] = scanner.string[i].split(" ");
            }
        }
        for (int i = 0, r = 2; i < 2 && r < 4; i++, r++) {
            for (int j = 0; j < 4; j++) {
                sSplit4[i] = scanner.string[r].split(" ");
            }
        }

        for (int i = 0; i < 1; i++) {
            for (int j = 0; j < sSplit3[i].length; j++) {
                System.out.println(output.sSplit3[i][j] + " " + output.sSplit3[i+1][j]);
            }
        }
        System.out.println();
        for (int i = 0; i < 1; i++) {
            for (int j = 0; j < sSplit4[i].length; j++) {
                System.out.println(sSplit4[i][j] + " " + sSplit4[i+1][j]);
            }
        }
    }
}

image

SilentHlive commented 4 years ago
package com.company;

import java.util.Scanner;

public class ConvertStrings {
    public static void main(String[] args) throws InterruptedException {
        First f = new First();
        f.start();
        f.join( );
        Second s = new Second(f.getS1(),f.getS2(),f.getS3(),f.getS4());
        s.start();
    }
}

class First extends Thread {
    String s1,s2,s3,s4;
    Scanner scan = new Scanner(System.in);

    public String getS1() {
        return s1;
    }
    public String getS2() {
        return s2;
    }
    public String getS3() {
        return s3;
    }
    public String getS4() {
        return s4;
    }

    public void run() {
        try{
        System.out.print("String 1 = ");
        s1 = scan.nextLine();
        System.out.print("String 2 = ");
        s2 = scan.nextLine();
        System.out.print("String 3 = ");
        s3 = scan.nextLine();
        System.out.print("String 4 = ");
        s4 = scan.nextLine();
        System.out.println();
        sleep(500);
            } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Second extends Thread {
String s1,s2,s3,s4;
    public Second(String str1, String str2, String str3, String str4){
        this.s1=str1;
        this.s2=str2;
        this.s3=str3;
        this.s4=str4;
    }

    public void run() {
        try{
            String[] arrOfStr1 = s1.split(" ");
            String[] arrOfStr2 = s2.split(" ");
            String[] arrOfStr3 = s3.split(" ");
            String[] arrOfStr4 = s4.split(" ");
            for (int i = 0; i<arrOfStr1.length;i++) {
                System.out.println(arrOfStr1[i]+ " "+arrOfStr2[i]);
                sleep(500);
            }
            System.out.println();
            for (int i = 0; i<arrOfStr3.length;i++) {
                System.out.println(arrOfStr3[i]+ " "+arrOfStr4[i]);
                sleep(500);
            }
        }
        catch (Exception e) {
        e.printStackTrace();
    }
  }
}

convertString

aida27 commented 4 years ago
class thread1 extends Thread {

    String String1 = "universiti utara malaysia";
    String String2 = "satu dua tiga";
    String String3 = "satu dua tiga empat";
    String String4 = "one two three four";

    public String getString1() {
        return String1;
    }

    public String getString2() {
        return String2;
    }

    public String getString3() {
        return String3;
    }

    public String getString4() {
        return String4;
    }

}

class thread2 extends Thread {

    String String1;
    String String2;
    String String3;
    String String4;

    thread2(String String1, String String2, String String3, String String4) {
        this.String1 = String1;
        this.String2 = String2;
        this.String3 = String3;
        this.String4 = String4;
    }

    @Override
    public void run() {
        String[] arrOfStr1 = String1.split("\\s");
        String[] arrOfStr2 = String2.split("\\s");
        String[] arrOfStr3 = String3.split("\\s");
        String[] arrOfStr4 = String4.split("\\s");

        for (int i = 0; i < arrOfStr1.length; i++) {
            System.out.println(arrOfStr1[i] + " " + arrOfStr2[i]);
        }
        System.out.println();

        for (int i = 0; i < arrOfStr3.length; i++) {
            System.out.println(arrOfStr3[i] + " " + arrOfStr4[i]);
        }

    }
}

public class main {

    public static void main(String[] args) throws InterruptedException {

        thread1 t1 = new thread1();

        t1.start();
        t1.join();

        thread2 t2 = new thread2(t1.getString1(), t1.getString2(), t1.getString3(), t1.getString4());
        t2.start();

    }
}
Capture
sonyhana7 commented 4 years ago
import java.util.*;

public class mainClass {
    public static void main(String[] args) throws InterruptedException {
        thread1 t1 = new thread1();
        t1.start();
        t1.join( );
        thread2 t2 = new thread2(t1.getA1(),t1.getA2(),t1.getA3(),t1.getA4());
        t2.start();
    }
}

import java.util.Scanner;

public class thread1  extends  Thread{
    String a1,a2,a3,a4;
    Scanner scan = new Scanner(System.in);

    public String getA1() {
        return a1;
    }
    public String getA2() {
        return a2;
    }
    public String getA3() {
        return a3;
    }
    public String getA4() {
        return a4;
    }

    public void run() {
        try{
            System.out.print("String 1 = ");
            a1 = scan.nextLine();
            System.out.print("String 2 = ");
           a2 = scan.nextLine();
            System.out.print("String 3 = ");
            a3 = scan.nextLine();
            System.out.print("String 4 = ");
            a4 = scan.nextLine();
            System.out.println();
            sleep(500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class thread2 extends Thread {

    String a1,a2,a3,a4;
    public thread2(String str1, String str2, String str3, String str4){
        this.a1=str1;
        this.a2=str2;
        this.a3=str3;
        this.a4=str4;
    }

    public void run() {
        try{
            String[] arrOfStr1 = a1.split(" ");
            String[] arrOfStr2 = a2.split(" ");
            String[] arrOfStr3 = a3.split(" ");
            String[] arrOfStr4 = a4.split(" ");
            for (int i = 0; i<arrOfStr1.length;i++) {
                System.out.println(arrOfStr1[i]+ " "+arrOfStr2[i]);
                sleep(500);
            }
            System.out.println();
            for (int i = 0; i<arrOfStr3.length;i++) {
                System.out.println(arrOfStr3[i]+ " "+arrOfStr4[i]);
                sleep(500);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

image

AhmedBawazir2020 commented 4 years ago

Capture3


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package issues5;

import static issues5.Issues5.lock;
import static java.lang.Thread.sleep;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
 *
 * @author Ahmed_Bawazir
 */
public class Issues5 {

    /**
     * @param args the command line arguments
     */
    public static ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        // TODO code application logic here

        Mythread a = new Mythread();
        Mythread2 b = new Mythread2();
        a.start();
        a.join();
        b.start();
        b.join();
    }

}

class Mythread extends Thread {

    String a1 = "universiti ";
    String a2 = "satu ";
    String b1 = "utara ";
    String b2 = "dua ";
    String z1 = "malsysia ";
    String z2 = "tiga ";

    String x[] = a1.split(" ");
    String x1[] = a2.split(" ");
    String y[] = b1.split(" ");
    String y1[] = b2.split(" ");
    String f[] = z1.split(" ");
    String f1[] = z2.split(" ");

    public void run() {
        System.out.println("universiti utara malsysia");
        System.out.println("satu dua tiga\n");
        System.out.println("satu dua tiga empat");
        System.out.println("one two three four\n\n");

        for (int i = 0; i < Stream.of(a1).count(); i++) {
            lock.lock();
            System.out.print(x[i] + "     ");
            lock.unlock();
            try {
                sleep(200);
            } catch (InterruptedException e) {
            }
        }

        for (int i = 0; i < Stream.of(a2).count(); i++) {
            lock.lock();
            System.out.println(x1[i] + " ");
            lock.unlock();
            try {
                sleep(200);
            } catch (InterruptedException e) {
            }
        }
        for (int i = 0; i < Stream.of(b1).count(); i++) {
            lock.lock();
            System.out.print(y[i] + "          ");
            lock.unlock();
            try {
                sleep(200);
            } catch (InterruptedException e) {
            }
        }

        for (int i = 0; i < Stream.of(b2).count(); i++) {
            lock.lock();
            System.out.println(y1[i] + " ");
            lock.unlock();
            try {
                sleep(200);
            } catch (InterruptedException e) {
            }
        }
        for (int i = 0; i < Stream.of(z1).count(); i++) {
            lock.lock();
            System.out.print(f[i] + "       ");
            lock.unlock();
            try {
                sleep(200);
            } catch (InterruptedException e) {
            }
        }

        for (int i = 0; i < Stream.of(z2).count(); i++) {
            lock.lock();
            System.out.println(f1[i] + " ");
            lock.unlock();
            try {
                sleep(200);
            } catch (InterruptedException e) {
            }
        }
    }
}

class Mythread2 extends Thread {

    String a1 = "satu ";
    String a2 = "one ";
    String x1 = "dua";
    String x2 = "two ";
    String y1 = "tiga ";
    String y2 = "three ";
    String s1 = "empat ";
    String s2 = "four ";

    String A[] = a1.split(" ");
    String A2[] = a2.split(" ");
    String X[] = x1.split(" ");
    String X2[] = x2.split(" ");
    String Y[] = y1.split(" ");
    String Y2[] = y2.split(" ");
    String S[] = s1.split(" ");
    String S2[] = s2.split(" ");

    public void run() {

        for (int i = 0; i < Stream.of(a1).count(); i++) {
            lock.lock();
            System.out.print("\n"+A[i] + "    ");
            lock.unlock();
            try {
                sleep(300);
            } catch (InterruptedException e) {
            }
        }
        for (int i = 0; i < Stream.of(a2).count(); i++) {
            lock.lock();
            System.out.println(A2[i] + "  ");
            lock.unlock();
            try {
                sleep(300);
            } catch (InterruptedException e) {
            }
        }
        for (int i = 0; i < Stream.of(x1).count(); i++) {
            lock.lock();
            System.out.print(X[i] + "     ");
            lock.unlock();
            try {
                sleep(300);
            } catch (InterruptedException e) {
            }
        }
        for (int i = 0; i < Stream.of(x2).count(); i++) {
            lock.lock();
            System.out.println(X2[i] + "  ");
            lock.unlock();
            try {
                sleep(300);
            } catch (InterruptedException e) {
            }
        }

        for (int i = 0; i < Stream.of(y1).count(); i++) {
            lock.lock();
            System.out.print(Y[i] + "    ");
            lock.unlock();
            try {
                sleep(300);
            } catch (InterruptedException e) {
            }
        }
        for (int i = 0; i < Stream.of(y2).count(); i++) {
            lock.lock();
            System.out.println(Y2[i] + "  ");
            lock.unlock();
            try {
                sleep(300);
            } catch (InterruptedException e) {
            }
        }

        for (int i = 0; i < Stream.of(s1).count(); i++) {
            lock.lock();
            System.out.print(S[i] + "   ");
            lock.unlock();
            try {
                sleep(300);
            } catch (InterruptedException e) {
            }
        }
        for (int i = 0; i < Stream.of(s2).count(); i++) {
            lock.lock();
            System.out.println(S2[i] + "  ");
            lock.unlock();
            try {
                sleep(300);
            } catch (InterruptedException e) {
            }
        }

    }
}
FatihahFauzi commented 4 years ago
import java.util.stream.Stream;

public class ConvertString {
    public static void main(String[] args) {
        String s1 = "universiti utara malsysia";
        String s2 = "satu dua tiga";

        String s3 = "satu dua tiga empat";
        String s4 = "one two three four";

        String[] split = s1.split(" ");
        String[] split1 = s2.split(" ");
        String[] split2 = s3.split(" ");
        String[] split3 = s4.split(" ");

        Thread t1 = new Thread(()->{

            for(int i=0;i<Stream.of(split).count();i++){
                System.out.print(split[i]+" ");
                try {
                    Thread.sleep(500);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                Thread.yield();
            }
            System.out.println();

            for (int j=0;j<Stream.of(split2).count();j++){
                System.out.print(split2[j]+" ");
                try {
                    Thread.sleep(500);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                Thread.yield();
            }
        });

        t1.start();

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread t2 = new Thread(()->{
            for(int k=0; k<Stream.of(split1).count(); k++) {
                System.out.print(split1[k]+" ");
                System.out.println();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread.yield();
            }

            for(int l=0; l<Stream.of(split3).count(); l++) {
                System.out.print(split3[l]+" ");
                System.out.println();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Thread.yield();
            }
        });
        t2.start();
    }
}

image

aidqayyum commented 4 years ago
public class one {
    public static void main(String[] args) throws InterruptedException {
        Thread1 thd1 = new Thread1();
        thd1.start();
    }
}
class Thread1 extends Thread{
    @Override
    public void run(){
        String stg1 = "Universiti Utara Malaysia";
        String stg2 = "Satu Dua Tiga";
        String stg3 = "Satu Dua Tiga Empat";
        String stg4 = "One Two Three Four";
        System.out.println("String 1 = "+stg1);
        System.out.println("String 2 = "+stg2);
        System.out.println("String 3 = "+stg3);
        System.out.println("String 4 = "+stg4);
        System.out.println();
        Thread2 thd2 = new Thread2();
        thd2.start();
    }
}
class Thread2 extends Thread{
    @Override
    public void run(){
        String stg1 = "Universiti Utara Malaysia";
        String stg2 = "Satu Dua Tiga";
        String stg3 = "Satu Dua Tiga Empat";
        String stg4 = "One Two Three Four";
        String[] word1 = stg1.split(" ");
        String[] word2 = stg2.split(" ");
        String[] word3 = stg3.split(" ");
        String[] word4 = stg4.split(" ");
        for(int i=0; i<word1.length; i++){
            System.out.print(word1[i]+" ");
            System.out.print(word2[i]+" ");
            System.out.println();
        }
        for(int i=0; i<word3.length; i++){
            System.out.print(word3[i]+" ");
            System.out.print(word4[i]+" ");
            System.out.println();
        }
    }
}

convert

farisleh commented 4 years ago
package test;

public class Main{
    public static void main(String[] args) throws InterruptedException {
        Thread1 t1 = new Thread1();
        t1.start();
    }  
}

class Thread1 extends Thread{
    @Override
    public void run(){
        String stg1 = "Universiti Utara Malaysia";
        String stg2 = "Satu Dua Tiga";
        String stg3 = "Satu Dua Tiga Empat";
        String stg4 = "One Two Three Four";
        String[] word1 = stg1.split(" ");
        String[] word2 = stg2.split(" ");
        String[] word3 = stg3.split(" ");
        String[] word4 = stg4.split(" ");
        for(int i=0; i<word1.length; i++){
            System.out.print(word1[i]+" ");
            System.out.print(word2[i]+" ");
            System.out.println();
        }
        for(int i=0; i<word3.length; i++){
            System.out.print(word3[i]+" ");
            System.out.print(word4[i]+" ");
            System.out.println();
        }
    }
}

try2