Reading Variables And Their Values From Files?
Apr 13, 2014
So here is my code:
while (key != 3) {
if (key == 2) {
System.out.println("Input from a file");
// System.out.print("Enter file name: ");
// String str = expression.nextLine();
int i = 0;
File file = new File(
[code]....
What I need to happen is the file gets read in, the file is in the form:
a * ( b + c ) / 2 + ( 8 * b )
a = 5
b = 10
c = 20
( x + y ) * z
x = 13
y = 21
z = 3
And so on. My code is supposed to start at the beginning and have output like:
Infix: a * b ( b + c ) / 2 + ( 8 * b )
variable values:
a = 5
b = 10
c = 20
**It then needs to store the variable names along with their values in a hash table**
postfix: a b c + * 2 / 8 b * +
value: 155
And it continues reading the file in the same fashion.
My issue is I am not properly extracting the variables and their values from the file. I have the variables in an array list actually, so I have those, but don't know how to efficiently navigate to the values, then store the character and value in a hash table, while protecting against possible blank lines within the file. Thus just calling nextLine()s and next()s wouldn't work to get to values because they would lead to an error.
View Replies
ADVERTISEMENT
Aug 19, 2014
I have an XML document that have multiple levels of nesting:
<virtue name="Innocence">
<rank ranklvl="1">
<stat type="Physical Mitigation" value="84" />
<stat type="Resistance Rating" value="89" />
<stat type="Tactical Mitigation" value="29" />
</rank>
<rank ranklvl="2">
[Code] ....
I need to return all the different values within each child element... i.e.
Virtue: Innocence
Rank: 1
Physical Mitigation = 84
Physical Rating = 89
Tactical Mitigation = 29
Virtue: Innocence
Rank: 2
Physical Mitigation = 162
Physical Rating = 175
Tactical Mitigation = 54
I can read the XML okay but I get an error on displaying the information.
for (int temp = 0; temp < nList.getLength(); temp++) {
//Node nNode = nList.item(temp);
Node nNode = nList.item(temp);
//System.out.println("
Current Virtue :" + nNode.getNodeName());
[Code] ....
View Replies
View Related
Mar 31, 2015
I have a multi scene JavaFX FXML application [URL] ....
I have added a TextField to Scene2.fxml and Scene3.fxml files each scene has its own Controller Class with a ScreensController class that loads a HashMap with Id name, Node screen that is defined in the ScreensFramework
class (Main class)
public void addScreen(String name, Node screen) {screens.put(name, screen);}
So each time you click a Button on a screen (scene) you fire an ActionEvent and move to another screen
myController.setScreen(ScreensFramework.screen2ID);
What I would like to do if it is possible is use the value in the TextField on Sceen2 and transfer it to the TextField on Scene3. I have discovered that unless both FXML files are loaded this is so far not possible. This is a desktop application. So how do you create a variable that is GLOBAL and has a life after one class is unloaded or one FXML file is unloaded?
At this point I do not want a database to accomplish this task.I have developed in Visual Basic 6 where I would just declare a global variable that could be used through out the application.
View Replies
View Related
Feb 6, 2015
I know that I need to use substrings, but I'm not sure how to implement them. All of the names are red in from the file "employees.txt".
Using the file Employees.txt
1) Scan the file and create user ids for each person according to the following scheme
a) Take the first and last character of the first name, convert them to upper case
b) Take the first and last character of the last name, convert them to upper case
c) Concatenate the results from a and b
i) Example: John Brown would yield => JNBN
d) Add the length of the first name to the length of the last name, disregarding spaces
e) Concatenate the result from step c to the result of step d
i) Example: John Brown would yield => JNBN9
f) Concatenate a four digit number to the end of this.
i) The numbers should begin with 0000 and increment by 1
ii) Example: If John Brown is the fifth name in the list the resulting UserID would be => JNBN90004
2) Display the user ids in four columns with a width of 14
View Replies
View Related
May 17, 2014
I have copied a wav file as explained in [URL] ..... When trying to play my new file, it runs but no sound is hear. My reading and writing class is as explained in another post in the site:
This is the function that reads the file:
// read a wav file into this class
public boolean read(DataInputStream inFile) {
myData = null;
byte[] tmpInt = new byte[4];
byte[] tmpShort = new byte[2];
[Code] ....
I can't hear the copied wav file, but still the wav file is correctly copied...
View Replies
View Related
Nov 11, 2014
public class Tester
{
public static void main(String[] args)
{
Product p1 = new Product();
p1.loadFromFile("Monitor.pr");
System.out.println("Actual: " + p1.toString());
System.out.println("Expected: Product [id=12345, name=Monitor, description=A freakin great monitor!]");
p1.setId(11111);
p1.setName("another product");
p1.setDescription("Description of another product!");
[code]....
The expected output is what my program should be doing and the actual output is what it's doing instead. As you can see, my code works for the middle test. But I cannot understand why it won't load the first fileFor the second half, the id and the quantity are separated by a comma... I'm not really sure how to deal with that. As you can see, I tried using a delimiter, but it doesn't seem to be doing any good.
View Replies
View Related
May 26, 2014
So I have a text file, and I want my Java program to store names from the text file. How do I do that? This is what I have so far.
Java Code: import java.util.Scanner;
import java.io.File;
BingoCard[] cards = new BingoCard[n]; //Array of BingoCard objects, n being the length of the Array
Scanner sc = new Scanner(File("Names")); //Names is the name of the file
for(int c = 0; c < cards.length; c++)
cards[c] = new BingoCard(sc.nextLine); mh_sh_highlight_all('java');
Here's the constructor.
Java Code: private string myName;
public BingoCard(String name)
{
myName = name;
} mh_sh_highlight_all('java');
Whenever I compile, I get this error message.
View Replies
View Related
Nov 1, 2014
I just can't seem to figure it out how to solve this inherently recursive assignment.The task is to check a directory path and read the files within that path; also, if there are more directories within it, we have to go deeper into those directories to read the files within them - if any. What you're looking at is my skeleton of the assignment:
public class SearchingForStrings {
public static void main(String[] args) {
String path = "."; // Default
File sf = new File(path);
String mainDirectory = args[0]; // These two are just
String keyString = args[1]; // command-line arguments
countLinesWithString(sf, mysteriesDirectory, keyString);
[code]....
View Replies
View Related
Apr 17, 2015
have to create a file named Lab13.txt. In the file I have 10 random numbers. I have to import the 10 numbers and have to Multiply all the numbers from Lab13.txt by 10 and save all the new numbers a new file named Lab13_scale.txt. so if the number 10 is in lab13.txt it prints 100 to Lab13_scale.txt. how do i get it to Multiply Here is what I have:
Java Code: import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
[Code].....
View Replies
View Related
Jul 24, 2014
I want to read about a 100 files. There are 4 Categories of the files. I also want to create a Database of these files. The 4 Categories are my Columns. My question is how to it. I know what i want to do but not how to write in code.
View Replies
View Related
Apr 10, 2015
how to use a Scanner to read data from a file. Is it possible to also average a set of numbers at the same time its being read?
View Replies
View Related
Mar 21, 2015
Lets say I have a desire to convert and entered value to Centigrade or Fahrenheit and that I have two Radio Buttons to designate the type of conversion with the Answer posted to a TextField on Scene Two from a button click event on Scene One ok Now I would like to convert the value in the TextField on Scene Two to Kelvin and reflect the value on Scene Three in a TextField by clicking a button on Scene Two
lets say I have 4 FXML files sceneONE sceneTWO and scenMain and I created 4 controllers ControllerONE and MasterController
So far I can navigate to the different scens with just the MasterController code below...
BUT because I have two FXML files I can not capture the value in the first FXML file and pass it to the second scene
This project is my attempt to adapt the FXML style code from this web site
Switching to Different Screens in JavaFX and FXML | JavaFXTutorials
Which uses this code to switch scenes
//get reference to the button's stage stage=(Stage) btnS1.getScene().getWindow();
//load up OTHER FXML document
root = FXMLLoader.load(getClass().getResource("ATTwo.fxml"));
But will not permit variable transfer due to scope of variables which reside in two different FXML files
public class MasterController implements Initializable{@FXML public Button btnS1;
@FXML public Button btnS2;
@FXML public Label lblS1;
@FXML public Label lblS2;
@FXML public TextField txfS1;
@FXML public TextField txfS2;
@FXML public AnchorPane root;
[Code] .....
View Replies
View Related
May 4, 2015
In the program below I'm trying to read the contents of several .txt files in the same diretory and create a new .txt file containing all of the data from each file. My output is generated in the console however my .text file is blank.
public static void main(String[] args) throws IOException {
String target_dir = "C:Files";
String output = "C:Filesoutput.txt";
File dir = new File(target_dir);
File[] files = dir.listFiles();
[Code] .....
View Replies
View Related
Mar 23, 2015
I am facing difficulties in identifying the proper way to add the selected colors of cards into an arraylist. I am having an arraylist which is:
Java Code: private List<String> selectedCars = new ArrayList<String>(); mh_sh_highlight_all('java'); And one more for the carColors:
Java Code: private List<String> carColors = new ArrayList<String>(); mh_sh_highlight_all('java');
The selectedCards array will store the selected cars ['TOYOTA','MAZDA','NISSAN'] as per the selection from the user.For certain types, there is one default color which is 'Black', however for some of them, the user can select different colors.(ex. if selection is 'Toyota')
Java Code:
String carColor="";
String toyotaColor=""; // The value will be retrieved from the form once the user selected the color
if (selectedCars.contains("MAZDA"))
{
carColor="Black";
}
else if (selectedCars.contains("TOYOTA"))
{
carColor=toyotaColor;
[code]....
View Replies
View Related
Oct 14, 2014
I am currently building a Plugin system for an application and I wanted to add a little security feature.
I have read many tutorials on this and they basically all came down to the same answer:
"Its complicated: You need a custom class loader and security manager and ... and even then its still tricky"
So I thought, before I load the classes dynamically I will simply read the class files. Why bother with a SecurityManager or ClassLoader if I can simply whitelist all allowed classes and search for all illegal Class access before I even load anything.
View Replies
View Related
Jun 13, 2014
I am able to get output from my constructor when I place a loop inside of it. However when I try to access the private variable that I thought was set by the constructor I get nothing. When I check its size it is zero as well.
Java Code:
public class WinningHand extends PokerCalculator {
private int p1Size;
private int p2Size;
private String[] p1Hand = new String[p1Size];
private String[] p2Hand = new String[p2Size];
[Code] ....
View Replies
View Related
Aug 21, 2014
why we are not able to read second string in this program.
String s1,s2,s3;
String decider;
Scanner sc = new Scanner(System.in);
[Code].....
Note: If I uncomment line num 9 it works fine
View Replies
View Related
May 4, 2015
How can I Cache the data I'm reading from a collection of text files in a directory using a TreeMap? Currently my program reads the data from several text files in a directory and the saves that information in a text file called output.txt. I would like to cache this data in order to use it later. How can I do this using the TreeMap Class? These are the keys,values: TreeMap The data I'd like to Cache is (date from the file, time of the file, current time).
import java.io.*;
public class CacheData {
public static void main(String[] args) throws IOException {
String target_dir = "C:Files";
String output = "C:Filesoutput.txt";
File dir = new File(target_dir);
File[] files = dir.listFiles();
[Code] ....
View Replies
View Related
Apr 29, 2015
In the current program I am trying to read multiple text files from a folder. I keep getting the following syntax error
"Syntax error on token ";", { expected after this token".
I highlighted the line where im getting this error in red.
import java.io.*;
import java.util.*;
public class CacheData {
// Directory path here
String path = "C:myfiles*.txt";
[Code] ....
View Replies
View Related
Apr 8, 2014
I've been trying to learn how to read in file input and have a question about this piece of code:
Java Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class readFile {
public static void main(String args[]) throws FileNotFoundException{
File file = new File("file.txt");
int sum[]=new int[5];
int i=0;
[Code] .....
"file.txt" holds the following information:
1
2
88
42
56
89
But my output looks like:
2
42
89
if I take out sum[i]=input.nextInt(); it will display all values in the file.
View Replies
View Related
Feb 4, 2014
Have 2 files File1.csv and file2.csv containing some values. Have to read both files and write unique values in to another file.
In the new the there must be unique rows and not duplicate rows (2 files contain some rows same). What is the best approach for doing this?
View Replies
View Related
Mar 8, 2014
My code's not working and I don't know why. I'm trying to read numerical values from a file and saving all instances where a letter is entered instead of a number to a string to be referenced as an error at a later point on in the code. However, there's an error and like I've stated before, I don't know what caused it.
public static void validateData() throws IOException{
File myfile = new File("gradeInput.txt");
Scanner inputFile = new Scanner(myfile);
for (int i=0; i<33; i++){
if(inputFile.hasNextDouble()){
double d = inputFile.nextDouble();
if (d<0||d>99999){
[Code] ....
This is the error that returns.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at Paniagua_Grading.validateData(Paniagua_Grading.java:29)
at Paniagua_Grading.main(Paniagua_Grading.java:6)
View Replies
View Related
Sep 13, 2014
//compiler error I'm receiving
J:CS3Student Folder Review Lab #2APlusImage.java:41: error: cannot find symbol
xcor.add(read.nexInt());
[Code].....
View Replies
View Related
Feb 3, 2014
I just want to be able to read type and weight in the Letter class I created (I created read out messages to check in the Letter class). I am able to read it with in the Mail class.
package org.mailprice.postage;
import java.util.Scanner;
public class Mail {
static Letter letter;
[code]....
View Replies
View Related
Oct 26, 2014
What step to know to develop software..
View Replies
View Related
Apr 13, 2015
In a program I created, I'm using a text file that contains some texts needed for the program. The method relevant to this is something like the following.
private String wordgen(){
try {
BufferedReader reader = new BufferedReader(new FileReader("src/Resources/adjectives.txt"));
Random rand = new Random();
int low = rand.nextInt(400);
String fil="";
int i=0;
while(i!=low){
[Code]...
The program runs fine in netbeans project but once the jar is created it does not corporate with the text file. ("null" is returned) How can I attach text files to jar and exe?
View Replies
View Related