We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm writing a program that reads and writes string variables to a txt file. I figured out how to output the variable to a string using BufferedWriter, but figuring out the BufferedReader has been trickier as I found what I needed in a tutorial online (see code below); however, it's written in java and I can't figure out how to recode it excluding public static void main(String[] args) {} and using Processing's IDE (PDE) pre-processor to avoid the "Illegal modifier for the class FavsPopupFragment; only public, abstract & final are permitted" error.
Could someone help show me how this code example for BufferedReader works in processing?
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Buffers{
private static final double PERIODS = 6;
public static void main(String [] args) {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int X = 0, g = 1, tmpX = 0;
String oneLine;
System.out.println("====Calculate your GPA====\nScale A=4, B=3, C=2, D=1, F=0");
while(g <= PERIODS){
try{
System.out.println("Per."+g+": ");
oneLine = in.readLine();
tmpX = Integer.parseInt(oneLine);
X += tmpX;
System.out.println("Added "+tmpX+" to period "+g);
g++;
}catch(IOException e){
System.out.println("Problem reading");
}catch(NumberFormatException e){
System.out.println("That's not a number!");
}
}
System.out.println("Your GPA is: "+(X/PERIODS));
System.out.println("===========================");
}
}
Edit: Note for anyone looking to use the initial code example above: Just remove the line "public class Buffers{ " and the " }" at the end for Processing.
Answers
Take a step back and try to understand exactly what the code is doing. Don't just copy and paste code.
But generally, you could convert this into a class that Processing uses. Just rename
main()
to something else, likeprocessFile()
. Then it should work pretty much exactly the same.Or you could take this code and put it into your Processing sketch. Please try to understand what the keywords mean. But you should be able to pretty much copy all of this code, change a couple of keywords, and have it work.
I've been trying to understand the code, even tried using the examples from the BufferedReader references in the online manual but the examples from there don't work in Processing. I'm trying to get a working example from this so I can learn the structure and start utilizing it's functions. Thank you, KevinWorkman, for your response and it's good to know the code can work, but I'm still learning java and don't understand the class process language.
Could someone demonstrate a working code example for the code?
Start with something smaller. If you're just trying to read a text file, then try just reading the first line from that text file. Trying to copy a bunch of code is just going to give you a headache. Write just a couple of lines of code and then test them before moving on.
You should also check out the Processing reference. Here is a smaller example that uses a
BufferedReader
. You might also check out theloadStrings()
function here.Those are the online reference examples I tried to learn to simply utilize BufferedReader but couldn't figure out how to get them to work.
The code originally referenced above is apparently the only way to transfer the BufferedReader string to a variable (according to the tutorial), hence the request on how to restructure it in processing. It's the antipathy of the function I'm looking to utilize for my code. I can learn from a functional example.
I found it whilst trying to find BufferedReader examples in tutorials that actually work in processing to learn from, but now I can knock out two birds with one stone.
Let's take a step back. What do you mean by transferring a BufferedReader string to a variable? Isn't that exactly what this line does from the above example?
I mean using the BufferedReader setup to perform calculations of the numerical input from the data file, as opposed to retrieving strings which can't be used computationally.
Each time the file is read the string is retrieved as a number and used in the programs calculations.
Your example code is working with String values, but they're using the
Integer.parseInt()
function to convert from String to int. This is what I meant when I said it's a good idea to understand the code before trying to use it.You can use
Integer.parseInt()
(or better yet, Processing'sint()
function) with theloadStrings()
function just fine.I used your suggestions using just loadStrings() and int() but ran into trouble when the string from the text file needed to be used as float.
Working loadStrings() int() example:
So I tried using Integer.parseInt() and Integer.valueOf() to work with it but got the NumberFormatExceptions error.
I need to be able to process the text data as pure numerical data. That's why I'm using BufferedWriter in my program in the first place, for numerical data (w/decimals) to be transferred to a file with FileWriter. Then the file is supposed to retrieve the string and utilize it as a float without rounding the number.
Okay, so I figured out how to load strings from a file and input them into a float (without the NumberFormatExceptions):
This successfully retrieves the string and displays it into the console as a stand alone program.
Problem is that I don't know how to integrate it into my code because this code segment is a closed loop, once i = 2 the conditional loop closes and I lose access to the values calculated from the loop, and therefore their use as float, also lose access to variables outside this code segment loop while inside it.
Edit, Update: Recoded with a different method, BufferedReader, and get the same issue where I can't transfer the X float converted from the string out of the loop.
Define x outside the loop. Use an ArrayList. (See reference)
Add each parsed value to the ArrayList inside the loop.
x will now be available...
Thanks KevinWorkman and koogs for your help! Got my program set up now with the new functions.
Thanks for the final suggestion, koogs. I ended up finding this example of defining variables outside loops:
Then utilizing the While loop, didn't even need to use ArrayList since it's a simple import:
It's an infinite loop of importing and displaying to the console but this was just the working example. Now I know to never underestimate the usefulness of the Processing.org forum and stackOverflow for Q&A and java code examples.
terrible.
a tight loop reading from a file? how many times is that running? waste of cycles.
if you're only doing this once (i == 0) then you don't need a loop.
just use
println(x);
Before the reason why it utilized the tight loop is because I couldn't figure out why the Float.parseFloat() returned an error "ArrayIndexOutofBoundsException: 1" when not located inside the while loop.
But using your suggestions, I retried with int i=0 outside the condition and got this to work:
Changed it to an simple if statement, removing the loops. It's much more simplistic as that's all that's needed. Is that what you meant?