Question

JAVA CODE String[] str = in.nextLine().split("\\s"); for(int i=0;i<=str.length;i++){ if (str[i].equals(stop)) break;    if(i%2==0){ System.out.print(str[i]+" "); }...

JAVA CODE

String[] str = in.nextLine().split("\\s");

for(int i=0;i<=str.length;i++){
if (str[i].equals(stop)) break;
  
if(i%2==0){
System.out.print(str[i]+" ");
}   
}

lets say the INPUT for the array is: girl, boy, man, woman, girl, man, boy

i dont want the array to print any string twice, i want the output to be like this:

print all of them but print each once ---------- OUTPUT: girl, boy, man, woman

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class CheckDuplicates {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] str = in.nextLine().split("\\s");

        for (int i = 0; i < str.length; i++) {
            boolean found = false;
            for (int j = 0; j < i; j++) {
                if (str[i].equals(str[j])) {
                    found = true;
                }
            }
            if (!found) {
                System.out.print(str[i] + " ");
            }
        }
        System.out.println();
    }
}
Add a comment
Know the answer?
Add Answer to:
JAVA CODE String[] str = in.nextLine().split("\\s"); for(int i=0;i<=str.length;i++){ if (str[i].equals(stop)) break;    if(i%2==0){ System.out.print(str[i]+" "); }...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • 1. What is output by the following code? String s0 = "Java"; int i1 = s0.indexOf('a');...

    1. What is output by the following code? String s0 = "Java"; int i1 = s0.indexOf('a'); int i2 = s0.indexOf('a', i1); int i3 = s0.indexOf('a', i1 + 1); System.out.print(i1 + " " + i2 + " " + i3); 2. What is output by the following code? String s1 = "Java"; String s2 = ""; for (int i = 0; i < s1.length(); i++) { s2 += s1; } System.out.print(s2);

  • My current code is: for (int i = 0; i < items; i++) { System.out.println(""); System.out.print("Enter...

    My current code is: for (int i = 0; i < items; i++) { System.out.println(""); System.out.print("Enter name of assessment item #" + (i+1) + ": "); if (input.hasNext()) { name[i] = input.next(); input.nextLine(); } System.out.print("Enter the weighting of assessment item #" + (i+1) + ": "); weight[i] = input.nextInt(); input.nextLine(); } System.out.println(""); System.out.print("Enter your mark for \"" + name[0] + "\" as a percentage: "); int mark1 = input.nextInt(); However, if I Enter "Final Examination" - the output from calling...

  • Hello, I have a assignment where the user inputs a word and the out will then...

    Hello, I have a assignment where the user inputs a word and the out will then tell the user how many of the five vowels are in the word so for example if I typed the word hello, it would output {0 1 0 0 0 } since there is only one vowel and that is e which is in the second row, I have most of it done, but I can't seem to figure out how to get the...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Here is my code to put a quote down, flip it, and reverse it. I do...

    Here is my code to put a quote down, flip it, and reverse it. I do not have much knowledge on coding. Does my code only use Variables, Console I/O, Java Program Design, Intro to IDEs, If Statements, Selection and Conditional Logic, Strings, Loops, Nesting, and Iteration? If not, could it be fixed to only use them? These are the offical steps of the assignment: - Ask a user to enter a quote - whether it's several sentences or a...

  • java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and sh...

    java programming how would i modify the code below to add a GUI (with a main menu with graphics and buttons, etc...) and include an option to print a list of all students added in a grid format and short by name, course, instructor, and location. ///main public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Student>students = new ArrayList<>(); int choice; while(true) { displayMenu(); choice = in.nextInt(); switch(choice) { case 1: in.nextLine(); System.out.println("Enter Student Name"); String name...

  • Language is Java. Thank you.It is a review test and I will find out the rest...

    Language is Java. Thank you.It is a review test and I will find out the rest if I just know that answers. 1) What is the output of the following code: public class Test public static void main(String] args) [ String s1 new String("Java String s2 new String("Java) System.out.print( (s1 s2)(s1.equals (s2))) A) false false B) true true C) false true D) true false E) None of the above 2) Given the following program: public class Test f public static...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT