Results 1 to 3 of 3
Thread: Read From File need help
- 09-29-2009, 04:43 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 1
- Rep Power
- 0
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 :
and here's my main classJava Code:Input.in 10 100 100 200 900 1000
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); } }
- 09-29-2009, 06:34 PM #2
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
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(); } }
- 09-30-2009, 01:08 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM -
Help for read a doc file
By thehero in forum Advanced JavaReplies: 2Last Post: 02-26-2009, 06:08 AM -
how to read openproj(Projity) file i.e. ,POD file(Project Management file)
By mahendra.athneria in forum New To JavaReplies: 0Last Post: 02-11-2009, 09:53 AM -
How to read and write to a file without taking out the comments in the file
By MAGNUM in forum New To JavaReplies: 5Last Post: 02-05-2009, 10:28 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks