Announcement

Collapse
No announcement yet.

Programming Question Part 2

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Originally posted by DriXnaK View Post
    Why don't I want to learn it yet? I still don't understand the syntax of what I'm trying to do so I'll re-read that link again and I guess try some more experimenting to see if I can get it right. This course is the most worthless ****ing course I've ever seen in my life.
    The code you posted works; the Scanner just does basically what a human would do if presented with the same task, which is, when you ask it "nextInt/next/nextDouble" it reads the file until it encounters a space or newline, and then tries to parse what it has read as an int or double (or just return it as a string).

    Comment


    • #17
      Problem is that the code isn't working. Also, I don't want it to actually add anything to the set or to the file except for "type" which needs to be added into the set. Right now the output file isn't being created or written to since I took out the manually added set entries which means that nothing is being entered into the set right now as far as I can tell. Here's the full code I have right now:

      public class Main {

      /**
      * @param args the command line arguments
      */
      public static void main(String[] args)
      throws FileNotFoundException
      {
      Scanner console = new Scanner(System.in);

      System.out.print("Input File: ");

      String inputFileName = console.next();

      //Creates treeset propertyType
      Set propertyType = new TreeSet();
      //Creates TreeMap "idMap" and sets keys and values.
      TreeMap idMap = new TreeMap();

      int property;
      String type;
      double price;
      int agent;

      while (console.hasNext())
      {
      property = console.nextInt();
      type = console.next();
      price = console.nextDouble();
      agent = console.nextInt();
      propertyType.add(type);
      }

      //Creates FileReader object with name of input file.
      FileReader reader = new FileReader("C:\\Users\\DriXnaK\\Documents\\NetBeansProjects\\A gentReport\\listings.txt");

      //Creates scanner object from FileReader object.
      Scanner agentReport = new Scanner(reader);

      //Prints out to agentreport.txt.
      System.out.print("Output file: ");
      PrintWriter outAgentReport = new PrintWriter("C:\\Users\\DriXnaK\\Documents\\NetBeansProjects\\A gentReport\\agentreport.txt");

      /*Creates set iterator and lists all elements in the propertyType set.
      Converts them to uppercase before printing to the agentreport.txt*/
      Iterator propertyIterator = propertyType.iterator();
      while (propertyIterator.hasNext())
      {
      String upperCaseProperty = propertyIterator.next();
      upperCaseProperty = upperCaseProperty.toUpperCase();
      outAgentReport.println(upperCaseProperty);

      }

      outAgentReport.println(" ");



      outAgentReport.close();
      }

      }

      Comment


      • #18
        Right now after I put in my input file when running it it then asks for the output file which it shouldn't be doing. If I take out the while statement it runs fine again, so I'm doing something wrong with the while statement.

        Comment


        • #19
          You're reading in from the console. You need to use the console to read the file name, and then create a scanner on that file. You're trying to read the property listings from the console, which is why it blocks (you aren't typing anything).

          Code:
              public static void main(String[] args)
              throws FileNotFoundException
              {
                  Scanner console = new Scanner(System.in);
          
                  //Creates treeset propertyType
                  Set propertyType = new TreeSet();
                  //Creates TreeMap "idMap" and sets keys and values.
                  TreeMap idMap = new TreeMap();
          
                  int property;
                  String type;
                  double price;
                  int agent;
                  
                  // read file name from console (should be listings.txt, presumably)
                  Scanner sc = new Scanner(new File(console.nextLine()));
          
                  while (sc.hasNext())
                  {
                      property = console.nextInt();
                      type = console.next();
                      price = console.nextDouble();
                      agent = console.nextInt();
                      propertyType.add(type);
                      // more code needs to go here! you should be also adding things to idMap
                  }
          
                  PrintWriter outAgentReport = new PrintWriter(System.out /* replace with output file when you're done*/ );
          
                  /*Creates set iterator and lists all elements in the propertyType set.
              Converts them to uppercase before printing to the agentreport.txt*/
                  for (String s : propertyType) outAgentReport.println(s.toUpperCase());
          
                  outAgentReport.println(" ");
                  outAgentReport.close();
              }

          Comment


          • #20
            dp

            Comment


            • #21
              lol Yep, you're right. Fixed it:

              while (agentReport.hasNext())
              {
              property = agentReport.nextInt();
              type = agentReport.next();
              propertyType.add(type);
              price = agentReport.nextDouble();
              agent = agentReport.nextInt();

              }

              Amazing how such little things can cause so many problems in programming. Thanks, that was seriously pissing me off.

              Comment


              • #22
                Ok, I'm running into several issues right now. First, it seems to be skipping entries that have more than 1 agent id. What I want to do is instead of skipping these, I want them to add the price to the agent id. I'm not sure how to do this in my while statement. Second, how can I format the price so I have two decimal places? I tried using DecimalFormat like I did on the last task I did, but I'm getting errors this time around. Finally, how do I format this properly in the print out so it's not giving an equals sign? Should I not be using idMap.entrySet()? Thanks. I'll keep trying to figure it out in the meantime.

                This is my printout of the treemap:

                101=100000.0
                105=30000.0
                106=200000.0
                107=40000.0
                110=250000.0

                Here's my code for the two parts that concern it:

                while (agentReport.hasNext())
                {
                property = agentReport.nextInt();
                type = agentReport.next();
                propertyType.add(type);
                price = agentReport.nextDouble();
                agent = agentReport.nextInt();
                idMap.put(agent, price);
                }

                //Creates a collection.
                Collection idMapC = idMap.entrySet();
                Iterator idMapiter = idMapC.iterator();

                while (idMapiter.hasNext())
                {
                outAgentReport.println(idMapiter.next());
                }

                Comment


                • #23
                  idMap.put(agent, price);


                  This line is wrong. Not in the sense of you are calling a function wrong, etc. but in the sense that you algorithm should do something different.

                  Imagine, for a moment, that you were doing this on paper and were creating (and updating) a table of agent/total price pairs as you read in the input file. If you saw agent ID 007, what would you do? Then do that in code.

                  Comment


                  • #24
                    "Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced. "

                    So basically what's happening right now is it's adding in the id/value pairs and erasing the previous value every time it comes across a previously entered key/value pair. I looked through the API but I don't see anything besides put. To be honest, I'm not really sure how to go about this. I can't find any examples of what I'm trying to do.

                    Comment


                    • #25
                      There is, of course, also get().

                      Comment


                      • #26
                        But doesn't that just pull an existing entry? I was under the impression that that doesn't write to the maptree.

                        Comment


                        • #27
                          You can't do this in one method call.

                          Comment


                          • #28
                            I'm going to call it a night. I've been at this for about 10 hours today now. I'll think about it some more in the morning. Thanks for all your help so far.

                            Comment


                            • #29
                              Do I need to use a multimap for this?

                              Comment

                              Working...
                              X