Results 1 to 10 of 10
Thread: Code stops for no reason
- 03-02-2011, 10:29 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
Code stops for no reason
this code just doesnt want to wrok. it stops on the last loop. It doesnt even run the loop. here is the code
Java Code:public static int testMethod(int lineCount3, int lineCounter, String userSelection, String[][] initialCourses, String[][] userData) { Scanner input = new Scanner(System.in); boolean check = false; int finalCheck = 0; for(int z = 1; z < lineCounter; z++) { if(userSelection.equals(initialCourses[z][1])) { System.out.println("SUCCESS"); System.out.println("WHY AM I STOPPING"); check = true; } else if(z == (lineCounter - 1)) { System.out.println("You have entered invalid course code"); System.out.println("Please enter course code again\n"); userSelection = input.nextLine(); z=0; } } if(check == true) { System.out.println("OMG OMG OMG"); for(int g = 1; g < lineCount3; g++) { System.out.println("final Test2"); if(userSelection.equals(userData[g][1])) { System.out.println("You are already enrolled in this course"); finalCheck = 1; break; } else if(g == (lineCount3 - 1)) { System.out.println("Course Added"); finalCheck = 1; break; } } } return finalCheck; }
- 03-02-2011, 11:31 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
What have you tried to test this? Where is the main method to call this?
- 03-02-2011, 11:34 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
i don't have time to explain all sorry, i went with anther approach and its working somehow.
Can somebody please teach me how to edit existent files with java? I just need basics like how do you add lines to a file, search lines in a file and delete lines in a file.
- 03-02-2011, 11:42 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Look up the sun io tutorials, this can be done fairly easily with what the tutorial explains
-
The Oracle Java tutorials are the first place to start.
- 03-03-2011, 12:11 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 63
- Rep Power
- 0
I wouldn't be posting here if i haven't tried already. I really don't get half of what those tutorials say.
This is all i have left to do to finish my project so please one kind soul help me?
If you dont think i tried i went with "http://download.oracle.com/javase/tutorial/essential/io/file.html" first and it doesn't explain it any way i learned so far and i tried other websites as well. The ways i have learned is with PrintWriter except i wasnt there and the book doesnt mention how to access files that are already created.
example: type of code i use to create files:
Java Code:File userFileLog = new File(userFileId); if(!userFileLog.exists()) { PrintWriter output2 = new PrintWriter(userFileLog); output2.print("Text"); output2.close(); }
- 03-03-2011, 12:19 AM #7
- 03-03-2011, 12:23 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
How I would do it is create a File object for the file you want to read from or write to. From there you chain a file stream to a buffered stream.
Java Code:try{ File f = new File("SomeFile"); FileInputStream fis = new FileInputStream(f); BufferedReader bis = new BufferedReader(fis); String line; while(line = bis.readLine() != null){ //do something } } catch(IOException ioe){ ioe.printStackTrace(); }
Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
A lot of IO learning is discovery. Google around the java API's for the methods and types of streams, and just practice it. They describe what they do in the api, and give you all the methods you can use.
-
- 03-03-2011, 04:56 AM #10
Similar Threads
-
Best reason to not go to school ever
By Zack in forum Forum LobbyReplies: 5Last Post: 11-28-2010, 05:09 AM -
Need a reason for output
By Hemant16 in forum Threads and SynchronizationReplies: 3Last Post: 09-26-2010, 11:05 AM -
sound works then stops!
By smileii in forum CLDC and MIDPReplies: 0Last Post: 09-26-2009, 01:06 AM -
Getting errors for some reason
By Swarvy in forum New To JavaReplies: 7Last Post: 09-30-2008, 03:45 PM
Bookmarks