14 Replies Latest reply: Mar 10, 2008 8:55 PM by 807591 RSS

    Issue with Scanning Files...

    807591
      So I have a program that scans a file and does a bunch of stuff with it...I won't go into details too much, as it's not relevant.

      Basically, if the file didn't exist, it would return an error message. If the file existed, but the input inside the file wasn't formatted properly, it would return a different error message for each possible issue. I've done all that. But now I have a file that I know is in the workspace, properly formatted, but when i call it in the console, it claims I have an invalid file name. That is, the error that I get if the file doesn't exist. I think it's an issue buried deep into my code, and it's driving me crazy.
      import java.util.*;
      import java.text.*;
      import java.io.*;
      
      public class AssignmentFive_3 {
           
           private static void Todaysdate() {
                SimpleDateFormat BartDateFormat =
                     
                     new SimpleDateFormat("MM/dd/yy");
                
                Date today = new Date();
                System.out.println(BartDateFormat.format(today));
                
           }
      
           public static void main(String[] args) {
                
                Todaysdate();
                
                Scanner sc1 = new Scanner(System.in);
                System.out.print("Please Enter a file name to be read: ");
                String filename = sc1.next();
                
                filename = ""+filename+".txt";
                
                int filelines=0;
                
                try {Scanner sc2 = new Scanner(new File (filename));
           
           while (sc2.hasNextLine()) {filelines++;
           
           String info = sc2.next();
           String name = sc2.next();
           String lname = sc2.next();
           String wage = sc2.next();
           String dependents = sc2.next();
           String deduct = sc2.next();
           String day1 = sc2.next();
           String day2 = sc2.next();
           String day3 = sc2.next();
           String day4 = sc2.next();
           String day5 = sc2.next();     
           
           Boolean error = false;
           
           Date date2 = new Date();
           String newDate = new String();
           
           try {SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
           Date date1 = simpleDateFormat.parse(info);
           Calendar calendar = new GregorianCalendar();
           calendar.setTime(date1);
           calendar.add(Calendar.DAY_OF_MONTH,5);
           date2 = calendar.getTime();
           newDate = simpleDateFormat.format(date2);
           } catch (ParseException F) {System.out.println("Reformat your date and try again."); error = true;}
           
           int one = name.indexOf('0');
           int two = name.indexOf('1');
           int three = name.indexOf('2');
           int four = name.indexOf('3');
           int five = name.indexOf('4');
           int six = name.indexOf('5');
           int seven = name.indexOf('6');
           int eight = name.indexOf('7');
           int nine = name.indexOf('8');
           int zero = name.indexOf('9');
           
           int one1 = lname.indexOf('0');
           int two2 = lname.indexOf('1');
           int three3 = lname.indexOf('2');
           int four4 = lname.indexOf('3');
           int five5 = lname.indexOf('4');
           int six6 = lname.indexOf('5');
           int seven7 = lname.indexOf('6');
           int eight8 = lname.indexOf('7');
           int nine9 = lname.indexOf('8');
           int zero0 = lname.indexOf('9');
      
           if (one > 0 || two > 0 || three > 0 || four > 0 || five > 0 || six > 0 || seven > 0 || eight > 0 || nine > 0 || zero > 0 || one1 > 0 || two2 > 0 || three3 > 0 || four4 > 0 || five5 > 0 || six6 > 0 || seven7 > 0 || eight8 > 0 || nine9 > 0 || zero0 > 0)
           {System.out.println("Aliens might have integers in their name...but we don't employ aliens! Fix your name."); error = true;}
           
           double hours1 = new Double(day1).intValue();
           double hours2 = new Double(day2).intValue();
           double hours3 = new Double(day3).intValue();
           double hours4 = new Double(day4).intValue();
           double hours5 = new Double(day5).intValue();
           double money = new Double(wage).intValue();
           double depend = new Double(dependents).intValue();
           double deducts = new Double(deduct).intValue();
           
           if (money < 7) {System.out.println("Don't be ridiculous! We wouldn't dare pay less than minimum wage. Fix your wage."); error = true;}
      
           if (money > 100) {System.out.println("Who do you think you are? Thor? Short of you being a mythical norse god, we don't pay that much per hour. Fix your wage"); error = true;}
           
           if (depend < 0) {System.out.println("It's wonderful you're so independent...but I doubt you're THAT independent. Fix your dependents."); error = true;}
           
           if (deducts < 0) {System.out.println("You really might want to think about your retirement...at least don't take money OUT of it. Fix your pre-tax retirement deduction."); error = true;}
           
           if (hours1 < 0) {System.out.println("Maybe you're lazy, but give yourself a bit more credit! Fix your hours for day one."); error = true;}
           if (hours2 < 0) {System.out.println("Maybe you're lazy, but give yourself a bit more credit! Fix your hours for day two."); error = true;}
           if (hours3 < 0) {System.out.println("Maybe you're lazy, but give yourself a bit more credit! Fix your hours for day three."); error = true;}
           if (hours4 < 0) {System.out.println("Maybe you're lazy, but give yourself a bit more credit! Fix your hours for day four."); error = true;}
           if (hours5 < 0) {System.out.println("Maybe you're lazy, but give yourself a bit more credit! Fix your hours for day five."); error = true;}
           
           if (hours1 > 12) {System.out.println("This company won't look kindly on sneaking into the office after-hours. Even if it is to work. Fix your hours for day one."); error = true;}
           if (hours2 > 12) {System.out.println("This company won't look kindly on sneaking into the office after-hours. Even if it is to work. Fix your hours for day two."); error = true;}
           if (hours3 > 12) {System.out.println("This company won't look kindly on sneaking into the office after-hours. Even if it is to work. Fix your hours for day three."); error = true;}
           if (hours4 > 12) {System.out.println("This company won't look kindly on sneaking into the office after-hours. Even if it is to work. Fix your hours for day four."); error = true;}
           if (hours5 > 12) {System.out.println("This company won't look kindly on sneaking into the office after-hours. Even if it is to work. Fix your hours for day five."); error = true;}
           
           double hours = (hours1 + hours2 + hours3 + hours4 + hours5);
           double pay1 = ((hours) * (money));
           double overhours = (hours-40);
           while (overhours < 0) {overhours = 0;} 
           double overpay = (overhours*(money*.5));
           double grosspay = (pay1 + overpay);
           double pay = (grosspay) - (100 + (50*depend));
           double taxincome = (pay - deducts);
           while (taxincome < 0) {taxincome = 0;}
           double taxcharge = (((.18 - (.02*depend))*taxincome));
           while (taxcharge < 0) {taxcharge = 0;}
           double paycheck = (taxincome - taxcharge);
           while (paycheck < 0) {paycheck = 0;}
           
           DecimalFormat format = new DecimalFormat("0.00");
           String paycheckprint = (format.format(paycheck) + " ");          
           
           int numby = 0;
           
           if (error = false) {
                while (filelines > numby)
           System.out.println("Pay to the order of " +name+ " " +lname+ ": " +paycheckprint+ "");
           System.out.println("Memo: employee #" + numby + " for the period " +info+ " through " +newDate+ "");
           numby++;
           }
           }
                } catch (Exception E) {System.out.println("Invalid file. Try again.");}     
           }
           
      }
        • 1. Re: Issue with Scanning Files...
          807591
          This most likely due to a path issue. Your program and you think the file is in two different locations. To correct this you can specify the full path to the file. Or even use a JFileChooser.
          • 2. Re: Issue with Scanning Files...
            807591
            import java.util.*;
            import java.text.*;
            import java.io.*;
             
            public class AssignmentFive_3 {
                 
                 private static void Todaysdate() {
                      SimpleDateFormat BartDateFormat =
                           
                           new SimpleDateFormat("MM/dd/yy");
                      
                      Date today = new Date();
                      System.out.println(BartDateFormat.format(today));
                      
                 }
             
                 public static void main(String[] args) {
                      
                      Todaysdate();
                      
                      Scanner sc1 = new Scanner(System.in);
                      System.out.print("Please Enter a file name to be read: ");
                      String filename = sc1.next();
                      
                      filename = ""+filename+".txt";
            put a breakpoint here or a "System.out.println(filename);" and see exactly what you have in the string, then if it's correct, look at your file in the OS and
            tell it to show extensions. That way you can see if there is something strange in your naming of the text file. Also remember if you are using a
            backslash "\" (Windoz seperator) then you have to escape it with an additional backslash like this:
            "C:\\mystuff\\myfiles\\myfile.txt"
            • 3. Re: Issue with Scanning Files...
              796365
              Create a very short test program* that tries to access the file in the same way (and does nothing else).
              If then you can't resolve the problem. post the code here and explain what the problem is.

              Include the EXACT ERROR MESSAGE and show which line if the test program (not just the line nunber) is causing the error.
              • 4. Re: Issue with Scanning Files...
                807591
                filename = ""+filename+".txt";
                Yet another person concatenating to an empty String. Why are people doing this?
                • 5. Re: Issue with Scanning Files...
                  807591
                  Because they get used to implicit conversoin to string, so they do it on everything without thinking.
                  • 6. Re: Issue with Scanning Files...
                    807591
                    morgalr wrote:
                    without thinking.
                    Which is in my experience is the biggest problem with uni students, they just don't think.
                    • 7. Re: Issue with Scanning Files...
                      807591
                      flounder wrote:
                      morgalr wrote:
                      without thinking.
                      Which is in my experience is the biggest problem with uni students, they just don't think.
                      I think you have to put more into that category than just uni students--beginners and casual users in general, but as far as uni students, I've found grad students are by far worse than any of the undergrads. To get a degree in CS at the uni I went to (ages ago) each student had to do a block of "consulting", basically fielding any and all questions in the computer lab, for an entire semester. Undergrades you cold reason with, but "most" or the grad stufents that came, already knew "No under grad could ever help them." It was humorous to see these guys' project dying on the most simple errors and they couldn't get it through their head what it was. I saw one guy fish through his core dump for 2+ weeks to verify exactly what one of the student consultants told him after looking at his code for 30 seconds: he refused to beleive that was the problem, would not ever consider it to try and then changed labs after he found it was just that simple.
                      • 8. Re: Issue with Scanning Files...
                        807591
                        morgalr wrote:
                        flounder wrote:
                        morgalr wrote:
                        without thinking.
                        Which is in my experience is the biggest problem with uni students, they just don't think.
                        I think you have to put more into that category than just uni students--beginners and casual users in general, but as far as uni students, I've found grad students are by far worse than any of the undergrads. To get a degree in CS at the uni I went to (ages ago) each student had to do a block of "consulting", basically fielding any and all questions in the computer lab, for an entire semester. Undergrades you cold reason with, but "most" or the grad stufents that came, already knew "No under grad could ever help them." It was humorous to see these guys' project dying on the most simple errors and they couldn't get it through their head what it was. I saw one guy fish through his core dump for 2+ weeks to verify exactly what one of the student consultants told him after looking at his code for 30 seconds: he refused to beleive that was the problem, would not ever consider it to try and then changed labs after he found it was just that simple.
                        Amen to that.

                        IMHO, there should be two default units in any CS degree- Problem Solving 101 and How To Debug 101. Not only would it solve half the problems on here (and in life) before they come up, it makes you less dependent on any one language.

                        I'm the first to admit that I have been guilty of, and will probably be guilty of in the future, bad debugging and/or problem solving practices. But I'll learn through experience to approach them better. If I'd been taught those at university, then I'd be a much more powerful programmer than I currently am.
                        • 9. Re: Issue with Scanning Files...
                          807591
                          I was actually comparing uni students with high school students. In high school you are basically given a bunch of info and are expected to remember it and regurgitate it come exam time. However at uni you are required to think more for yourself and extrapolate answers. For example, in marine biology a famous question is "Why don't trees grow in the sea". Nowhere throughout the course would that topic be covered implicitly but you could provide a decent explanation if you relied on all the information provided over the semester.
                          • 10. Re: Issue with Scanning Files...
                            807591
                            flounder wrote:
                            I was actually comparing uni students with high school students. In high school you are basically given a bunch of info and are expected to remember it and regurgitate it come exam time. However at uni you are required to think more for yourself and extrapolate answers. For example, in marine biology a famous question is "Why don't trees grow in the sea". Nowhere throughout the course would that topic be covered implicitly but you could provide a decent explanation if you relied on all the information provided over the semester.
                            trees dont grow in the sea? dude... you just blew my mind.
                            its like... its like my fingers. they can touch everything else but themselves. whoa.
                            • 11. Re: Issue with Scanning Files...
                              807591
                              That code is such a mess, I'm not even going to try to figure out how to make it work. However, I see two gross errors. First:
                              if (error = false) 
                              Here you're assigning the value false to the variable error. Since an assignment expression evaluates to the value that was assigned, the code in the if block will never be executed. You probably meant to write this:
                              if (error == false) 
                              but don't do that. It's redundant and, as you just discovered, prone to create subtle bugs if you type it in wrong. Just do this:
                              if (!error) 
                              The second error is this:
                              catch (Exception E) {System.out.println("Invalid file. Try again.");}
                              You catch what could be any subclass of Exception and just assume it's an IOException, and then further assume the exception means the file doesn't exist. At the very minimum, when you catch an exception you should call printStackTrace() so you'll know what exception was thrown and where.
                              • 12. Re: Issue with Scanning Files...
                                807591
                                IMHO, there should be two default units in any CS degree- Problem Solving 101 and How To Debug 101. Not only would it solve half the problems on here (and in life) before they come up, it makes you less dependent on any one language.
                                Amen and Halaluya!

                                I took a class called language design (300 level class) and WOW, we had to study and program in 7 different languages before the end of the semester, not to mention the Pascal, C, C++, LISP, and Assembler that other courses worked with. Basically if you didn't know how to debug--you were gone.

                                I have seen a lot of peeps on here just don't know or are too lazy to run their debugger and have no trouble shooting skills what so ever. It's too bad. When I started programming, it was in a locked room in the attic on my days off and when I had a few minutes of time (back in the day when dinosaurs roamed the earth--like TRS-80 Color Computers, Model 1, 3, and 4's--VIC 20's, Apple IIe, and even before). Nobody I knew programmed and so I "experimented" and read books geared towards one machine and unfamiliar languages while I programmed in yet another and finally decided with an 0.89 MHz CPU, that graphics had to done in Assembler--for those of you too young to remember those days Assembler is an even more arcane art involving late nights, bountiful reboots, and much gnashing of teeth. So I couldn't even imagine people not knowing how to debug and trouble shoot.
                                • 13. Re: Issue with Scanning Files...
                                  807591
                                  Learning Assembly is great for pounding into your head the idea of "the computer does EXACTLY what you tell it to, nothing more, nothing less". I also had a language design class where we wrote a basic token parsing engine, and did assignments in several languages. Was a good experience.
                                  • 14. Re: Issue with Scanning Files...
                                    807591
                                    Yes, exactly--Learning assembler did make for some interesting comments and Jokes though--Q: What are you doing? A: I'm learning about SEX on the 6809. Q: 6809's have SEX? A: Ya, but I don't think I'll be using it much....