Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: help im a begginer

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help im a begginer

    problem i have been trying to validate the subject code which is written on a file called Subjects.txt i want to validate if the subjectcode on the file is unique and does not contain the same subject code.

    here is code of methods so far:
    public boolean codeExists () throws Exception{
    		File file = new File ("Subjects.txt");
    		Scanner input = new Scanner(file);
     
     
    		boolean exists = false;
    		while(input.hasNext()){
    		String subjectCode = input.next();
    		String subjectName = input.next();
     
    		if(input.next().equals(subjectCode)){
    			exists = true;
    		}
    		else{
     
    			exists = false;
     
    		}
     
    		}
     
    		input.close();
     
    		return exists;
    	}

    thanks


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: help im a begginer

    Ok, so what is the format of the text file?

    I would assume you have more than 2 subject names and you are wanting to test each against each other.

    Consider the code you have above. In the while loop, you are asking input to give you the next, then the next again. It is important to remember that the next method moves the imaginary placeholder on the scanner object. So, here is a description of what is happening:
    Loop 1: Subject 1 compared to Subject 2
    Loop 2: Subject 3 compared to Subject 4
    Loop 3: Subject 5 compared to Subject 6
    ect.

    What you want to do (if my assumption of your goal is correct) is to read in each subject, store it in a data structure (such as an array, ArrayList, Vector, ect.), then go through the data structure and compare each Subject to all the other Subjects in the data structure.

    Do I get the general idea of what you are doing and does that make sense?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. [SOLVED] Begginer Question please help ASAP! (Easy to Answer)
    By Bagzli in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 11th, 2010, 10:00 PM
  2. begginer wondering why his guessing game won't work
    By Ligawulf in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 8th, 2010, 12:22 AM
  3. Any ideas for begginer?
    By SwEeTAcTioN in forum Java Theory & Questions
    Replies: 11
    Last Post: October 27th, 2009, 03:28 AM