CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 47
  1. #31
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    var "s" was never given a type.
    Yes, it was. It's type is String. Look at the highlighted line in the image in post#30.
    It shows a definition for s of type String.

    There are some potential problems with the that line:
    1) local variables inside of a method must be given values before they are used.
    2) a variable local to a method is not known outside of that method
    Norm

  2. #32
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    then how to I enter a String, pass it to Bytes. and then record it into the file with dos?
    do you know how to do it?

  3. #33
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    Did you try the code I posted in post#11.

    I used that code with several different methods of the DataOutputStream class and got different results.
    You should write a small test program using that code with different methods to see which output what you want.
    Norm

  4. #34
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    Did you try the code I posted in post#11.

    I used that code with several different methods of the DataOutputStream class and got different results.
    You should write a small test program using that code with different methods to see which output what you want.
    I dont know how to test it.
    I created a new project, pasted your code and it was giving errors. probably needed to import something. but I dont get what does that has to do with what im looking for.
    please, I barely know Java.

  5. #35
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    pasted your code and it was giving errors
    Yes, you would need to define a class and a method and import the classes used in the code.

    what does that has to do with what im looking for.
    You need to find what method to use. The code I posted will show you what a method writes out. You need to try the different methods to find the one that writes out what you want. Compile and execute that code. If the output is what you want then that shows you what method to use. If the output is not right, change the method to another method, compile and execute to see if that method's output is better. If not, change the method and do it again. That is what I tried. I saw several different outputs. Some of them I posted earlier.
    Norm

  6. #36
    Join Date
    Nov 2015
    Posts
    37

    Lightbulb Re: how to write Strings into a .dat

    ok. I got the simplified idea.
    I enter the String by kb. Store it in an Array (vector). then write it on the .dat with writeChars:
    added "import java.util.Arrays;"
    Code:
    private static String[] filename;
    [...]
    System.out.print("Enter Filename:"); //path
    filename = sc.nextLine();
    dos.writeChars(filename);
    Name:  frfrfrg.jpg
Views: 40
Size:  11.9 KB
    please tell me how to fix that.

  7. #37
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    Why are you using an array? Read the user's input into a String and use a method to write that String to the file.
    Norm

  8. #38
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    Why are you using an array? Read the user's input into a String and use a method to write that String to the file.
    and if I use String.toByte(), how would that be?
    can you write an example?

  9. #39
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    I thought I did in post#11. That shows the use of one of the DataOutputStream class's methods and its output. You need to try some of the other methods to see if any of them write out the bytes you want.

    Here is the code again with small changes:
    Code:
          ByteArrayOutputStream baos = new ByteArrayOutputStream();  // so you can see what was written
          DataOutputStream dos = new DataOutputStream(baos);
          String aStr = "1234";
          dos.writeChars(aStr);      // [0, 49, 0, 50, 0, 51, 0, 52]    <<<  2 bytes / char
          dos.close();
          System.out.println(Arrays.toString(baos.toByteArray()));  // show what was written
    Last edited by Norm; December 1st, 2015 at 12:24 PM.
    Norm

  10. #40
    Join Date
    Nov 2015
    Posts
    37

    Exclamation Re: how to write Strings into a .dat

    Name:  frfrfrg.jpg
Views: 42
Size:  34.8 KB

    how do I assign sc.nextWHATEVER() via keyboard to a variable? cuz it skips the filename entry.

  11. #41
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    The Scanner class's next... methods leave the lineend character in it buffer. The nextLine method reads the lineend and returns an empty line. You need to call nextLine after calls to any other next... method to read the empty line before using nextLine to read a String from the console. Do a internet search for more details.
    Norm

  12. #42
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    I've fixed the scanners. getting an error here:
    arrayNameData[i] = sc.next(); // writing NameData[i] (line 52)

    Code:
    package examen5ingles;
    import java.util.Scanner;
    import java.util.Arrays;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.EOFException;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.StringWriter;
    public class crear {
    	
    	public static void main(String[] args) {
    
    		    Scanner sc = new Scanner(System.in);
    	        FileOutputStream fos = null;
    	        DataOutputStream dos = null;
    
    	        int t;  //# custom fields (1byte)
    	        int c;
    	        int i;  //  FOR counter
    	        int r; // # registers
    	        int Camp=0; // fulled fields counter
    	        int k;
    	        String filename;
    	        
    	        try {
    	            fos = new FileOutputStream("C:/Dev-Pas/EXAMEN444.dat");
    	            dos = new DataOutputStream(fos);
    
    	            System.out.print("Enter Serial #:"); //serial
    	            dos.write(sc.nextShort());
                    
    	            System.out.print("Enter Filename:"); //path
    	            filename = sc.next(); //scan.next() reads one word
    	            dos.writeChars(filename);
    	            sc.nextLine();
    
    	            //Date.valueOf(LocalDate.of(2, 2, 2));// date here
    	            //  dos.writeInt(d);
    
    	            System.out.print("Enter the amount of customized fields:"); //campos custom
    	            t = sc.nextInt();
    
    	            String [] arrayNameData = new String [t];
    	            
    	            for(i=1; i<t+1; i++) //code 1,2,3,4,etc.
    	            {
    	            
    	              System.out.print("Enter the name of field "+i+":"); //name,tel,email,adress.etc
    	              arrayNameData[i] = sc.next(); // writing NameData[i] 
    	              dos.writeChars(arrayNameData[i]);
    	              sc.nextLine();
    	            }
                
    	            System.out.print("Enter the total # of Contacts:"); // # of registers with name,tel,.etc each
    	            r = sc.nextByte();
    	            dos.writeByte(r);
    	            
    	            String [] arrayTextData = new String [r]; //array that holds the actual data
    	            
    	            System.out.print("Enter the"+r+"Contacts:"); 
    	            for(i=1; i<=r; i++)  //first Contact to the last
    	            {
    	            	Camp=0; //fulled_fields_counter = 0
    	            	
    	             for (k=1; k<=t; k++)  //from the first custom field to the last, that is "t"
    	                {
    	                 System.out.print("Enter"+arrayNameData[k]); //exmpl: "Enter Email"
    	                 arrayTextData[k]=sc.next(); //exmpl: user enters Email
    	                 if (arrayTextData.length != 0) //user might just enter nothing
    	                 {
    	                   Camp=(Camp+1);
    	                 }
    	                }
    	            }
    	            dos.writeInt(Camp);
    	                                     
    	            for (c=1; c<t+1; c++)  //cant campos custpm
    	            {
    	             //arrayNumData[c]=c;
    	            	
    	             if (arrayTextData.length != 0)
    	             {
    	              //dos.writeInt(arrayNumDato[c]);
    	              dos.writeBytes(arrayTextData[c]); 
    	             }
    	            }
    	            Camp=0;  //FIN
    	            
    
    	        } catch (FileNotFoundException e) {
    	            System.out.println(e.getMessage());
    	        } catch (IOException e) {
    	            System.out.println(e.getMessage());
    	        } finally {
    	            try {
    	                if (fos != null) {
    	                    fos.close();
    	                }
    	                if (dos != null) {
    	                    dos.close();
    	                }
    	            } catch (IOException e) {
    	                System.out.println(e.getMessage());
    	            }
    	        }
    	    }
    	}

  13. #43
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    getting an error here:
    Please copy the full text of the error message and paste it here. It has important info about the error.
    Norm

  14. #44
    Join Date
    Nov 2015
    Posts
    37

    Re: how to write Strings into a .dat

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at examen5ingles.crear.main(crear.java:52)

  15. #45
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: how to write Strings into a .dat

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at examen5ingles.crear.main(crear.java:52)
    At line 52 the program used an index (2) that was past the end of the array. The array has less than 3 elements.
    Remember array indexes range from 0 to the array's length-1
    The code should make sure the index is not past the end of the array.
    Norm

Page 3 of 4 FirstFirst 1234 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured