Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Program

Options
  • 03-05-2006 1:02pm
    #1
    Registered Users Posts: 427 ✭✭


    Hi,
    Im writing a program which makes a html page containing 25 images given by the user. The html code is read from 2 files(input1.txt and input2txt) and is written to the new html file. The problem is input1 is never written to the file. I think I am overwriting the file after I write input1 but I am not sure. Any ideas?

    import java.io.*;
    import java.util.*;
    class Filepath
    {
    	public static void main(String[] args) 
    	{
    		String carrier1;
    		String carrier2;
    		System.out.println("Please enter the number images you are using:");
    		int images = Console.readInt();
    		int control = 1;
    		System.out.println("Please enter the folder name:");
    		String Foldername = Console.readString();
    		try
    		{
    			PrintWriter webpage = new PrintWriter(new File(Foldername + ".html"));
    			Scanner start = new Scanner(new File("input1.txt"));
    			while (start.hasNextLine())
    			{
    				carrier1 = start.nextLine();
    				System.out.print(carrier1);
    				webpage.print(carrier1);
    			}
    		}
    		catch (IOException e)
    		{
    		}
    		try
    		{
    			PrintWriter webpage = new PrintWriter(new File(Foldername + ".html"));
    			webpage.println("");
    			webpage.println("");
    			while (control <= images && control <= 25)
    			{
    				if (control < 10)
    				{
    					webpage.println("<a href='pics/" + Foldername + "/0" + control + ".jpg'><img src='pics/" + Foldername + "/thumbnails/0" + control + ".jpg' BORDER='0' width='100' height='100'></a>");
    				}
    				else
    				{
    					webpage.println("<a href='pics/" + Foldername + "/" + control + ".jpg'><img src='pics/" + Foldername + "/thumbnails/" + control + ".jpg' BORDER='0' width='100' height='100'></a>");
    				}
    				control++;
    			}
    			try
    			{
    				Scanner end = new Scanner(new File("input2.txt"));
    				while (end.hasNext())
    				{
    					carrier2 = end.nextLine();
    					webpage.print(carrier2);
    				}
    			}
    			catch (IOException e)
    			{
    			}
    			webpage.close();
    		}
    		catch (IOException e)
    		{
    		}
    	}
    }
    


Comments

  • Registered Users Posts: 4,188 ✭✭✭pH


    Yes most likely

    Removed the 2nd
    PrintWriter webpage = new PrintWriter(new File(Foldername + ".html"));
    

    And put it all in a single try/catch block so that you still have scope of the initial webpage variable and it should work.


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Agreed! Beat me to it pH

    Also - Don't know if it makes a difference but you have used a method called hasNext() in the while loop which loops through 'end' but the method to loop through 'start' is called hasNextLine()


  • Registered Users Posts: 427 ✭✭Kevo


    Theres a similar problem with this code. It doesnt print anything at all. Although the file is still created. Can you see any problems?
    Thanks for the quick replies.
    import java.io.*;
    import java.util.*;
    class Filepath
    {
    	public static void main(String[] args) 
    	{
    		String carrier1;
    		String carrier2;
    		System.out.println("Please enter the number images you are using:");
    		int images = Console.readInt();
    		int control = 1;
    		System.out.println("Please enter the folder name:");
    		String Foldername = Console.readString();
    		try
    		{
    			PrintWriter webpage = new PrintWriter(new File(Foldername + ".html"));
    			Scanner start = new Scanner(new File("input1.txt"));
    			while (start.hasNextLine())
    			{
    				carrier1 = start.nextLine();
    				webpage.print(carrier1);
    			}
    			while (control <= images && control <= 25)
    			{
    				if (control < 10)
    				{
    					webpage.println("<a href='pics/" + Foldername + "/0" + control + ".jpg'><img src='pics/" + Foldername + "/thumbnails/0" + control + ".jpg' BORDER='0' width='100' height='100'></a>");
    				}
    				else
    				{
    					webpage.println("<a href='pics/" + Foldername + "/" + control + ".jpg'><img src='pics/" + Foldername + "/thumbnails/" + control + ".jpg' BORDER='0' width='100' height='100'></a>");
    				}
    				control++;
    			}
    			Scanner end = new Scanner(new File("input2.txt"));
    			while (end.hasNextLine())
    			{
    				carrier2 = end.nextLine();
    				webpage.print(carrier2);
    				webpage.close();
    			}
    		}
    		catch (IOException e)
    		{
    		}
    	}
    }
    


  • Registered Users Posts: 4,188 ✭✭✭pH


    Try replacing the webpage.println()'s with System.out.println().

    or just set webpage = System.out;

    run the program printing output to the screen, add additional debug prints to see where it's going - then when you're happy switch it back to the file.


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    webpage.close is inside the second while loop that loops through 'end' so the PrintWriter is being closed after the first iteration of this loop. Move it outside, after the while loop!


  • Advertisement
  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Also, you should have an 'e.printStackTrace()' in your catch statement so you will actually be notified of exceptions (instead of ignoring them!) ;)


  • Registered Users Posts: 427 ✭✭Kevo


    It seems to skip everything after the first scanner is used.
    Scanner start = new Scanner(new File("input1.txt"));
    

    edit
    Im getting a file not found exception on input1.txt. I dont understand why this is. input1.txt is in the same folder and the filenames match exactly.


  • Registered Users Posts: 4,188 ✭✭✭pH


    Dr Pepper wrote:
    Also, you should have an 'e.printStackTrace()' in your catch statement so you will actually be notified of exceptions (instead of ignoring them!) ;)
    Agreed - Even better throw a RuntimeException - and use the thrown exception as the cause.

    Never have empty catch blocks - your code may be taking an exception route and you don't know it
    try {
    
    ...
    
    } catch (Exception e) {
       throw new RuntimeException("Something bad happened", e);
    }
    


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    Kevo wrote:
    It seems to skip everything after the first scanner is used.
    Scanner start = new Scanner(new File("input1.txt"));
    

    edit
    Im getting a file not found exception on input1.txt. I dont understand why this is. input1.txt is in the same folder and the filenames match exactly.

    Not sure why this is myself.. Are you sure input1.txt is in the same folder as Filepath.java? You could try putting in the full path (e.g. "C:\\whatever\\input.txt"). I'm not familiar with the 'Scanner' object. Maybe the file input1.txt is open in another program and you cannot access it? (Although that probably wouldn't be a FNF exception)


  • Closed Accounts Posts: 13 smythmark


    This may be your problem. As far as I know input1.txt doesn't have to be in the same folder as the .class file, but rather it needs to be in the folder that you run the program from. If this is already the case then buggered if I know.:D


  • Advertisement
  • Closed Accounts Posts: 80 ✭✭Torak


    smythmark wrote:
    This may be your problem. As far as I know input1.txt doesn't have to be in the same folder as the .class file, but rather it needs to be in the folder that you run the program from. If this is already the case then buggered if I know.:D

    Actually as far as i remember it has to be in the directory specified in the user.dir system property. (which defaults to the folder that you run from)
      System.out.println(System.getProperty("user.dir"));
    


  • Registered Users Posts: 427 ✭✭Kevo


    Torak wrote:
    Actually as far as i remember it has to be in the directory specified in the user.dir system property. (which defaults to the folder that you run from)
      System.out.println(System.getProperty("user.dir"));
    

    I used this code and input1.txt is definitely in the correct folder.
    Heres the latest code:
    import java.io.*;
    import java.util.*;
    class Filepath
    {
    	public static void main(String[] args) 
    	{
    		String carrier1;
    		String carrier2;
    		System.out.println("Please enter the number images you are using:");
    		int images = Console.readInt();
    		int control = 1;
    		System.out.println("Please enter the folder name:");
    		String Foldername = Console.readString();
    		try
    		{
    			System.out.println("Begin");
    			PrintWriter webpage = new PrintWriter(new File(Foldername + ".html"));
    			System.out.println("Begin2");
    			Scanner start = new Scanner(new File("input1.txt"));
    			System.out.println("Begin3");
    			while (start.hasNextLine())
    			{
    				System.out.println("1");
    				carrier1 = start.nextLine();
    				webpage.print(carrier1);
    			}
    			while (control <= images && control <= 25)
    			{
    				System.out.println("2");
    				if (control < 10)
    				{
    					webpage.println("<a href='pics/" + Foldername + "/0" + control + ".jpg'><img src='pics/" + Foldername + "/thumbnails/0" + control + ".jpg' BORDER='0' width='100' height='100'></a>");
    				}
    				else
    				{
    					webpage.println("<a href='pics/" + Foldername + "/" + control + ".jpg'><img src='pics/" + Foldername + "/thumbnails/" + control + ".jpg' BORDER='0' width='100' height='100'></a>");
    				}
    				control++;
    			}
    			Scanner end = new Scanner(new File("input2.txt"));
    			while (end.hasNextLine())
    			{
    				System.out.println("3");
    				carrier2 = end.nextLine();
    				webpage.print(carrier2);
    			}
    			webpage.close();
    			System.out.println("End");
    		}
    		catch (IOException e)
    		{
    			throw new RuntimeException("Something bad happened", e);
    		}
    	}
    }
    


  • Registered Users Posts: 4,188 ✭✭✭pH


    have you tried moving the input file to a known directory(c:\files) and opening it from there? If this works then you know where the problem is!
    Scanner start = new Scanner(new File("C:\\files\\input1.txt"));
    

    Also you could try this to see where java is looking for this file :
    File f = new File("input1.txt");
    System.out.println("File is [" + f.getAbsolutePath() + "]");
    Scanner start = new Scanner( f );
    


  • Registered Users Posts: 427 ✭✭Kevo


    I can't believe this. I named the input file input1.txt.txt. It works perfectly now. Thanks for your help!

    Just one more question. This is from a past paper.

    Write a program which displays the maximum in a collection of integers passed as command line arguments. eg. java MaxInteger 4 7 6 3 8 9.

    heres my attempt:
    int max = 0;
    int tmp;
    int i = 0;
    while (i < ?????????????????????????)
    {
          tmp = args[i]
          if (tmp > max)
          {
                 max = tmp;
          }
    }
    System.out.print(max);
    

    I understand most of the questuion but i am unsure how to see the total amount of integers. Any ideas?
    BTW this isn't homework.


  • Registered Users Posts: 841 ✭✭✭Dr Pepper


    The number of iterations for the loop is equal to args.length, therefor, instead of while use for(int i = 0; i < args.length; i++) Otherwise it looks good!


  • Closed Accounts Posts: 13 smythmark


    Kevo wrote:
    I can't believe this. I named the input file input1.txt.txt. It works perfectly now. Thanks for your help!
    BTW this isn't homework.

    You must have named it input1.txt and saved it as type text file. Very annoying feature of notepad:mad:


  • Registered Users Posts: 4,188 ✭✭✭pH


    smythmark wrote:
    You must have named it input1.txt and saved it as type text file. Very annoying feature of notepad:mad:
    Couple this with the brilliant idea that is 'hide extensions for known file types' ...


Advertisement