Results 1 to 1 of 1
  1. #1
    Java Dude is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Post HashMap with Objects, reading from file, writing to file. (note system)

    Hello! Im new to the forum, and pretty new to java programming and this forum. I dont exactly know where to post it, so it should be fine here untill some admin/mod moves it to a more correct section.

    I am trying to make a note-system for myself so i can store notes about different philosops. I want to be able to write a philosop name and store it into a .txt file named navn.txt i also want to store notes about philosops which i write into the notat.txt file (sorry for not having english variables).

    Okey now the reading part works fine. the new problem is :
    - When i try to add new names i get this problem. It does store the new name but the system crashes, so i have to start the java index over
    Java Code:
    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.Scanner.throwFor(Scanner.java:907)
    	at java.util.Scanner.next(Scanner.java:1530)
    	at java.util.Scanner.nextInt(Scanner.java:2160)
    	at java.util.Scanner.nextInt(Scanner.java:2119)
    	at NotSys.menyValg(index.java:169)
    	at NotSys.meny(index.java:164)
    	at NotSys.menyValg(index.java:172)
    	at NotSys.meny(index.java:164)
    	at NotSys.menyValg(index.java:179)
    	at NotSys.meny(index.java:164)
    	at index.main(index.java:7)
    Java Code:
    import java.util.*;
    import java.io.*;
    
    class index {
    	public static void main(String[] args) {
    		NotSys s = new NotSys("navn.txt","notat.txt");
    		s.meny();
    	}
    }
    class Name {
        String nkey;
        String name;
        String lastname;
        HashMap <String, Note> mnote = new HashMap<String, Note>();
        
        Name(String nkey, String name, String lastname) {
        	this.nkey = nkey;
        	this.name = name;
        	this.lastname = lastname;
        }
        
        void addNote(String nkey, String note) {
        	String kkey = nkey+"-"+note;
        	Note n = mnote.get(kkey);
        	if(n == null) {
        		n = new Note(nkey, note);
        		mnote.put(nkey, n);
        	}
        	else {
        	//	n.oppdater(nkey, note);
        	}
        }
    }
    
    class Note {
        String nkey;
        String note;
        Note(String nkey, String note) {
        	this.nkey = nkey;
        	this.note = note;
        }
        /*
        void oppdater(String nkey, String note) {
        	if(nkey == null) {
        		nkey
        		
        	}
        }
        */
    }
    
    class NotSys {
        HashMap <String, Name> mname = new HashMap <String,Name>();
        File fil1;
        File fil2;
        
        NotSys(String ffil1, String ffil2) {
        	fil1 = new File(ffil1);
        	fil2 = new File(ffil2);
        	if(fil1.exists()) {
        		try {
        		    Scanner sc = new Scanner(fil1);
        		    sc.useDelimiter("[;\n\r]");
        		    while(sc.hasNext()) {
        		    	//  key;name;lastname (key = name+"-"+lastname)
        		    	// .txt Myname-Mylastname;Myname;Mylastname
        		    	String nkey = sc.next();
        		    	String name = sc.next();
        		    	String lastname = sc.next();
        		    	mname.put(nkey, new Name(nkey, name, lastname));
        		    }
        		    sc.close();
        		}
        		
        		catch (Exception e) {
        		    System.out.println("Error: "+e.getMessage());
        		}
        	}
        	
        	if(fil2.exists()) {
        		try {
        		    Scanner sc = new Scanner(fil2);
        		    sc.useDelimiter("[;\n\r]");
        		    while(sc.hasNext()) {
        		    	// key;note
        		    	//.txt Myname-Mylastname;Note text bla bla bla bla
        		    	String nkey = sc.next();
        		    	String note = sc.nextLine();
        		    	Name name = mname.get(nkey);
        		    	name.addNote(nkey, note);
        		    	sc.close();
        		    }
        		}
        		catch (Exception e) {
        		    System.out.println("Error: "+e.getMessage());
        		}
        	}
        }
         
        void addName(){
    	System.out.println();
    	System.out.println("#### New name ####");
    	try{
    	    FileWriter f = new FileWriter(fil1, true);
    	    Scanner sc = new Scanner(System.in);
    	    System.out.print("Name: ");
    	    String name = sc.next();
    	    System.out.print("Lastname: ");
    	    String lastname = sc.next();
    	    String nkey = name+"-"+lastname;
    	    f.write(nkey+";"+name+";"+lastname+"\n");	    
    	    f.close();
    	    sc.close();
        	mname.put(nkey, new Name(nkey, name, lastname));
    	}
    	catch (Exception e) {
    	    System.err.println("Error: "+e.getMessage());
    	}
        }
        void addNote(){
    	try {
    	    FileWriter f = new FileWriter(fil2, true);
    	    Scanner sc = new Scanner(System.in);
    	    System.out.print("Note: ");
    	    String note = sc.nextLine();
    	    System.out.print("Name-Lastname: ");
    	    String name = sc.next();
    	    f.write(name+";"+note+"\n");
    	    f.close();
    	    sc.close();
    	}
    	catch (Exception e) {
    	    System.err.println("Error: "+e.getMessage());
    	}
        }
        
    
        void allName() {
    	Iterator<Name> it = mname.values().iterator();
    	int a = mname.size();
    	String[] sortname = new String[a];
    	int t = 0;
    	while (it.hasNext()) {
    	    Name n = (Name) it.next();
    	    sortname[t] = n.nkey;
    	    t++;
    	}
    	java.util.Arrays.sort(sortname);
    	System.out.println("Name Lastname Key");
    	for(int j = 0; j < t; j++) {
    	    Name n = (Name) mname.get(sortname[j]);
    	    System.out.println(n.name+" | "+n.lastname+" | "+n.nkey);
    	}
        }
    
        void meny() {
    	System.out.println("");
    	System.out.println("");
    	System.out.println("#### MENY ####");
    	System.out.println("(1) Add new name");
    	System.out.println("(2) Add new notes");
    	System.out.println("(3) Show all names");
    	System.out.println("(10) Exit");
    	menyValg();
        }
    
        void menyValg() {
    	Scanner sc = new Scanner(System.in);
    	int valg = sc.nextInt();
    	switch (valg) {
    	case 1 : addName();
    	    meny();
    	    break;
    	case 2 : addNote();
    	    meny();
    	    break;
    	case 3 :// visAlleNoteer();
    	    allName();
    	    meny();
    	case 10 : break ;
    	default :
    	    System.out.println("Error: Pick a number 1-3 // 10 = Exit!");
    	    meny();
    	}
    	sc.close();
        }
    }
    Last edited by Java Dude; 12-15-2012 at 06:21 AM. Reason: Update, made the variables methods and text to english, fixed old problem, new problem now.

Similar Threads

  1. Reading from and writing to file
    By dyelax in forum New To Java
    Replies: 3
    Last Post: 10-17-2012, 09:32 PM
  2. reading a file and writing to a file....help!!!!
    By java_prgr in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 07:53 PM
  3. Replies: 9
    Last Post: 10-20-2009, 11:52 AM
  4. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  5. Reading and writing to a file
    By jigglywiggly in forum New To Java
    Replies: 13
    Last Post: 03-09-2009, 11:44 AM

Posting Permissions

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