Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28
Like Tree1Likes

Thread: Reading numbers from text file!

  1. #21
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    131
    Rep Power
    0

    Default Re: Reading numbers from text file!

    Can`t this be done with the appropriate RegEX?

  2. #22
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Reading numbers from text file!

    Where does regex come into it?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #23
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Reading numbers from text file!

    I have the code in two separate classes, which are in two different files. Here is the class with the main method:

    Java Code:
    public class MainBinaryInsertionSort {
    	public static void main(String[] args) {
    
    		BinaryInsertionSort z = new BinaryInsertionSort();
    
    		z.openTextFile();
    		z.readTextFile();
    		z.closeTextFile();
    	}
    }
    And the class with all the methods that do all the hard work:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class BinaryInsertionSort {
    
    	private Scanner x;
    	ArrayList<String> studentIDS = new ArrayList<String>();
    
    	public void openTextFile() {
    		try {
    			x = new Scanner(new File("StudentInformation.txt"));
    		}
    		catch (Exception e) {
    			System.out.println("Error! Could not find file!");
    		}
    	}
    
    	public void readTextFile() {
    
    		while(x.hasNext()) {
    
    			String a = x.next();
    			String b = x.next();
    			String c = x.next();
    			String d = x.next();
    			studentIDS.add(d);
    
    			System.out.println(d);
    			}
    	}
    
    	public void closeTextFile() {
    		x.close();
    	}
    
    
    	String[] convertedStudentIDS = studentIDS.toArray(new String[studentIDS.size()]);
    	System.out.println(convertedStudentIDS);
    
    }
    So far I have managed to get all the ID #s into the ArrayList AND I have converted the ArrayList into an array. However, I am getting an error that is supposedly hinting at a mistake with printing out the converted array. This is the error I get:

    Java Code:
    .\BinaryInsertionSort.java:38: error: <identifier> expected
            System.out.println(convertedStudentIDS);
                              ^
    .\BinaryInsertionSort.java:38: error: <identifier> expected
            System.out.println(convertedStudentIDS);
                                                  ^
    .\BinaryInsertionSort.java:38: error: cannot find symbol
            System.out.println(convertedStudentIDS);
                               ^
      symbol:   class convertedStudentIDS
      location: class BinaryInsertionSort
    .\BinaryInsertionSort.java:38: error: cannot find symbol
            System.out.println(convertedStudentIDS);
                  ^
      symbol:   class out
      location: class System
    4 errors
    Press any key to continue . . .
    I am getting those errors when I try to compile. Please help!

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Reading numbers from text file!

    Please look very carefully. Is that line of code in a method? Is it in a constructor? Where exactly is that line of code located?

  5. #25
    Java Dude is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: Reading numbers from text file!

    Okey first of all, i had to change a bit on your .txt file since i couldnt make the double work with "." but only with ",".

    My english is not so good, so sorry for mistakes. And am very new at java myself and i am having a similare problem. So here is my try.

    Java Code:
    /* How the .txt file looks for me to make this work
    Mark;25;3,5;0789126
    Jessica;21;3,9;0769821
    Ashley;19;3,1;0791582
    Tom;18;3,7;0698751
    Brittney;22;3,2;0581943
    Kevin;25;3,4;0968242
    Joseph;25;3,1;0181793
    John;19;2,8;0908716
    Dimitri;23;3,0;0891769
    Kimberly;24;3,8;0981913
    Name;Age;GPA(use , not .);ID
     */
    
    import java.util.*;
    import java.io.*;
    /* Okey first compile it javac index.java
     * then to run it use java index yourtextfilename.txt
     * very important to add your .txt file name after index (or whatever u name ur mainclass)
     * and im very new to java so its probably easyer / faster / better ways to make this work
     * but this is what i know so i hope it helps you out somehow
     */
    
    class index {
    	public static void main(String[] args) {
    		// if we find 1 argument in the line then okey
    		if(args.length == 1) {
    			readFile s = new readFile(args[0]);
    			s.meny();
    		}
    		else {
    			// else big problem
    			System.out.println("Not correct amount of files!");
    		}
    	}
    }
    
    // The data class that recives the data later. 
    // with this class you will be able to do the rest of your homework. you just need to
    // add the methods you will need!
    class Data {
        String name;
        int age;
        double gpa;
        String id;   
        Data(String name, int age, double gpa, String id) {
        	this.name = name;
        	this.age = age;
        	this.gpa = gpa;
        	this.id = id;
        }
    }
    
    /* 
     * First of all your .txt file doubles cant be like 10.5 it has to be 10,5
     * so what i did is use something else to space the data ";" and not ",". 
     */
    class readFile {
    	// make a hashmap to store the textdata in
        HashMap <String, Data> hmap = new HashMap <String,Data>();
        File fil1;   
        // lets take the argument string "java index ..." and check if the 
        readFile(String ffil1) {
        	fil1 = new File(ffil1);
        	// file exists
        	if(fil1.exists()) {
        		try {
        		    Scanner sc = new Scanner(fil1);
        		    // use ";" "enter" and "return" as splitter
        		    sc.useDelimiter("[;\n\r]");
        		    // check the textfile
        		    while(sc.hasNext()) {
        		    	String name = sc.next();
        		    	int age = sc.nextInt();
        		    	double gpa = sc.nextDouble();
        		    	// i made the id to a string, so that the hashmap can use it as key
        		    	// you could also recive the id as int, and use hmap.put(""+id,...
        		    	// but i picked this 1, and the ID will only be used as a key so why not
        		    	String id = sc.next();
        		    	// put the data we read from the .txtfile into your hashmap.
        		    	hmap.put(id, new Data(name, age, gpa, id));
        		    	// (key, new Data(variables,variables,variables,variables))
        		    }
        		    sc.close();
        		}
        		
        		catch (Exception e) {
        		    System.out.println("Error: "+e.getMessage());
        		}
        	}
        }
        
        void allName() {
        	Iterator<Data> it = hmap.values().iterator();
        	// we use the Iterator to read the valueobjects (correct me if im wrong, new into java)
        	int a = hmap.size();
        	// use the hashmap data size as a (to make our array that will sort and print out the ids)
        	// in our example we used 10 names/lines of data, so a would be 10.
        	String[] sortid = new String[a];
        	//the array sortid is made, and its a int big [a]/[10]
        	int t = 0;
        	// we make a counter
        	while (it.hasNext()) {
        		//make connection and start counting and putting correct id to the array
        	    Data d = (Data) it.next();
        	    sortid[t] = d.id;
        	    t++;
        	}
        	java.util.Arrays.sort(sortid);
        	// we use the sorter.
        	System.out.println("Name - Age - GPA - ID");
        	for(int j = 0; j < t; j++) {
        		//for-loop to print out the data
        	    Data d = (Data) hmap.get(sortid[j]);
        	    System.out.print(d.name+" - ");
        	    System.out.print(d.age+" - ");
        	    System.out.print(d.gpa+" - ");
        	    System.out.println(d.id);
        	}
        }
         
        void meny() {
    	System.out.println("#### MENY ####");
    	System.out.println("(1) All name");
    	System.out.println("(9) Exit");
    	menyValg();
        }
    
        void menyValg() {
    	Scanner sc = new Scanner(System.in);
    	int valg = sc.nextInt();
    	switch (valg) {
    	case 1 : allName();
    	    meny();
    	    break;
    	case 10 : break ;
    	default :
    	    System.out.println("Error: Pick a number 1-x // 9 = Exit!");
    	    meny();
    	}
    	sc.close();
        }
    }
    Last edited by Java Dude; 12-15-2012 at 04:48 AM. Reason: CODE not QUOTE

  6. #26
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Reading numbers from text file!

    I got the compile-time error to disappear by altering the code a little. This is what I have now:

    Java Code:
    public class MainBinaryInsertionSort {
    	public static void main(String[] args) {
    
    		BinaryInsertionSort z = new BinaryInsertionSort();
    
    		z.openTextFile();
    		z.readTextFile();
    		z.closeTextFile();
    	}
    }
    And:

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class BinaryInsertionSort {
    
    	private Scanner x;
    
    
    	public void openTextFile() {
    		try {
    			x = new Scanner(new File("StudentInformation.txt"));
    		}
    		catch (Exception e) {
    			System.out.println("Error! Could not find file!");
    		}
    	}
    
    	public void readTextFile() {
    
    		ArrayList<String> studentIDS = new ArrayList<String>();
    
    		while(x.hasNext()) {
    
    			String a = x.next();
    			String b = x.next();
    			String c = x.next();
    			String d = x.next();
    			studentIDS.add(d);
    
    			System.out.println(d);
    
    
    			String[] convertedStudentIDS = studentIDS.toArray(new String[studentIDS.size()]);
    
    			System.out.println(convertedStudentIDS);
    			}
    	}
    
    	public void closeTextFile() {
    		x.close();
    	}
    }
    When I run it, however, I get gibberish:

    Java Code:
    0789126
    [Ljava.lang.String;@6d172f8f
    0769821
    [Ljava.lang.String;@d338d3d
    0791582
    [Ljava.lang.String;@337da690
    0698751
    [Ljava.lang.String;@7e425258
    0581943
    [Ljava.lang.String;@342c502a
    0968242
    [Ljava.lang.String;@49431028
    0181793
    [Ljava.lang.String;@51d9d7ab
    0908716
    [Ljava.lang.String;@34de4588
    0891769
    [Ljava.lang.String;@6ca6fed5
    0981913
    [Ljava.lang.String;@47a489ad
    Press any key to continue . . .
    I'm guessing the program fails to successfully convert the ArrayList to an array. Help?

  7. #27
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Reading numbers from text file!

    You're seeing the default toString() representation of an Array of String. To see the contents of the array, you could always use the Arrays class toString(...) method:

    Java Code:
      System.out.println(java.util.Arrays.toString(convertedStudentIDS));

  8. #28
    Asvin is offline Member
    Join Date
    Nov 2010
    Posts
    66
    Rep Power
    0

    Default Re: Reading numbers from text file!

    Awesome! It works perfectly! Thanks everyone for all your help! :)

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Reading a text file
    By dougie1809 in forum New To Java
    Replies: 23
    Last Post: 03-12-2012, 11:22 PM
  2. Reading file full of numbers, cannot add them together
    By quinnvanorder in forum New To Java
    Replies: 8
    Last Post: 02-26-2012, 03:28 AM
  3. Seaching a text file for credit card numbers
    By javamann in forum New To Java
    Replies: 9
    Last Post: 11-21-2011, 12:44 AM
  4. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  5. Reading a text file
    By diegosened in forum New To Java
    Replies: 4
    Last Post: 01-15-2010, 11:32 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
  •