There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer blue screen boot bsod crash dell drive driver drivers error ethernet excel freeze gaming google hard drive hdd hdmi internet internet explorer internet explorer pop ups keyboard laptop malware memory missing monitor motherboard network problem ram router security server slow software svchost.exe trojan ubuntu 11.10 uninstall usb video virus vista windows windows 7 windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Illegal start of type and expression & empty while statement

Reply  
Thread Tools
javalava's Avatar
Junior Member with 6 posts.
 
Join Date: Jan 2012
Experience: Beginner
25-Jan-2012, 01:36 PM #1
Illegal start of type and expression & empty while statement
Hi!
I was wondering if somone could help me with my java program. I am getting some errors and dont know how to fix them. The program is not finished yet, so the end bit may no make sense! im still working on it.

public static void main(String[] args) {
String file;
Scanner scan = new Scanner (System.in);
System.out.println("Enter file names : ");
while(scan.hasNextLine()); {
file = scan.nextLine(); }
if (file.endsWith(.txt));
System.out.println("Text File: " + file);
if (file.contains(del));
System.out.println("Delete: " + file);
if (file.contains(del)&& (file.endsWith(.txt));

System.out.println("Text File: " + file); }
if (while(file.contains("blank line tp end"))) == true));
}
sepala's Avatar
Computer Specs
Member with 3,822 posts.
 
Join Date: May 2010
Location: Under the LION FLAG(Sri Lanka)
Experience: SD Student (JAVA)
25-Jan-2012, 02:27 PM #2
Reasons for "Illegal Start Of Type" exception

1. contains() method of String accepts CharacterSequence. But what you have passed is a variable.
So, it should be something like this > file.contains("sa");

2. endsWith() method of String accepts "String" as a parameter. What you have passed is a variable.
So it should be > file.endsWith(".txt")

Do the same for the next few similar errors.



Reason for Empty while Statement

1. You have closed the while loop before the condition. So, remove the semicolon and replace it to correct position


Other Errors

Have a look at this code snippet

Code:
if (file.contains(del)&& (file.endsWith(.txt));
Apart from the mistakes I ahve mentioned, you have forgotten to close the brackets properly. It should be
Code:
if (file.contains("del"))&& (file.endsWith(".txt"));
This code
Code:
if (while(file.contains("blank line tp end"))) == true));
is 100% wrong
__________________
My country Is My Life
Love your country whatever it is..It is your soul..It is your strength..Never blame to it..
Are you an Asian using TSG? Then join Asian TSG Group
javalava's Avatar
Junior Member with 6 posts.
 
Join Date: Jan 2012
Experience: Beginner
25-Jan-2012, 02:41 PM #3
Thank you thank you thank you. although now i am getting one error saying i may not have initialized the variable when it is.
String file;
Scanner scan = new Scanner(System.in);
System.out.println("Enter file names : ");
while (scan.hasNextLine()) {
file = scan.nextLine();
}
if (file.endsWith(".txt")) { ------------------->>>> error:variable may not have been initialized
System.out.println("Text File: " + file);
if (file.contains("del"));
System.out.println("Delete: " + file);
if (file.contains("del") && (file.endsWith(".txt")));
System.out.println("Text File: " + file);
}
}
}
sepala's Avatar
Computer Specs
Member with 3,822 posts.
 
Join Date: May 2010
Location: Under the LION FLAG(Sri Lanka)
Experience: SD Student (JAVA)
25-Jan-2012, 02:59 PM #4
I assume your whole code is under the main method. If it is, then you should expect that error because the "file" variable ISN'T INITIALIZED. So, it should be initialized in this way

String file = null;

not just

String file;


ALL THE LOCAL VARIABLES INSIDE A METHOD HAS TO BE INITIALIZED FIRST
__________________
My country Is My Life
Love your country whatever it is..It is your soul..It is your strength..Never blame to it..
Are you an Asian using TSG? Then join Asian TSG Group
javalava's Avatar
Junior Member with 6 posts.
 
Join Date: Jan 2012
Experience: Beginner
26-Jan-2012, 06:11 AM #5
Thanks. what line of code will i need to end the code when a blank line is entered?
sepala's Avatar
Computer Specs
Member with 3,822 posts.
 
Join Date: May 2010
Location: Under the LION FLAG(Sri Lanka)
Experience: SD Student (JAVA)
26-Jan-2012, 12:50 PM #6
your question is not clear. If you are asking "How do I close the operations if a blank line is entered", then this is the way.

1. Blank line means a NULL value OR just a space
2. Your code have to check whether the insertion is null or not.
3. If null, the while loop should break

Have a look at the following code

Code:
if(file==null || file.length()==0)
{
break;
}
put this code at the beginning of your while loop
__________________
My country Is My Life
Love your country whatever it is..It is your soul..It is your strength..Never blame to it..
Are you an Asian using TSG? Then join Asian TSG Group
sepala's Avatar
Computer Specs
Member with 3,822 posts.
 
Join Date: May 2010
Location: Under the LION FLAG(Sri Lanka)
Experience: SD Student (JAVA)
27-Jan-2012, 12:32 PM #7
Anyway, if you need anymore help. please copy and paste the complete class, because then only we can read the code easily
Reply

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:36 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.