• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Read multiple line text file extract decimal number values

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trying to read a text file with the following 3 separate lines, a = 5.5, b = 4.5, c = 3.5 and get the program to ignore the a =, b =, c = parts and only read in the decimal number values and put the decimal number values into the program variables a, b, c. Searched youtube found nothing. Do not know if it is possible.
 
Saloon Keeper
Posts: 2613
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Billy Briggs wrote:Do not know if it is possible.


It is possible - what have you got so far?
 
Bartender
Posts: 20833
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YouTube???          

https://www.youtube.com/watch?v=QG8Yr5tkyTg
 
Billy Briggs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nearly got it working. Just can't get the number values written onto the output text files. Code so far is,

import static java.lang.System.out;

import java.io.File;
import java.io.FileWriter;
import java.util.Date;
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintWriter;
//import java.util.regex;
import java.text.SimpleDateFormat;

public class Inputdata2 {

public static void main(String [] args) throws IOException {

File file = new File("C:\\Users\\Gordon\\Documents\\Inputdata.txt");
Scanner scanStr = new Scanner(file);
scanStr.useDelimiter("[^0-9]+ ");

while(scanStr.hasNext())
{
String avalue = scanStr.next();
String bvalue = scanStr.next();
String cvalue = scanStr.next();

double a = Double.parseDouble(avalue);
double b = Double.parseDouble(bvalue);
double c = Double.parseDouble(cvalue);

double sum = a + b + c;

System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);

System.out.println(+ a + " + " + b + " + " + c + " = " + sum);

//String inputa = String.valueOf(a);
//String inputb = String.valueOf(b);
//String inputc = String.valueOf(c);
//String result = String.valueOf(sum);

}
scanStr.close();

Date currentDateTime = new Date();
   SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/Y HH:mm");
   String presentDateTime = dateFormat.format(currentDateTime);
   
   try {

   FileWriter fw = new FileWriter("C:/Users/Gordon/Documents/Output.txt");
   PrintWriter pw = new PrintWriter(fw);
   
   pw.println("Inputs");
   pw.println("a = "+a);
   pw.println("b = "+b);
   pw.println("c = "+c);
   pw.println();
   pw.println("Outputs");
   pw.println("sum = "+sum);
   pw.println();
   pw.println();
   pw.println();
   pw.println(presentDateTime);

   pw.close();

   } catch (IOException e) {
       out.print("Error");
   }

   try {

   FileWriter fw = new FileWriter("C:/Users/Gordon/Documents/Inputdata.txt");
   PrintWriter pw = new PrintWriter(fw);
   
   pw.println("a = "+a);
   pw.println("b = "+b);
   pw.println("c = "+c);
   
   pw.close();

   } catch (IOException e) {
       out.print("Error");
   }
}
}
 
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember to UseCodeTags (that's a link) when you post code.
 
Billy Briggs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have managed to get it working.
 
Knute Snortum
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great!  Would you like to share what you did for the benefit of others?
 
Billy Briggs
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a copy of the final working code

import static java.lang.System.out;
import java.io.File;
import java.io.FileWriter;
import java.util.Date;
import java.util.Scanner;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

public class InputOutputdata {

public static void main(String [] args) throws IOException {

File file = new File("C:\\Users\\Gordon\\Documents\\Inputdata.txt");
Scanner scanStr = new Scanner(file);
scanStr.useDelimiter("[^0-9]+ ");

while(scanStr.hasNext())
{
double a = Double.parseDouble(scanStr.next());
double b = Double.parseDouble(scanStr.next());
double c = Double.parseDouble(scanStr.next());

double sum = a + b + c;

System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println();
System.out.println(+ a + " + " + b + " + " + c + " = " + sum);

Date currentDateTime = new Date();
   SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/Y HH:mm");
   String presentDateTime = dateFormat.format(currentDateTime);
   
   try {

   FileWriter fw = new FileWriter("C:/Users/Gordon/Documents/Output.txt");
   PrintWriter pw = new PrintWriter(fw);
   
   pw.println("Inputs");
   pw.println("a = "+a);
   pw.println("b = "+b);
   pw.println("c = "+c);
   pw.println();
   pw.println("Outputs");
   pw.println("sum = "+sum);
   pw.println();
   pw.println();
   pw.println();
   pw.println(presentDateTime);

   pw.close();

   } catch (IOException e) {
       out.print("Error");
   }

   try {

   FileWriter fw = new FileWriter("C:/Users/Gordon/Documents/Inputdata.txt");
   PrintWriter pw = new PrintWriter(fw);
   
   pw.println("a = "+a);
   pw.println("b = "+b);
   pw.println("c = "+c);
   
   pw.close();

   } catch (IOException e) {
       out.print("Error");
   }
}
scanStr.close();
}
}
 
Marshal
Posts: 64638
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Billy Briggs wrote:. . .
double a = Double.parseDouble(scanStr.next());
double b = Double.parseDouble(scanStr.next());
double c = Double.parseDouble(scanStr.next());
. . .
. . .

Don't use parseDouble() if you are using a Scanner. Use myScanner.nextDoiuble().
Don't close a Scanner. Use try with resources instead, which is much more reliable.
Don't make your methods so long. Look at this FAQ.
Don't do reading and writing in the same method. They belong in different methods.
 
Knute Snortum
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And do UseCodeTags (that's a link).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!