hy im having a little problem with a loop with jgrasp here is my basic program

import javax.swing.JOptionPane;

public class loops1
{

	public static void main (String[]args)
	{ 
      String name
		do
		{
			String name =  JOptionPane.showInputDialog ("who is the cleverest person in the room?");
			
		}
		while (name != "erin");
		
	} 
}

ok so i declared the varible name outside so it can pick it up but it keeps giving me an error message saying name has already been declared What am i doing wrong?

Recommended Answers

All 3 Replies

Because you declare it twice:

[B]String[/B] name
do
		{
			[B]String[/B] name =  ..... ;
		}

You declare it once outside the loop and use it like any other variable:

String name = "5";

name="asdfasfd";

name += "ssss";

Also you must add a ';' at the end of the declaration and initialize it. Otherwise you will get a NullPointerException.

Also use the equals method for comparing strings:

while (name.equals("erin"));

Also you must add a ';' at the end of the declaration and initialize it. Otherwise you will get a NullPointerException.

"name" is definitely assigned before any use. Unless the code is changed so that is no longer true, it's better to use the declaration without initialization.

THANKS GUYS got it sorted

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.