-
November 28th, 2015, 12:07 PM
#31
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
-
November 28th, 2015, 12:12 PM
#32
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?
-
November 28th, 2015, 12:15 PM
#33
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
-
November 28th, 2015, 12:18 PM
#34
Re: how to write Strings into a .dat
Originally Posted by Norm
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.
-
November 28th, 2015, 12:24 PM
#35
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
-
November 30th, 2015, 06:28 PM
#36
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);
please tell me how to fix that.
-
November 30th, 2015, 09:32 PM
#37
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
-
December 1st, 2015, 12:00 PM
#38
Re: how to write Strings into a .dat
Originally Posted by Norm
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?
-
December 1st, 2015, 12:21 PM
#39
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
-
December 1st, 2015, 06:25 PM
#40
Re: how to write Strings into a .dat
how do I assign sc.nextWHATEVER() via keyboard to a variable? cuz it skips the filename entry.
-
December 1st, 2015, 06:31 PM
#41
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
-
December 6th, 2015, 06:37 PM
#42
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());
}
}
}
}
-
December 6th, 2015, 07:22 PM
#43
Re: how to write Strings into a .dat
Please copy the full text of the error message and paste it here. It has important info about the error.
Norm
-
December 6th, 2015, 07:38 PM
#44
Re: how to write Strings into a .dat
Originally Posted by Norm
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)
-
December 6th, 2015, 07:47 PM
#45
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|