hello guys iam new in this forum and comunity can you please help me with my code?

i must input a string or name depending on how any time i like and out u it at the end
but my code only ouputed the last name i enetered
and it skipped the loop instead i dont know why

import java.io.*;

public class name{

    public static void main(String args[]) throws IOException {
    	BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
    	String[] input = new String[100];
    	int i=0,ctr=1;
    
    	System.out.print("Enter Name:\t");
    	input = in.readLine();
    	do{
    		
    		System.out.print("Enter Name:\t");
    		input[i] = in.readLine();
    		i++;
    	}while(input[i]!=input);

    
    	for (int j = 0; j < i; j++)
		System.out.println(input[j]);
    
    	
    	
    }
    
    
}

:-/

I made a correction but the code is slightly different.

There were some errors in your code, such as assigning a String to the String[] reference and not an actual indice of the String array.

Here's the code--

import java.io.*;

public class name{

    public static void main(String args[]) throws IOException {
    	BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); // getting a handle to the standard input stream
    	String[] input = new String[100]; // declaring an array that can hold up to 100 Strings
    	int i=0,ctr=1; // i determines the amount of Strings entered. Not entirely sure what ctr is doing...

    //	System.out.print("Enter Name:\t"); // Unnecessary, this will be printed at least once in do-while loop
    //	input = in.readLine();             // error, attempting to assign a String to the address of String array!
    	boolean stopped = false; // determines if loop should continue or end
    	do{ // doing something at least once...

    		System.out.print("Enter Name:\t"); // printing out something
    		String userInput = in.readLine();  // reading a line of characters from keyboard then storing the result in String

    		if(userInput.equalsIgnoreCase("exit")){ // if the stored result consists of "exit" (ignoring letter cases) then....
				stopped = true; // set stopped to true, ending this do-while loop
			}
    		else{ // otherwise...
    			input[i++] = userInput; // store the user's input in indice i of the array then increment i
			}
    	}while(stopped == false); // continue loop until stopped is true

    	for (int j = 0; j < i; j++)
			System.out.println(input[j]); // should print out the values in the array
    }
}

I made a correction but the code is slightly different.

There were some errors in your code, such as assigning a String to the String[] reference and not an actual indice of the String array.

Here's the code--

import java.io.*;

public class name{

    public static void main(String args[]) throws IOException {
    	BufferedReader in = new BufferedReader (new InputStreamReader (System.in)); // getting a handle to the standard input stream
    	String[] input = new String[100]; // declaring an array that can hold up to 100 Strings
    	int i=0,ctr=1; // i determines the amount of Strings entered. Not entirely sure what ctr is doing...

    //	System.out.print("Enter Name:\t"); // Unnecessary, this will be printed at least once in do-while loop
    //	input = in.readLine();             // error, attempting to assign a String to the address of String array!
    	boolean stopped = false; // determines if loop should continue or end
    	do{ // doing something at least once...

    		System.out.print("Enter Name:\t"); // printing out something
    		String userInput = in.readLine();  // reading a line of characters from keyboard then storing the result in String

    		if(userInput.equalsIgnoreCase("exit")){ // if the stored result consists of "exit" (ignoring letter cases) then....
				stopped = true; // set stopped to true, ending this do-while loop
			}
    		else{ // otherwise...
    			input[i++] = userInput; // store the user's input in indice i of the array then increment i
			}
    	}while(stopped == false); // continue loop until stopped is true

    	for (int j = 0; j < i; j++)
			System.out.println(input[j]); // should print out the values in the array
    }
}

thankyou so much for your help but if you notice the do whille loop will be skiped and not a part of the process how can i do that?

the do-while loop is skipped where? In your code or mine?

In your code you make two mistakes. The first one (already mentioned) was the assignment of String to type String[].

The second one is the condition statement in your while loop-- while(input[i]!=input) Now you're trying to compare a String (input) to a String[] (input), or in english you're trying to compare beans with apples.

I'm assuming you want the user to input information until the array is full? If so you'd do something like this-- while(i != input.length) -- now you're comparing beans with beans (the integer i with the integer value of the array length).

The only problem with this is that the user will now be trapped in the do-while loop until he/she enters 100 names. There should be some variability in how many names can be entered. *A solution to that was implemented in my version.

* I solely mean a solution and not the only solution. There are a million ways to skin a cat, I just used what i thought was the easiest way to understand.

import java.io.*;
import javax.swing.*;

public class name{

    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
        String[] input = new String[100];
        int i=0,ctr=1;
        String name;
        do{

            name=JOptionPane.showInputDialog("Enter Name:");
            input[i] = name;
            i++;
        }while(input[i]!=name);


        for (int j = 0; j < i-1; j++)
        System.out.println(input[j]);



    }


}

start quote:

import java.io.*;
import javax.swing.*;

public class name{

    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
        String[] input = new String[100];
        int i=0,ctr=1;
        String name;
        do{

            name=JOptionPane.showInputDialog("Enter Name:");
            input[i] = name;
            i++;
        }while(input[i]!=name);


        for (int j = 0; j < i-1; j++)
        System.out.println(input[j]);



    }


} 

end quote.

Don't make us guess. Is this new code, old code, same problem, new problem? What's the question?

hi dude
in your code an error is there for adssigning the string to string array without mentioning the index;and another in do while in condition
part

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.