This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

First time post! Need help with loop that test if a file exists.  RSS feed

 
Steve Ki
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, Im just wrapping up my Java class and need help on a issues.

The assignment requires the user to enter a file name they want to open. If it exists, it opens and reads the file. If not it continues to ask the user to enter a new file name.
This is where I get confused because Java will not let me use the same code inside the loop



This gets the compiler to tell you that its already declared in main. I understand that . So I tweaked the names and it compiles correctly but then you get stuck in the loop when you enter an existing file. Any advice? Thanks


 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where you are "reusing" the same variable name, you just take off the variable type the second and additional times.

String first_name = "Joe";

first_name = "Sue"; // OK - just giving a different value to "first_name"

String first_name = "Tom"; // compile error - trying to declare another "first_name" variable

first_name = "Nancy"; // OK - just giving a different value to "first_name"
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Ki wrote:Any advice? Thanks

Hi Steve, and welcome to JavaRanch.

The answer is: Yes; turn those declarations inside your loop into assignments.
String filename = keyboard.nextLine();
Declares a variable called 'filename', so if you put it inside your loop as well, the compiler thinks you're trying to define another variable called 'filename'.

Winston

PS: Too slow.
 
Steve Ki
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Red, sometimes I think the solution is gonna be complex and it turns out to be so easy. Thanks Again!
 
Steve Ki
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Winston Gutkowski wrote:
Steve Ki wrote:Any advice? Thanks

Hi Steve, and welcome to JavaRanch.

The answer is: Yes; turn those declarations inside your loop into assignments.
String filename = keyboard.nextLine();
Declares a variable called 'filename', so if you put it inside your loop as well, the compiler thinks you're trying to define another variable called 'filename'.

Winston

PS: Too slow.


Thanks Winston, i look forward to learning here and maybe even answer a question correctly!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!