Results 1 to 10 of 10
  1. #1
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default 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;
    	}
    does anyone has any ideas why? I only got few hours till i have to have this finished...

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What have you tried to test this? Where is the main method to call this?

  3. #3
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Look up the sun io tutorials, this can be done fairly easily with what the tutorial explains

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  6. #6
    Bagzli is offline Member
    Join Date
    Feb 2011
    Posts
    63
    Rep Power
    0

    Default

    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();
    		}

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by Bagzli View Post
    I wouldn't be posting here if i haven't tried already. I really don't get half of what those tutorials say.
    But anything we post here will be just repeating what is in the tutorial(s). If you don't understand them, what makes you think you will understand us. The only difference is that you want us to waste our time repeating stuff.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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();
    }
    It will look something like this, I am a little rusty with I/O and have mostly used Object streams to up to this point. The tutorials do a good job of covering this, just keep trying to understand it.

    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Junky View Post
    But anything we post here will be just repeating what is in the tutorial(s). If you don't understand them, what makes you think you will understand us. The only difference is that you want us to waste our time repeating stuff.
    ++

    Why should we re-write tutorials that have been written better than anything we can do?

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

Similar Threads

  1. Best reason to not go to school ever
    By Zack in forum Forum Lobby
    Replies: 5
    Last Post: 11-28-2010, 05:09 AM
  2. Need a reason for output
    By Hemant16 in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-26-2010, 11:05 AM
  3. sound works then stops!
    By smileii in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-26-2009, 01:06 AM
  4. Getting errors for some reason
    By Swarvy in forum New To Java
    Replies: 7
    Last Post: 09-30-2008, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •