Results 1 to 3 of 3
  1. #1
    sdcz6 is offline Member
    Join Date
    Sep 2009
    Posts
    1
    Rep Power
    0

    Default Read From File need help

    hi guys,

    its my fresh start here in JAVA can you help me do the ReadFromFile method uhm i need to read all the inputs from a file named "Input.in" which are :

    Java Code:
    Input.in
    10 100
    100 200
    900 1000
    and here's my main class

    Java Code:
    import java.io.*;
    import java.util.*;
    public class one
    {
    	public static void main(String[]args) throws Exception
    	{
    
    
    		Scanner sc = new Scanner(new FileReader("Input.in"));
    
    		int min = Integer.parseInt(sc.next());
    		int max = Integer.parseInt(sc.next());
    
    		int count=0;
    		int maxCount=0;
    	
    		for(int x=min;x<=max;x++)
    		{
    			int temp=x;
    			//System.out.print(x+" ");
    			do{
    				if(temp%2==0)
    					temp=temp/2;
    				else
    					temp=(temp*3)+1;	
    			count++;
    			//System.out.print(temp+" ");
    
    			}
    			while(temp!=1);
    			//System.out.print(count+1);
    			
    			if(temp==1)
    			{	if(count>maxCount)
    				maxCount = count+1;
    				count=0;
    			}
    
    			//System.out.println();
    		}
    		System.out.print(min+" "+max+" "+maxCount);
    		
    	}
    }

  2. #2
    oxano is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Okey the following code reads the integers from the file into an array.
    I used 3 types of ways to put it into arrays.
    array of int : int[6] (known size)
    array of int : int[0] (unknown size)
    arraylist of Integer : ArrayList<Integer>

    what do i mean with known and unknown size.
    now we both know that it will have to read 6 ints and store them in an array, so we can create an array of 6. ( ... = new int[6])
    now if we haven't seen the file before we can't create an array with the size of 500 and hope it will fit into it (actually we can, but takes up too much memory space, looks bad, if it's more than 500 : crash , .... don't do that :) )
    so that's the unknown size part. if there are 4 integers in the file, it will expand to an array of 4 etc..

    the int arrays are known as static arrays, as they have a set maximum of objects / integers that you can put in. ArrayList is a dynamic array. Meaning you can just keep adding and adding and the ArrayList class will make sure it will increase size etc. Same if you're removing objects out of it, it will reduce in size.

    in the following code i have used all 3 + added quite some comments to hopefully make it more understandable for you.

    even used colors to make it more readable (OMG:eek:, yes i have too much time :D)


    Java Code:
    [COLOR="Blue"]import [/COLOR]java.io.*;
    [COLOR="Blue"]import [/COLOR]java.util.*;
    
    [COLOR="Blue"]public [/COLOR][COLOR="Blue"]class [/COLOR]One{
        [COLOR="Blue"]private int [/COLOR][COLOR="Purple"]intArrayKnownSize[][/COLOR] = [COLOR="Blue"]new int[/COLOR][6];  //if known size
        [COLOR="Blue"]private int[/COLOR] [COLOR="Purple"]intArrayUnknownSize[][/COLOR] = [COLOR="Blue"]new int[/COLOR][0]; //if unknown size
        [COLOR="Blue"]private [/COLOR]ArrayList<Integer> [COLOR="Purple"]intArrayList[/COLOR] = [COLOR="Blue"]new [/COLOR]ArrayList<Integer>(); //if unknown size, Easy solution
        [COLOR="Blue"]private [/COLOR]Scanner [COLOR="Purple"]sc[/COLOR];
    
        [COLOR="Blue"]public [/COLOR]One (){
    
            [COLOR="Blue"]try[/COLOR]{
            [COLOR="Purple"]sc[/COLOR] = [COLOR="Blue"]new [/COLOR]Scanner([COLOR="Blue"]new [/COLOR]FileReader("Input.in"));
            }
            [COLOR="Blue"]catch[/COLOR](Exception e){
                System.out.println("something went wrong with the file.");
            }
    
            [COLOR="Blue"]int [/COLOR]number,i=0;
    	[COLOR="Blue"]while [/COLOR](sc.hasNextInt()){            [COLOR="Gray"]//as long as there are integers left to read...[/COLOR]
                number = [COLOR="Purple"]sc[/COLOR].nextInt();          [COLOR="Gray"]//read an integer from the file.[/COLOR]
    
                [COLOR="Purple"]intArrayKnownSize[i][/COLOR] = number; [COLOR="Gray"] //easy way to put it in the array. Possible if the amount of integers is
                                                // predefined.[/COLOR]
                i++;                            [COLOR="Gray"]//required to put it in the right position
    [/COLOR]
                addOne(number);                 [COLOR="Gray"]//method for when you want to use an array of ints when the amount of[/COLOR]
                                                [COLOR="Gray"]//integers  is not predefined[/COLOR]
    
                [COLOR="Purple"]intArrayList[/COLOR].add(number);       [COLOR="Gray"]//used an ArrayList, it's a dynamic Array, meaning it will change its size
                                                //to whatever is needed to fit the content [/COLOR][COLOR="Gray"]in it.[/COLOR]
    
           }
    
           [COLOR="Blue"]for[/COLOR]( i =0 ; i<6 ; i++){
    		    System.out.print([COLOR="Purple"]intArrayKnownSize[i][/COLOR]);
                System.out.print(" "+ [COLOR="Purple"]intArrayUnknownSize[i][/COLOR]);
                System.out.println(" " + [COLOR="Purple"]intArrayList.get(i)[/COLOR] +"[COLOR="Blue"]\n[/COLOR]");
            }
        }
    
        [COLOR="Blue"]private [/COLOR][COLOR="Blue"]void [/COLOR]addOne([COLOR="Blue"]int [/COLOR]number){
            [COLOR="Blue"]int [/COLOR]tempArray[] = [COLOR="Purple"]intArrayUnknownSize[/COLOR];                      [COLOR="Gray"]//temporary array to not loose the data of the 7[/COLOR]
                                                                        [COLOR="Gray"]// current one.[/COLOR]
            [COLOR="Purple"]intArrayUnknownSize[/COLOR] = [COLOR="Blue"]new [/COLOR][COLOR="Blue"]int[/COLOR][tempArray.length+1];          [COLOR="Gray"]//create an array that's 1 bigger than the previous[/COLOR]
            [COLOR="Blue"]for[/COLOR]([COLOR="Blue"]int [/COLOR]i=0;i<tempArray.length ; i++){
                [COLOR="Purple"]intArrayUnknownSize[i][/COLOR] = tempArray[i];                 [COLOR="Gray"] //put the data back into the larger array[/COLOR]
            }
            intArrayUnknownSize[intArrayUnknownSize.length-1] = number;   [COLOR="Gray"]//add integer on the newly created spot[/COLOR]
        }
    
    
    
    
        [COLOR="Blue"]public static void [/COLOR]main(String[]args) [COLOR="Blue"]throws [/COLOR]Exception{
    		One one = [COLOR="Blue"]new [/COLOR]One();
        }
    
    }

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

Similar Threads

  1. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  2. Help for read a doc file
    By thehero in forum Advanced Java
    Replies: 2
    Last Post: 02-26-2009, 06:08 AM
  3. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  4. Replies: 5
    Last Post: 02-05-2009, 10:28 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •