Java programming- Help needed!?
Java programming- Help needed!?
At present, I am currently unable to compile the program which I have been working on, as I keep getting the error of: “can not find symbol-variable i”.
Here is my code:
import java.util.Scanner;
class jets24
{
public static void main (String[]args)
{
Scanner input= new Scanner (System.in);
//variables//
int TotalLuxurySeats = 4 ;
int TotalExecutiveSeats = 6 ;
int Option = 0 ;
int LuxurySeatsSold=0;
int ExectutiveSeatsSold=0;
String[] CustomerName= new String [10];
// seat objectives within the array//
for(int i=0; i<10; i++)
{
if (i<4)
CustomerName[i]=new String ("Luxury");
else
CustomerName[i]=new String ("Exectutive");
}
while(Option!=4 || LuxurySeatsSold>0 || ExectutiveSeatsSold>0)
{
System.out.println(” Hello and welcome to CCNJet! “);
System.out.println(” “);
System.out.println(” Please select an option from the following menu “);
System.out.println(” “);
System.out.println(” 1. Book a luxury seat “);
System.out.println(” 2. Book an exectutive seat “);
System.out.println(” 3. Display seat details/availability “);
System.out.println(” 4. Exit “);
Option=input.nextInt();
if ((Option==1) && (LuxurySeatsSold<4))
{
System.out.println(" Please enter your name ");
CustomerName[LuxurySeatsSold]=input.next();
System.out.println(" ");
System.out.println(" Thank you for your luxury seat order " + CustomerName[LuxurySeatsSold] + " You have seat number " + (LuxurySeatsSold + 1));
LuxurySeatsSold++;
LuxurySeatsSold=TotalLuxurySeats -1;
}
if ((Option==2) && ExectutiveSeatsSold<4)
{
System.out.println(" Please enter your name ");
CustomerName[ExectutiveSeatsSold]=input.next();
System.out.println(" ");
System.out.println(" Thank you for your exectutive seat order " + CustomerName[ExectutiveSeatsSold] + " You have seat number " + (ExectutiveSeatsSold+ 1));
ExectutiveSeatsSold++;
ExectutiveSeatsSold=TotalExecutiveSeats -1;
}
if (Option==3)
{
System.out.println(" Here is a report of available/unavailable seats on the aircraft ");
System.out.println("");
System.out.println(" Luxury seats ");
while (CustomerName[i]!=null)
for (int i=0;j<4;i++) //can not find symbol-variable i".//
{
if(customerName[i]==null) break;
System.out.println("Seat Number: " + i + "Name: " + customerName[i]);
}
System.out.println("Executive Seats");
while(CustomerName[k]!=null)
for( k=4;l<10;k++)
{
if(customerName[k]==null) break;
System.out.println("Seat Number : " + k + "Name: " + customerName[i]); }
}
}
if (Option==4)
{
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("=============");
System.out.println(" Good bye,thanks for looking!");
System.out.println("=========");
}
}
}
I realise that i is currently out of scope, however how can I adjust this so that is is?
Thanks in advance for any help,
Ben
JeffKan1- The following line is highlighted after the compile: while (CustomerName[i]!=null) it's still confussing me!
Best answer:
Here’s the ‘i’ it’s talking about:
while (CustomerName[i]!=null)
The variable i only exists in the for loops, which that line of code is not inside.
Tags: HELP, java, needed, programming
Under Forum

3 Comments for Java programming- Help needed!?
1. DevNull | March 25th, 2009 at 5:15 pm
while (CustomerName[i]!=null)
When you say this, i is not yet initialized. It doesn’t exist outside of the for loop that you initialize it in. You need to restructure your loops.
2. JeffKan1 | March 25th, 2009 at 5:48 pm
Hey Ben! Which line is highlighted after the compile? I can’t tell by looking at the source list.
It’s always possible that i is out of scope for some weird reason, such as a bad ( or { . If you haven’t tried yet, tab down through the left brackets, and see if your editor will highlight the matching righ bracket.
Another idea is to put pieces of the code into their own separate methods, and then your main line logic at the top of the class can just call these methods. That may help you figure out which i variable is giving you the trouble.
Paste back in here the fix to your code, I’d be interested in seeing it. :o)
3. ?Neeraj Yadav? | March 25th, 2009 at 6:36 pm
You were using i and k before declaring it and initilizing it
int i =0;
int k =0;
while (CustomerName[i]!=null)
for (i=0;i<4;i++) // can not find symbol-variable i”.//
{
if(CustomerName[i]==null) break;
System.out.println(“Seat Number: ” + i + “Name: ” + CustomerName[i]);
}
System.out.println(“Executive Seats”);
while(CustomerName[k]!=null)
for( k=4;k<10;k++)
{
if(CustomerName[k]==null) break;
System.out.println(“Seat Number : ” + k + “Name: ” + CustomerName[i]); }
}
Still think logic is somewhere wrong but try it will work after some work arrounds.
Why dont you use something like eclipse IDE
which will give you proper code assist and bug tracking and make learning curve easy.
eclipse.org/downloads/
Classic 3.4.1
Hope this will help
Cheers:)
Leave a Comment for Java programming- Help needed!?
Trackback this post | Subscribe to the comments via RSS Feed