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.

Page 2 of 2 FirstFirst 12
Results 26 to 41 of 41

Thread: Reading txt

  1. #26
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    Quote Originally Posted by Norm View Post
    The tutorial shows an example. The Bicycle interface defines several methods. The ACMEBicycle class impletments that interface and provides definitions for the methods defined in the Bicycle interface. Now an instance of the ACMEBicycle class can be treated as a Bicycle allowing any other code to call any of the Bicycle interface's methods.

    For your case, the City class must implement the Comparable interface and provide definitions for the method(s) in Comparable. Then the sort method which requires objects of type Comparable will call the methods to sort a collection of City objects.
    Read the API doc for the Collections class's sort method. It says:


    A commonly used interface is ActionListener. Have you used that? Look at the code for that.
    Also see an example in the tutorial:
    https://docs.oracle.com/javase/tutor...nlistener.html
    Will try it out and try to understand it better. Thanks Norm.
    "Tick, tack"

  2. #27
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    Have tried to use an actionlistener all night.

    Nothing is working for me... even with all the damn youtube videos around, my books or the examples.
    :/ Feel really lost.
    "Tick, tack"

  3. #28
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Reading txt

    Okay, let's make this simple. Here is an interface.

    interface EvenOdd {
    boolean isEven(int v);
    }

    Create a small class that implements the interface and returns either true if the integer is even or false if it is odd. To test it, simply call the method with a number.

    The only difference structure wise between this and the Comparable interface is that the Comparable interface is provided by the JDK API so all you have to do is implement it.

    In this case, I created it so you need to copy the EvenOdd declaration to your source file.

    Regards,
    Jim

  4. #29
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    Quote Originally Posted by jim829 View Post
    Okay, let's make this simple. Here is an interface.

    interface EvenOdd {
    boolean isEven(int v);
    }

    Create a small class that implements the interface and returns either true if the integer is even or false if it is odd. To test it, simply call the method with a number.

    The only difference structure wise between this and the Comparable interface is that the Comparable interface is provided by the JDK API so all you have to do is implement it.

    In this case, I created it so you need to copy the EvenOdd declaration to your source file.

    Regards,
    Jim
    Okay so i made one main class, interface and the class that does everything for me :

    main:
    public class tets {
     
    	public static void main(String[] args) {
    		EvenOdd c = new small();
    		System.out.println(c.isEven(3));
    	}
     
    }

    interface:

    interface EvenOdd {
    	boolean isEven(int v);
     
    }

    class:
    public class small implements EvenOdd {
     
    	public boolean isEven(int v) {
    		if (v % 2 == 0) {
    		return true;
    		}
    		return false;
    	}
     
    }


    This kinda makes sense but my own code doesn't make sense haha.
    Last edited by MrLowBot; January 20th, 2019 at 05:19 AM.
    "Tick, tack"

  5. #30
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    And also, been looking at the comparable from java.
    It has it's own method yet i don't know how to use it nor do i know
    if i should call all my methods from "TheMainCity" --> "Interface" --> "CityImplementedWithComparable".
    "Tick, tack"

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading txt

    Here's another example:
    import java.util.*;
     
    public class InterfaceExample {
       // Define the interface with a method
       interface TalkToMe {
          public void speak();   
       }
     
       // Define some classes that implement the interface
       class Dog implements TalkToMe {
          public void speak() {
             System.out.println("bow wow");
          }
       }
       class Cat implements TalkToMe {
          public void speak() {
             System.out.println("Meow");
          }
       }
       class Bird implements TalkToMe {
          public void speak() {
             System.out.println("Tweet");
          }
       }
     
       //-------------------------------------------------------
       InterfaceExample() {
          // Define a list to hold objects that implement the TalkToMe interface
          List<TalkToMe> ttmList = new ArrayList<>();    
          Cat cat = new Cat();
          ttmList.add(cat);
          TalkToMe ttm = new Dog();
          ttmList.add(ttm);
          ttmList.add(new Bird());
     
          // iterate the list and call the interface's method
          for(TalkToMe x : ttmList) {
             callSpeak(x);
          }
       }
     
       //  Handle TalkToMe objects
       void callSpeak(TalkToMe ttm) {
          ttm.speak();
       }
     
       public static void main(String[] args) {
          new InterfaceExample();
       }
    }
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    Think that i got this now:

    Main:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Comparator;
     
    public class cityMain {
    	private static BufferedReader r;
    	public static ArrayList<City2> City = new ArrayList<City2>();
     
    	public static void main(String[] args) {
    		int Z = 0;
    		String C = null;
    		try {
    			FileReader file = new FileReader("C:\\Users\\karwa\\Desktop\\BB.dat");
    			r = new BufferedReader(file);
    			String currLine;
    			while ((currLine = r.readLine()) != null) {
    				if (currLine.trim().length() > 0) {
    					String[] split = currLine.split(";");
    					Z = (Integer.parseInt(split[0]));
    					C = (split[1]);
    				}
    				City2 d = new City2(Z, C);
    				City.add(d);
    			}
     
    			City.sort(Comparator.comparing(City2::getZipCode));
    			for (int i = 0; i < cityMain.City.size(); i++) {
    				System.out.println(cityMain.City.get(i).getZipCode() + " " + cityMain.City.get(i).getCityName());
    			}
     
    		} catch (IOException e) {
    			System.out.println("Erorr : " + e);
    		}
     
    	}
     
    }

    City2:
    public class City2 implements Comparable<City2> {
     
    	private int zipCode;
    	private String cityName;
     
    	public City2(int zipCode, String cityName) {
    		this.zipCode = zipCode;
    		this.cityName = cityName;
    	}
     
    	public int getZipCode() {
    		return zipCode;
    	}
     
    	public String getCityName() {
    		return cityName;
    	}
     
    	@Override
    	public int compareTo(City2 city) {
    		return this.getZipCode() - city.getZipCode();
    	}
     
    }
    I get the correct answers back and using the "compareTo" method.
    "Tick, tack"

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading txt

    public static ArrayList<City2> City = new ArrayList<City2>();

    Variable names should begin with a lowercase letter: city vs City
    The name of a list should reflect what is in the list. cities would be a list of cities, city is singular and does not represent a list.

    using the "compareTo" method.
    Are you sure that method is ever used or called? Add a print statement to show that it is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    Quote Originally Posted by Norm View Post
    public static ArrayList<City2> City = new ArrayList<City2>();

    Variable names should begin with a lowercase letter: city vs City
    The name of a list should reflect what is in the list. cities would be a list of cities, city is singular and does not represent a list.


    Are you sure that method is ever used or called? Add a print statement to show that it is executed.
    AH hell it is not being called! And nothing is coming out of it because it isnt getting called... Not sure of what to do now.
    Tried with a print statement but all i get is five zeros on a line one after eachother.


    Print looks like this:

    0
    0
    0
    0
    0
    0
    0
    23642 Höllviken
    35243 Växjö
    51000 Jönköping
    72211 Västerås
    75242 Uppsala
    90325 Umeå
    96133 Boden


    Is doing something... it is doing "1-1"... basically.
    But it has override too... i don't get it?!
    Last edited by MrLowBot; January 20th, 2019 at 12:02 PM.
    "Tick, tack"

  10. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading txt

    all i get is five zeros
    What print statement creates those zeros? Please post the code.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    This is from the main :
    	while ((currLine = r.readLine()) != null) {
    				if (currLine.trim().length() > 0) {
    					String[] split = currLine.split(";"); // We tell it where it should split.
    					Z = (Integer.parseInt(split[0])); // We one value of the integers.
    					C = (split[1]); // Same with the characters.
    					City2 d = new City2(Z, C); // We put them in the other class.
    					cities.add(d); // We also add them in the array list for "City2".
     
     
    					System.out.println(d.compareTo(d)); //This part right here prints out Zeros.. But i cannot put anything else in the "d" spot of compareTo. 
     
     
     
    				}
    			}
    "Tick, tack"

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading txt

      d.compareTo(d)
    Look at what the compareTo method does. It subtracts two values and returns the results. Where do the two values come from?
    They both come from the object referenced by d. A value minus itself will give 0


    I was suggesting that the print statement be placed inside of the compareTo method to show if it was being called by the sort method.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    Well it is not being called. Just checked with a print inside the method.
    "Tick, tack"

  14. #39
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading txt

    it is not being called.
    Ok, why not? Where in you code is it supposed to be called?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #40
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Reading txt

    Quote Originally Posted by Norm View Post
    Ok, why not? Where in you code is it supposed to be called?
    No idea tbh. I never thought about it and was caught up with just trying to get the right values out.
    But now i want the compareTo to work. Just don't know of how to make that happen.
    "Tick, tack"

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Reading txt

    how to make that happen
    Use a sort method that takes a list/collection of objects that implement the Comparable interface. Read the API doc for the different sort methods.

    http://docs.oracle.com/javase/8/docs/api/index.html
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Reading lines from a txt file
    By neliJav in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 6th, 2013, 01:54 PM
  2. reading .txt files
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 8th, 2012, 12:47 PM
  3. reading and witing to txt file
    By nautilusz in forum File I/O & Other I/O Streams
    Replies: 25
    Last Post: January 22nd, 2012, 02:02 PM
  4. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 07:49 PM
  5. Reading .txt and storing into an array
    By vluong in forum Collections and Generics
    Replies: 1
    Last Post: January 4th, 2010, 02:07 PM