Results 1 to 5 of 5
- 03-27-2012, 09:53 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Just wondering if somebody could check my IF ELSE statement ?
Hi guys, Just wondering if somebody could check my IF ELSE statements as it seems to completely skip part of my code!
Sorry about the long piece of code. Basically I open i file dialog box, then open a txt file. Read the data from it. If it is a blank line skip that line, if the line is a // line skip it, but when it reaches a flag "[ " that data should be stored as appropriate.
Thanks for any help!
Java Code:public void readData() { FileDialog fileDialog = new FileDialog(mainWindow, "Open", FileDialog.LOAD); fileDialog.setVisible(true); filename = fileDialog.getFile(); //Loads a file dialog window for the user to select a file try { //Preparing a Scanner that will scan the document Scanner input = new Scanner(new File (filename)); while( input.hasNextLine() )//Read each line of the file { lineOfText = input.nextLine(); //Reads a line of text AudioVisual av; typeOfData = null; if (! lineOfText.isEmpty() && ! lineOfText.startsWith("//")) { //Ignore } else if (lineOfText.startsWith("[")) //If this line is a flag { if (lineOfText.equalsIgnoreCase("[CD data]")) typeOfData = "CD"; //Stores type of data to be CD else if (lineOfText.equalsIgnoreCase("[DVD data]")) typeOfData = "DVD"; //Stores type of data to be DVD else if (lineOfText.equalsIgnoreCase("")){ System.out.println("Unknown data type!"); //Deal with unknown type not sure if this is right } input.useDelimiter("[ ]*(,) [ ]*"); //Use delimiter } else if (typeOfData.equals("CD"))//Checks to make sure its CD data//When it gets to this line it skips to catch and gives NullPointerException { av = new CD(); av.extractTokens(input); //Extracts the tokens addItem(av); } else if (typeOfData.equals("DVD")) //Checks to make sure its DVD data { av = new DVD(); av.extractTokens(input); //Extracts the tokens addItem(av); } else { typeOfData = null; //Not sure about this line } } input.close();//Closes 2nd scanner }catch (Exception e){e.printStackTrace();} }Last edited by sim18; 03-27-2012 at 09:58 AM.
- 03-27-2012, 09:56 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Just wondering if somebody could check my IF ELSE statement ?
Please use [code] tags [/code] when posting code, especially large chunks of it like this.
Please do not ask for code as refusal often offends.
- 03-27-2012, 09:59 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Just wondering if somebody could check my IF ELSE statement ?
Nowhere in between that declaration and that 'else if' do you instantiate typeOfData, so it is always null if you fall through to that bit of your if..else if...else block.Java Code:while () { typeOfData = null; ... else if (typeOfData.equals("CD")) ... }Please do not ask for code as refusal often offends.
- 03-27-2012, 10:05 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 30
- Rep Power
- 0
Re: Just wondering if somebody could check my IF ELSE statement ?
Do you mean moving typeOfData = null, further down in my code say to line 26, or that line 37's else if is wrong ?
Thank you
- 03-27-2012, 10:47 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
I was wondering if it is possible to record with Java
By Ben132 in forum New To JavaReplies: 1Last Post: 03-23-2012, 04:09 AM -
I was wondering if why Im getting a cant find symbol error can someone please help
By saba2005 in forum New To JavaReplies: 2Last Post: 09-25-2011, 02:48 AM -
Wondering what I need to learn to make web apps
By silverglade in forum New To JavaReplies: 10Last Post: 06-10-2011, 01:10 AM -
Wondering..~
By ryozkidz in forum Java AppletsReplies: 0Last Post: 01-20-2011, 10:09 PM -
Hi, i was wondering if someone could help me
By deathnote202 in forum New To JavaReplies: 4Last Post: 04-23-2010, 05:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks