Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
saving output as textfile

Posted by RKM (RKM), 30 May 2003
Hello
this message is related to the message that has  the tittle (array in descending order) because  I still have little problem since I need the positions with the additional one
Also l have to save the output (only  the values of positions )in text file ,one value in each line ,so I can use it by another program
Thank you   for your time.  


Posted by admin (Graham Ellis), 30 May 2003
If you replace

Code:
OriginalPosition.put(new Integer(in1[i]),new Integer(i));


with

Code:
OriginalPosition.put(new Integer(in1[i]),new Integer(i+1));


You'll get each of your position numbers one higher.

If you replace System.out with an appropriate PrintStream or PrintWriter object, which you will need to construct / open before you try to write to it, you'll get the output to the file rather than the screen.

Posted by RKM (RKM), 1 June 2003
i found some difficulties in constructing the files.

actually,I am working on a project which consists of two part each part  is written in different language ( the sorting program is the part which is written in java)
and I want to connect between the two part using the files,so i want to change that code

for example

intializing the array must be as following
int [] in1= function that open saved file which contains the numbers ( one number in a line) and insert them in the array
and at the end I must store the output (only the original positions ) in a text file ( one number in a line) so i can use this file in the second part of the project.

i hope that you can help me and thanks in advance

Posted by admin (Graham Ellis), 1 June 2003
Quote:
i found some difficulties in constructing the files.


I know the feeling  

Learning how to handle files in Java is not a trivial task ...

In order to know about files, you have to know about exceptions
In order to know about exceptions, you need to understand inheritance
In order to understand inheritance, you need to know about objects ....

Why are you using two languages?   What is the second language? Most languages can sort (and many, let's be honest, can sort in a much easier piece of code than Java).  It would seem much more practical to use one language rather than two  ...

Posted by RKM (RKM), 2 June 2003
I " have to"  use two language ( java and Oracle) and i can not use only one.
and I am using Oracle  to build a database  and to do other tasks.
the two part are finished, but the problem is how to connect them  and i think using files-as i described in previous message- is  the solution  even it is  so complicated  .

please, help me  as soon as possible.

Posted by admin (Graham Ellis), 3 June 2003
OK - I can appreciate that SQL is a different sort of language and that you need to manipulate the data files further once they're written out by your Oracle client.

I've extended my earlier example to output to a file rather than to the standard ouput - this includes opening a print stream, catching any errors in the act of doing the open, outputting to the file, and closing the file on completion.   I've also added in that extra "1" to each count that you were asking about earlier.

Code:
import java.util.*;
import java.io.*;

public class Open {
public static void main (String [] args) {
       int [] in1 = { 0,0,0,80,0,0,100,0,0,90,0,0,70,60};

// Remember Original Positions, giving precedence to earlier occurrence
       ArrayList Values = new ArrayList();
       HashMap OriginalPosition = new HashMap();
       for (int i=in1.length-1; i>=0; i--) {
               OriginalPosition.put(new Integer(in1[i]),new Integer(i+1));
               Values.add(new Integer(in1[i]));
               }

// Sort incoming array
       Collections.sort(Values);

PrintStream MyOutput = null;
try {
       MyOutput = new PrintStream(new FileOutputStream("abc123.txt"));
   }
catch (IOException e)
   {
      System.out.println("OOps");
   }


// List out sorted values in reverse order, with position numbers
if (MyOutput != null) {
       for (int i=Values.size()-1; i>=0; i--) {
               int val = ((Integer)(Values.get(i))).intValue();
               MyOutput.print("" + val + "   ");
               MyOutput.println(OriginalPosition.get(new Integer(val)));
               }
       MyOutput.close();
       } else {
       System.out.println("No output file written");
       }
}
}


I am getting concerned that this thread / exchange is moving away from the provision of discussions and answers of points that are of concern to newcomers to Java, and is turning more into a "please write my application for me" thread.   Quite apart from the time this takes, I don't feel that I would be doing you a good service in the long term if I provided a piece of code that you didn't fully understand, written to meet a requirement that I don't fully understand.

So I'll provide a couple more snippets of code that will help you on the input side of your program, but I'm going to leave the integration of them into the final program you need up to you.

Here's a piece of code to read a file ...

Code:
BufferedReader demo = new BufferedReader(new FileReader(demofile));
  for (int k=0;k<10;k++)
     {
     String next_line = demo.readLine();
     System.out.println(next_line);
     }


... you'll need to add exception handling in case the file isn't available,  and you'll want to convert the incoming String to an integer.   Have a look at java.lang.Integer where there are a number of suitable methods available - getInteger or parseInt may do the trick for you.

Here's an example of the use of parseInt from a piece of code I have lying around - it's part of a more complex example that's splitting up (tokenizing) each input line in a file into a number of variables, but it does give you an example of a call.

Code:
status = Integer.parseInt(Splitter.nextToken(" \t"));




Posted by RKM (RKM), 5 June 2003
the program works but the only problem is
when the  file consist of  less than 10 numbers



import java.util.*;
import java.io.*;
class TextFileReader
{
private BufferedReader inputFile;
private String fileName;
private boolean dataLeft;

public TextFileReader (String name)
//************************************************
// This constructor opens the file given by the name passed in the parameter.
//************************************************
{
fileName = name;
try {inputFile = new BufferedReader (new FileReader(fileName));}
catch (Exception error)
{
System.err.println ("An error occured opening file " +
fileName + "!\n");
System.exit(1);
}
dataLeft = true;
}
public String readLine ()
//************************************************
// This method reads and returns one line of the text file. If the file
// is empty, an empty string is returned.
//************************************************
{
String inputLine;

try
{
if ( (inputLine = inputFile.readLine()) == null )
{
dataLeft = false;
return "";
}
else return inputLine;
}

catch (Exception error)
{
System.err.println ("An error occurred reading from file " +
fileName + "!\n");
System.exit(1);
}

return "";
}

public boolean moreData ()
//************************************************
// This function returns true if the end of file has not been read.
//************************************************
{
return dataLeft;
}
}





public class Open {
static TextFileReader inFile;

public static void main (String [] args) {
  float[] array = new float[10];
  inFile = new TextFileReader("c:\\me.txt");
  for(int i =0;i<10;i++)
  {
   String next_line= inFile.readLine();
   System. out.println(next_line);
   array[i] = java.lang.Float.parseFloat(next_line);// intializing array of float type
 }
for(int i=0;i<10;i++)
System.out.println(array[i]);
}
}

Posted by admin (Graham Ellis), 6 June 2003
If you can tell us what the errors, it'll be easier to offer advise.




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho