This question already has an answer here:
What does "Incompatible types: void cannot be converted to ..." mean?
(1 answer)
Closed 4 years ago.
import java.util.Scanner;
import java.io.File;
public class Book
{
private String bookString;
private String bookTitle;
private String bookAuthor;
public Book(String fileName, String title, String author)
{
readBook(fileName);
bookTitle = title;
bookAuthor = author;
}
public boolean containsTitle()
{
}
public int getNumOfCharacters()
{
}
public int countWords(String word)
{
}
public int countSentences()
{
}
public String randomWord(int length)
{
}
public int firstOccurrence(String word)
{
}
public String getSecondSentence()
{
}
public void readBook(String fileName)
{
bookString = "";
try
{
Scanner file = new Scanner(new File(fileName));
while (file.hasNextLine())
{
String line = file.nextLine();
bookString += line + "\n";
}
file.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
Here is some skeleton code that I am trying to finish. My first question is-- don't I have to store the readBook(fileName) in a variable? I tried to store it, then finish the getNumOfCharecters() method with:
bSL = bS.length()
return bSL
I am getting an error message. "incompatible types; void cannot be converted to java.lang.String"
Thanks to anyone willing to help.
The error you get should suggest you that the method is supposed to return a string. Change your method to :
public String readBook(String fileName)
{
bookString = "";
try
{
Scanner file = new Scanner(new File(fileName));
while (file.hasNextLine())
{
String line = file.nextLine();
bookString += line + "\n";
}
file.close();
}
catch (Exception e)
{
System.out.println(e);
}
return bookString;
}
then get the output into a variable like String bS = readBook( fileName );
Related
I have read the file and it should print out the data on the console, but the problem is that I get this error message: Exception in thread "main" java.lang.NumberFormatException: For input string: "UNKNOWN". I've put the maximum length as an integer, but how do I put it as a string as well?
Here's what I have done so far:
import java.util.*;
import java.io.*;
public class Task1 {
public static void main(String[] args) {
List<Person> personFile = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader("people-data.txt"));
String fileRead = br.readLine();
while (fileRead != null) {
String[] peopleData = fileRead.split(":");
String commonName = personData[0];
String latinName = personData[1];
int maximumLength = Integer.parseInt(personData[2]);
Person personObj = new Person(commonName, latinName, maximumLength);
personFile.add(personObj);
fileRead = br.readLine();
}
br.close();
}
catch (FileNotFoundException ex) {
System.out.println("File not found!");
}
catch (IOException ex) {
System.out.println("An error has occured: " + ex.getMessage());
}
System.out.println(personFile);
}
}
Person Class:
import java.util.*;
public class Person1 {
private String commonName;
private String latinName;
private int maximumLength;
public Person1(String personName, String latinName, int maximumLength) {
this.commonName = personName;
this.latinName = latinName;
this.maximumLength = maximumLength;
}
public String getCommonName() {
return commonName;
}
public String getLatinName() {
return latinName;
}
public int getMaximumLength() {
return maximumLength;
}
#Override
public String toString() {
return null;
}
}
Text File:
Alisha Khan:Cephaloscyllium ventriosum:100
Jessica Lane:Galeocerdo cuvier:UNKNOWN
Michael Brown:Sphyrna mokarren:600
...
This line in your input file:
Jessica Lane:Galeocerdo cuvier:UNKNOWN
is causing problem on this line in your code:
int maximumLength = Integer.parseInt(personData[2]);
because parseInt throws NumberFormatException on UNKNOWN. You need to decide what you want to do in this case. For example this code will keep maximumLength to default value -1 when an invalid integer is encountered:
int maximumLength = -1;
try {
int maximumLength = Integer.parseInt(personData[2]);
} catch (NumberFormatException e) {
}
You should teach your code your convention of UNKNOWN. Currently the code treats it as number.
I have created a Song class that includes data members for a Song (Title, artist, album,length). I already have a .txt file that contains different songs which is stored into an array list. After the user finishes adding or deleting songs the program should write to the text file in the original format of the text file (comma separated).
My problem is that the program writes to the file without the commas and it is causing my program to crash when I re-run it by getting an ArrayIndexOutOfBoundsException. I have attempted to use nested for-loops and the printf function but have had no success. How can i write to the file with its original formatting and avoid crashing after i write to it and attempt to run it again?
This is the format of the .txt File
Rock Lobster,The B-52's,The B-52's,4:37
Walk Like An Egyptian,The Bangles,Different Light,3:24
This is my Song Class with toString() method
public class Song {
//Declaring all data members.
private String title;
private String artist;
private String album;
private String length;
private static int songCounter = 0;
//Constructors for Song class.
public Song(String title, String artist, String album, String length){
this.title = title;
this.artist = artist;
this.album = album;
this.length = length;
songCounter++;
}
//Get and Set methods
public String getTitle(){
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist(){
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum(){
return album;
}
public void setAlbum(String album){
this.album = album;
}
public String getLength(){
return length;
}
public void setLength(String length){
this.length = length;
}
public static int getSongCounter(){
return songCounter;
}
public int compareArtist(Song o){
return artist.compareTo(o.artist);
}
public int compareTitle(Song o){
return title.compareTo(o.title);
}
//Overriding the toString() function.
#Override
public String toString(){
return title +","+artist+","+album+","+length;
}
}
This is my Main Class that reads from the file and contains a write method
public class Library {
public static void main(String[] args) {
ArrayList <Song> songList = new ArrayList <Song> ();
boolean repeat = true;
try{
Scanner read = new Scanner (new File("SongList.txt"));
do{
String line = read.nextLine();
String [] tokens = line.split(",");
songList.add(new Song(tokens[0], tokens[1], tokens[2], tokens[3]));
}while(read.hasNext());
read.close();
}catch (FileNotFoundException e){
System.out.println("File not found.");
}
while ( repeat ){
System.out.println("\nSelect a Function");
System.out.println("1. Search Song");
System.out.println("2. Add Song");
System.out.println("3. Delete Song");
System.out.println("4. Display Songs");
System.out.println("5. Quit");
switch (MenuInputCheck(1, 5)){
case 1: searchSong(songList);
break;
case 2: addSong(songList);
break;
case 3: deleteSong(songList);
break;
case 4: displaySorted(songList);
break;
case 5: saveFile(songList);
repeat = false;
break;
}
}
}
public static void saveFile(ArrayList <Song> songList){
try{
PrintWriter writer = new PrintWriter("SongList.txt");
for (int i = 0; i < songList.size();i++){
writer.println(i);
}
writer.close();
} catch (FileNotFoundException e){
System.out.println("File not found.");
}
}
presently the code is simply writing an integer
for (int i = 0; i < songList.size();i++){
writer.println(i);
}
I suggest you create a method in Song which will write its fields sperated by commas
e.g.
public String writeMe () {
StringBuffer buf = new StringBuffer ();
buf.append (name).append(",").append(artist).....;
return buf.toString ();
}
and then in your loop call
writer.write (singList.get(i).writeMe ();
Update
Using you updated code you can get the output using
writer.println(song.toString());
You're writing the value of i to the file?? You should be writing the song information. In the absents of your Song class, I'm just making up fields and methods
If you're using Java 8 you can use the StringJoiner API...
public static void saveFile(ArrayList <Song> songList){
try{
PrintWriter writer = new PrintWriter("SongList.txt");
for (Song song : songList){
StringJoiner sj = new StringJoiner(",");
sj.add(song.getTitle());
sj.add(song.getArtiest());
sj.add(song.getAlbum());
sj.add(song.getDuration());
writer.println(sj.toString());
}
writer.close();
} catch (FileNotFoundException e){
System.out.println("File not found.");
}
}
Otherwise you will need to use something like a String#format or a StringBuilder to generate the output...
Now might be the time to introduce you to JAXB...
Updated
So, based on your updated code, including the Song class, you should be able to do something as simple as...
public static void saveFile(ArrayList <Song> songList){
try{
PrintWriter writer = new PrintWriter("SongList.txt");
for (Song song : songList){
writer.println(song.toString());
}
writer.close();
} catch (FileNotFoundException e){
System.out.println("File not found.");
}
}
Has anyone an idea about what is wrong with my attempt to call a method from a C# dll in my Java code?
Here is my example:
Java code:
public class CsDllHandler {
public interface IKeywordRun extends Library {
public String KeywordRun(String action, String xpath, String inputData,
String verifyData);
}
private static IKeywordRun jnaInstance = null;
public void runDllMethod(String action, String xpath, String inputData,
String verifyData) {
NativeLibrary.addSearchPath(${projectDllName},
"${projectPath}/bin/x64/Debug");
jnaInstance = (IKeywordRun) Native.loadLibrary(
${projectDllName}, IKeywordRun.class);
String csResult = jnaInstance.KeywordRun(action, xpath, inputData,
verifyData);
System.out.println(csResult);
}
}
And in C#:
[RGiesecke.DllExport.DllExport]
public static string KeywordRun(string action, string xpath, string inputData, string verifyData) {
return "C# here";
}
The Unmanaged Exports nuget should be enough for me to call this method (in theory) but I have some strange error:
Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokePointer(Native Method)
at com.sun.jna.Function.invokePointer(Function.java:470)
at com.sun.jna.Function.invokeString(Function.java:651)
at com.sun.jna.Function.invoke(Function.java:395)
at com.sun.jna.Function.invoke(Function.java:315)
at com.sun.jna.Library$Handler.invoke(Library.java:212)
at com.sun.proxy.$Proxy0.KeywordRun(Unknown Source)
at auto.test.keywords.utils.CsDllHandler.runDllMethod(CsDllHandler.java:34)
at auto.test.keywords.runner.MainClass.main(MainClass.java:24)
Well, after another day of research and "trial and error" I have found the cause of my problem and a solution.
The cause was that my C# dll had a dependency on log4net.dll. For running a static method from a standalone C# dll the code from the question is all you need.
The solution for using C# dll with dependencies is to create another dll with no dependency and to load the original dll in this adapter with reflection. In Java you should load the adapter dll with jna and call any exported method. I was able not only to execute methods from the adapter but also to configure log4net with reflection and Java
Here is my code:
(C#)
public class CSharpDllHandler {
private static Logger log = Logger.getLogger(CSharpDllHandler.class);
public interface IFrameworkAdapter extends Library {
public String runKeyword(String action, String xpath, String inputData,
String verifyData);
public String configureLog4net(String log4netConfigPath);
public String loadAssemblies(String frameworkDllPath,
String log4netDllPath);
}
private static IFrameworkAdapter jnaAdapterInstance = null;
private String jnaSearchPath = null;
public CSharpDllHandler(String searchPath) {
this.jnaSearchPath = searchPath;
// add to JNA search path
System.setProperty("jna.library.path", jnaSearchPath);
// load attempt
jnaAdapterInstance = (IFrameworkAdapter) Native.loadLibrary(
"FrameworkAdapter", IFrameworkAdapter.class);
}
public String loadAssemblies(String frameworkDllPath, String log4netDllPath) {
String csResult = jnaAdapterInstance.loadAssemblies(frameworkDllPath,
log4netDllPath);
log.debug(csResult);
return csResult;
}
public String runKeyword(String action, String xpath, String inputData,
String verifyData) {
String csResult = jnaAdapterInstance.runKeyword(action, xpath,
inputData, verifyData);
log.debug(csResult);
return csResult;
}
public String configureLogging(String log4netConfigPath) {
String csResult = jnaAdapterInstance
.configureLog4net(log4netConfigPath);
log.debug(csResult);
return csResult;
}
public String getJnaSearchPath() {
return jnaSearchPath;
}
}
In the main method just use something like this:
CSharpDllHandler dllHandler = new CSharpDllHandler(
${yourFrameworkAdapterDllLocation});
dllHandler.loadAssemblies(
${yourOriginalDllPath},${pathToTheUsedLog4netDllFile});
dllHandler.configureLogging(${log4net.config file path});
dllHandler.runKeyword("JAVA Action", "JAVA Xpath", "JAVA INPUT",
"JAVA VERIFY");
dllHandler.runKeyword("JAVA Action2", "JAVA Xpath2", "JAVA INPUT2",
"JAVA VERIFY2");
In C# I have the desired methods on the original dll:
public static string KeywordRun(string action, string xpath, string inputData, string verifyData) {
log.Debug("Action = " + action);
log.Debug("Xpath = " + xpath);
log.Debug("InputData = " + inputData);
log.Debug("VerifyData = " + verifyData);
return "C# UserActions result: "+ action+" "+xpath+" "+inputData+" "+verifyData;
}
and all the magic is in the DLL Adapter:
namespace FrameworkAdapter {
[ComVisible(true)]
public class FwAdapter {
private const String OK="OK";
private const String frameworkEntryClassName = "${nameOfTheDllClass with method to run }";
private const String log4netConfiguratorClassName = "log4net.Config.XmlConfigurator";
private static Assembly frameworkDll = null;
private static Type frameworkEntryClass = null;
private static MethodInfo keywordRunMethod = null;
private static Assembly logDll = null;
private static Type logEntryClass = null;
private static MethodInfo logConfigureMethod = null;
private static String errorMessage = "OK";
[RGiesecke.DllExport.DllExport]
public static string loadAssemblies(string frameworkDllPath, string log4netDllPath) {
try {
errorMessage = LoadFrameworkDll(frameworkDllPath, frameworkEntryClassName);
LoadFrameworkMethods("KeywordRun", "Setup", "TearDown");
errorMessage = LoadLogAssembly(log4netDllPath, log4netConfiguratorClassName);
if (errorMessage.CompareTo(OK) == 0)
errorMessage = LoadLogMethods("Configure");
}
catch (Exception e) {
return e.Message;
}
return errorMessage;
}
[RGiesecke.DllExport.DllExport]
public static string configureLog4net(string log4netConfigPath) {
if (errorMessage.CompareTo("OK") == 0) {
StringBuilder sb = new StringBuilder();
sb.AppendLine("Try to configure Log4Net");
try {
FileInfo logConfig = new FileInfo(log4netConfigPath);
logConfigureMethod.Invoke(null, new object[] { logConfig });
sb.AppendLine("Log4Net configured");
}
catch (Exception e) {
sb.AppendLine(e.InnerException.Message);
}
return sb.ToString();
}
return errorMessage;
}
[RGiesecke.DllExport.DllExport]
public static string runKeyword(string action, string xpath, string inputData, string verifyData) {
StringBuilder sb = new StringBuilder();
object result = null;
try {
result = keywordRunMethod.Invoke(null, new object[] { action, xpath, inputData, verifyData });
sb.AppendLine(result.ToString());
}
catch (Exception e) {
sb.AppendLine(e.InnerException.Message);
}
return sb.ToString();
}
private static String LoadFrameworkDll(String dllFolderPath, String entryClassName) {
try {
frameworkDll = Assembly.LoadFrom(dllFolderPath);
Type[] dllTypes = frameworkDll.GetExportedTypes();
foreach (Type t in dllTypes)
if (t.FullName.Equals(entryClassName)) {
frameworkEntryClass = t;
break;
}
}
catch (Exception e) {
return e.InnerException.Message;
}
return OK;
}
private static String LoadLogAssembly(String dllFolderPath, String entryClassName) {
try {
logDll = Assembly.LoadFrom(dllFolderPath);
Type[] dllTypes = logDll.GetExportedTypes();
foreach (Type t in dllTypes)
if (t.FullName.Equals(entryClassName)) {
logEntryClass = t;
break;
}
}
catch (Exception e) {
return e.InnerException.Message;
}
return OK;
}
private static String LoadLogMethods(String logMethodName) {
try {
logConfigureMethod = logEntryClass.GetMethod(logMethodName, new Type[] { typeof(FileInfo) });
}
catch (Exception e) {
return e.Message;
}
return OK;
}
private static void LoadFrameworkMethods(String keywordRunName, String scenarioSetupName, String scenarioTearDownName) {
///TODO load the rest of the desired methods here
keywordRunMethod = frameworkEntryClass.GetMethod(keywordRunName);
}
}
}
Running this code will provide all the logged messages from the original C# DLL to the Java console output (and to a file if configured). In a similar way, we can load any other needed dll files for runtime.
Please forgive my [very probable wrong] way of doing things in C# with reflection, I'm new to this language.
Please I need help, I am trying to get my code working properly. The code is supposed to read .txt file with the name A2Q1in.txt" and print out all the information that the file contains by using loadEmployees method. when compile the code I get 2 errors that I do not know how to fix them, please have a look at output at the end of question.
Code:
mport java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class A2Q1
{
public static void main(String[] parms)
{
Employee [] employees ;
String [] array_;
array_ = new String [50];
employees = loadEmployees();
System.out.println("\nProgram completed normally.");
}
public static Employee[] loadEmployees()
{
Employee[] employees;
BufferedReader fileIn;
String inputLine;
try
{
fileIn = new BufferedReader(new FileReader("A2Q1in.txt"));
inputLine = fileIn.readLine();
while (inputLine != null)
{
System.out.println(inputLine);
inputLine = fileIn.readLine();
}
fileIn.close();
}
catch (IOException ioe)
{
System.out.println(ioe.getMessage());
}
print String employees(employees);
}
}
/*********************************************************************/
/*********************************************************************/
class Employee
{
private String employeecomany;
private String name;
private String division;
private Double wage;
public Employee(String employeecomany, String name, String division, Double wage)
{
this.employeecomany = employeecomany;
this.name = name;
this.division = division;
this.wage = wage;
}
public double getWage()
{
return getWage;
}
public String toString()
{
return String.format("%-10s %3d $%4.2f $%5.2f ", employeecomany, name, division, getWage());
}
}
output errors
2 errors and 2 warnings found:
* Errors *
File: C:\Users\samiralbayati\Desktop\Comp 1020 JAVA assignment 2\A2Q1.java [line: 39]
Error: Syntax error, insert ";" to complete LocalVariableDeclarationStatement
File: C:\Users\samiralbayati\Desktop\Comp 1020 JAVA assignment 2\A2Q1.java [line: 68]
Error: getWage cannot be resolved to a variable
Try to remove the print String employees(employees); and write return employees;
and change your code
public double getWage()
{
return getWage;
}
to
public double getWage()
{
return wage;
}
This is because, you did not initialized or declared the getWage variable.
I have a text file that has is set out like the following
Title - Welcome to the Dibb
Date - 13/03/11
Information - Hello and welcome to our website.
Title - Welcome to student room
Date - 06/05/11
Information - Hello and welcome to the student room. We are a online forum that allows previous and current students to ask questions.
I need to parse this text file and save things like the title line, date line and the rest will be saved as information. I know how to read the file and save the full file as a string but I am stuck on getting the select information.
CODE
This is the code I have used to read the text file
helloTxt.setText(readTxt());
}
private String readTxt() {
InputStream inputStream = getResources().openRawResource(R.raw.pages);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1) {
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String str = byteArrayOutputStream.toString();
return str;
}
Read file line by line
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
br.close();
If you can guarantee that every line has as maximum one - then you can use following pattern.
String[] tokens = line.split("\s-\s");
For this line
Title - Welcome to the Dibb
It would give you
tokens[0] = "Title";
tokens[1] = "Welcome to the Dibb";
I try to write some classes which can help you to approach your issue
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Test4 {
private List<Information> parser(String data) {
List<Information> informations = new ArrayList<Information>();
String blocks[] = data.split("\n\r");
for(String block : blocks) {
String[] lines = block.split("\n");
Information information = new Information();
information.setTitle((lines[0].split("-"))[1].trim());
information.setDate((lines[1].split("-"))[1].trim());
information.setInfo((lines[2].split("-"))[1].trim());
informations.add(information);
}
return informations;
}
private void runner() throws IOException {
InputStream inputStream = getClass().getResourceAsStream("input.txt");
String input = "";
int cc;
while((cc = inputStream.read()) != -1) {
input += (char) cc;
}
List<Information> informations = parser(input);
for(Information information : informations) {
System.out.println(information);
}
}
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
Test4 test4 = new Test4();
test4.runner();
}
class Information {
private String title;
private String date;
private String info;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
#Override
public String toString() {
return "Information [" + (title != null ? "title=" + title + ", " : "")
+ (date != null ? "date=" + date + ", " : "") + (info != null ? "info=" + info : "") + "]";
}
}
}