Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: input array

  1. #1
    eIO
    eIO is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default input array

    Hello!
    this is my program
    Java Code:
    import java.util.*;
    
    public class InputArray {
    	public static void main (String[] args){
    		
            Scanner in = new Scanner("D:\\Docs\\temp\\4.txt");
    		
    		String[] days = new String[100];
    		
    		int n = 0;// Current number of values in days.
    		
    		while (in.hasNext()){
    			if (n>= days.length){
    				System.err.println("Too much data!");
    				System.exit(1);
    			}
    			days[n] = in.nextLine();
    			n++;
    			System.out.println(""+days[n]);
    		}
    		
    		//... Now do something with the days!
    	}
    }
    Array.txt.
    Java Code:
    "Su"
    "Mo" 
    "Tu" 
    "We" 
    "Th" 
    "Fr" 
    "Sa"
    why in Console
    Java Code:
    null
    Please help me. i need help.

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    you are incrementing your index value before you are printing the array value. So you are inserting into days[n] and printing days[n+1] which is empty. put your n++ statement below the println and see what happens.

  3. #3
    eIO
    eIO is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    days[n] = in.nextLine();
    			
    			System.out.println(""+days[n]);
    			n++;
    Console
    Java Code:
    D:\Docs\temp\4.txt
    ((

  4. #4
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    you are passing a string to the scanner, you need to pass a file

  5. #5
    Arvind Verma is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    you should do like this
    File file = new File("D:\\Docs\\temp\\4.txt");
    Scanner in = new Scanner(file);

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    And you need to decide whether that file is named Array.txt or 4.txt

    db
    Arvind Verma likes this.

  7. #7
    eIO
    eIO is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Help with validating input into an array
    By kcoltharp in forum New To Java
    Replies: 1
    Last Post: 02-25-2011, 10:05 AM
  2. Input skips on array
    By kryptonian03 in forum New To Java
    Replies: 4
    Last Post: 12-13-2010, 01:48 AM
  3. Array Input
    By Rose88 in forum New To Java
    Replies: 2
    Last Post: 04-19-2009, 11:39 PM
  4. input placed in array
    By smilejava in forum New To Java
    Replies: 5
    Last Post: 11-12-2007, 08:29 AM
  5. input placed in array
    By smilejava in forum New To Java
    Replies: 1
    Last Post: 11-05-2007, 01:32 PM

Posting Permissions

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