473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Variable may not have been initialzed

2 New Member
Hi all,

I'm trying to write a fairly simple program in Java that will allow the user to write to a text file, specifiying the File name and Content.
I keep getting the same error though that comes up with 'Variable [Variable name] may not be initialized' I've struggled for ages now but can't find the solution

Any help would be greatly appreciated! Sorry about the lack of comments.

Expand|Select|Wrap|Line Numbers
  1. import java.lang.*;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.net.*;
  5. public class FileMakerApp extends Object
  6. {
  7.   public static void main(String[] argStrings) throws Exception
  8.   {
  9.     String lnBreak = ("----------------------------------");
  10.  
  11.     int menu = 0; 
  12.  
  13.     do
  14.     {
  15.       Scanner choice = new Scanner(System.in);
  16.  
  17.       System.out.print(" What would you now like to do? "); 
  18.       System.out.println(lnBreak);
  19.       System.out.println("1: Create new file");
  20.       System.out.println("2: View name of last file created");
  21.       System.out.println("3: View content of last file created");
  22.       System.out.println("4: View location of last file created");
  23.       System.out.println("5: Exit Program");
  24.       System.out.println(lnBreak);
  25.  
  26.       menu = choice.nextInt();
  27.  
  28.       switch (menu) 
  29.       {
  30.       case 1:
  31.         System.out.print(" Please insert the name of the text file (E.G input.txt)you wish to create: ");
  32.  
  33.         Scanner input = new Scanner(System.in);
  34.         String fileName = input.nextLine();
  35.         PrintStream out = new PrintStream(fileName);
  36.  
  37.         System.out.print(" Please insert the text you wish to be saved to the file and press 'enter': ");
  38.  
  39.         Scanner inputs = new Scanner(System.in);
  40.         String inputText = inputs.nextLine();
  41.  
  42.         out.println(inputText);
  43.  
  44.         out.close();
  45.       case 2:
  46.         System.out.println();
  47.         System.out.println("File called: " + fileName + ".txt");
  48.         System.out.println();
  49.         break;
  50.       case 3:
  51.         System.out.println();
  52.         System.out.println("File content:");
  53.         System.out.println();
  54.         System.out.println(inputText);
  55.         System.out.println();
  56.         System.out.println(lnBreak);
  57.         break;
  58.       case 4:
  59.         System.out.println();
  60.         System.out.println("File located at: D:\\files\\2\\classes");
  61.         System.out.println();
  62.         break;
  63.  
  64.       case 5: 
  65.         System.out.println();
  66.         System.out.println("Program Successfully Exited");
  67.         System.out.println();
  68.       default:
  69.         System.out.println();
  70.         System.out.println("No such option. Please try again.");
  71.         System.out.println();
  72.         break;
  73.       }
  74.     } 
  75.     while (menu !=5);
  76.  
  77.   }

P.S the variables that are causing the errors are 'fileName' and 'inputText'
Dec 8 '07 #1
3 1129
JosAH
11,448 Recognized Expert MVP
Suppose the user first types '2' (check it in your source code); obviously variable
'fileName' isn't initialized yet. That's why the compiler is complaining.

kind regards,

Jos
Dec 8 '07 #2
FooFan
2 New Member
Ah I see...

I've now taken the file writing part of the code out of the Do.. while loop, but can't think of a way to enable a 'create new file' option in the menu that will allow the other menu options to be kept updated.

I tried repeating the code as a case 5, but just got the 'variable is already defined' error.

The program runs fine without it, but it would've been a nice feature.

Thanks for your help.
Dec 8 '07 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
Declaring variables in switch cases is going to get you into trouble. With switch statements I recommend keeping the code in each case as simple as possible, say just call a subroutine method you've defined:
Expand|Select|Wrap|Line Numbers
  1. switch (menu) {
  2. case 1:
  3.     createNewFile();
  4.     break;
  5. case 2:
  6.     viewLastFilename();
  7.     break;
  8. ...
  9. }
  10.  
You can either pass arguments to these subroutines and return values, or use the state of the current object.
Dec 8 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
by: Donna Hawkins | last post by:
I want to use javascript to redirect to a URL which has been passed as a variable (in php). I have searched but cannot find any solution. I think this code is a basic redirect: <script...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
5
by: risha | last post by:
void FlashReadM(volatile unsigned long *fl_ad,unsigned long abb) { unsigned long ulvald; abb=2; ulvald=0; //fl_ad=(volatile unsigned long *)0x8020; ulvald= *(fl_ad); //return ucVal; }
7
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
6
by: mickey.marshall | last post by:
I have a simlpe project that has a base class and two inherited classes. I want to make a variable that gets initialzed and set one time and is then accessable from all inherited classes. I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.