FeaturesPluginsDocs & SupportCommunityPartners

NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

Writing Text File information

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
jlperry09



Joined: 12 Nov 2009
Posts: 4

PostPosted: Thu Nov 12, 2009 1:19 am    Post subject: Writing Text File information Reply with quote

I need some assistance, I am writing this program for school, and it requires me to read the user's input for the file name, and then write the output after converting to uppercase, sorting, and using a map to calculate total property listed in dollars.

I got the program to read the user's input, can print out the output, but do not know to write the output to a different text file

Can someone provide some assistance. Following is my code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package realestate;

import java.awt.Component;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.io.FileInputStream;
import java.io.PrintWriter;


/**
* Written on November 11, 2009
* @author jlperry09
*/
public class Main
{

private static JTextArea jTextArea1;
private static Component jTextArea;
private static Object out;
private static int messageType;
private static Object in;


/**
* @param args the command line arguments
*/
public static void main(String[] args)
throws FileNotFoundException, IOException

{



// TODO code application logic here



Scanner realEstate = new Scanner(System.in);
System.out.print("Please enter the file name to import:");
String input = realEstate.nextLine();
System.out.print("Output File Name: ");
String output = realEstate.nextLine();
//System.out.print(input);


if (input != null)
{

FileReader userInput = new FileReader(input);
Scanner inputFile = new Scanner(userInput);


PrintWriter outputFile = new PrintWriter("agentreport.txt");
int lineNumber = 1;

while (lineNumber != 0)
{
String file = inputFile.nextLine();
String propertyType = file.substring(9, 20);
propertyType = propertyType.toUpperCase();
outputFile.print(propertyType);

System.out.println(propertyType);
lineNumber++;
}
System.out.close();
}

else if (input == null)
{
input = null;
JOptionPane.showMessageDialog(null, "This is the user's input" + input);

}

System.exit(0);

}


}

_________________
jlperry
Back to top
View user's profile Send private message MSN Messenger
bhobiger



Joined: 29 Oct 2009
Posts: 52

PostPosted: Thu Nov 12, 2009 7:28 am    Post subject: Reply with quote

For starters change
Code:
PrintWriter outputFile = new PrintWriter("agentreport.txt");

in
Code:
PrintWriter outputFile = new PrintWriter(new FileWriter("agentreport.txt"));
Back to top
View user's profile Send private message
jlperry09



Joined: 12 Nov 2009
Posts: 4

PostPosted: Thu Nov 12, 2009 9:28 am    Post subject: Reply with quote

bhobiger

Replaced the information that you provided, and I am now not creating an output file, and of course nothing is getting written to a file.

Thank you for the quick response

_________________
jlperry
Back to top
View user's profile Send private message MSN Messenger
bhobiger



Joined: 29 Oct 2009
Posts: 52

PostPosted: Thu Nov 12, 2009 11:53 am    Post subject: Reply with quote

inputFile.nextLine() throws a NoSuchElementException when no new line is available in the input file (the red lines in your output window). The exception terminates the program before the new file is actually created.

You must encapsulate your loop in a try...catch block:
Code:
                try {
                    String file = inputFile.nextLine();
                    String propertyType = file.substring(9, 20);
                    propertyType = propertyType.toUpperCase();
                    outputFile.print(propertyType);

                    System.out.println(propertyType);
                    lineNumber++;
                } catch (NoSuchElementException ex) {
                    break;
                }

After the loop make sure that you close your output file:
Code:
outputFile.close();
Back to top
View user's profile Send private message
jlperry09



Joined: 12 Nov 2009
Posts: 4

PostPosted: Fri Nov 13, 2009 11:36 pm    Post subject: Need some assistance with sorting a set Reply with quote

Now, I am not understanding how to sort my set. Any assistance would be greatly appreciated.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package realestate;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
*
* @author jlperry09
*/
public class Main
{

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException
{
// TODO code application logic here

Scanner realEstate = new Scanner(System.in);
System.out.print("Please enter the file name to import:");
String input = realEstate.nextLine();
System.out.println(input);
System.out.println(input.length());
if (input.length() != 0)
{
//System.out.println(input);
FileReader userInput = new FileReader("listings.txt");
Scanner inputFile = new Scanner(userInput);
//System.out.println(input);
while (inputFile.hasNextLine())
{
//String line = inputFile.nextLine();
//out.println("/* " + lineNumber + " *?" + line);
//lineNumber++;
String file = inputFile.nextLine();
//System.out.println(file);
String propertyType = file.substring(9, 21);
String propertyType2 = file.substring(35);
String propertyType3 = file.substring(24, 34);
propertyType = propertyType.toUpperCase();
//System.out.println(propertyType);
Set <String> propertyTypeS = new HashSet<String>();
propertyTypeS.add(propertyType);
propertyTypeS.add(propertyType2 + " " + propertyType3);
//for (String propertyTypeX: propertyTypeS ) {
// System.out.println(propertyTypeX);
//}
}
}

else if (input.length() == 0)
{


}
System.out.close();



//String propertyType = file.substring(9, 20);
//propertyType = propertyType.toUpperCase();



System.exit(0);

}

}

_________________
jlperry
Back to top
View user's profile Send private message MSN Messenger
bhobiger



Joined: 29 Oct 2009
Posts: 52

PostPosted: Sat Nov 14, 2009 1:49 pm    Post subject: Re: Need some assistance with sorting a set Reply with quote

jlperry09 wrote:
Now, I am not understanding how to sort my set. Any assistance would be greatly appreciated.

I think you should seriously consider reading a few tutorials on that subject.
Back to top
View user's profile Send private message
jlperry09



Joined: 12 Nov 2009
Posts: 4

PostPosted: Thu Nov 19, 2009 12:09 am    Post subject: Reply with quote

Now I am having a problem with mapping and I need some assistance please:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package realestate;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.TreeMap;
import java.util.Set;
import java.util.Scanner;
import java.util.TreeSet;

/**
*
* @author Jacqueline Perry
*/
public class Main

{

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException
{
// TODO code application logic here

Scanner realEstate = new Scanner(System.in);
System.out.print("Please enter the file name to import:");
String input = realEstate.nextLine();
System.out.println(input);
//System.out.println(input.length());
if (input.length() != 0)
{

FileReader userInput = new FileReader("listings.txt");
Scanner inputFile = new Scanner(userInput);
//
TreeSet <String> propertyTypeS = new TreeSet<String>();
TreeSet <String> AgentIdSet = new TreeSet<String>();
TreeMap tm = new TreeMap();
Set<String> keySet = tm.keySet();
while (inputFile.hasNextLine())
{
String file = inputFile.nextLine();
String propertyType = file.substring(9, 20);
String agentIdField = file.substring(34);
String propValue = file.substring(23, 34);
//Map<String, totProp> AgentID = new HashMap<String, totProp>();

propertyType = propertyType.toUpperCase();
if (propertyTypeS.contains(propertyType))
{
//do nothing
}
else{
propertyTypeS.add(propertyType);
}
AgentIdSet.add(agentIdField + " " + propValue);

if (tm.containsKey(agentIdField))
{
//tm.put(agentIdField, new Double(290));
for (String Key : keySet)
{
if(agentIdField.contains(Key))
{
System.out.println(Key + "++++" + tm.get(Key));
System.out.println(propValue + "++++" );
//int dblAmt = Integer.parseInt(propValue);
//int dblAmt3 = Integer.parseInt(String.valueOf(Key));
//System.out.println(dblAmt3 + "++++" + dblAmt);
//tm.put(agentIdField, new Double(propValue));
break;
}
}
}
else
{
tm.put(agentIdField, new Double(propValue));
}
}//while
for (String element: propertyTypeS)
{
System.out.println(element);
}
for (String element: AgentIdSet)
{
System.out.println(element);
}

for (String Key : keySet)
{
System.out.println(Key + tm.get(Key));
}
} //if file name

else if (input.length() == 0)
{
System.out.print("Please enter a valid file name");

}

System.out.close();
System.exit(0);


}


}

_________________
jlperry
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB