Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Program to read from created file

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program to read from created file

    This is the code i am currently working with. What it is supposed to do is read the file "scripts.dat" which is the following

    SCRIPTS.DAT
    Secret Assassins 0 1 0 0 1 0 0 1 1 40
    The UltraZombies 0 1 0 1 0 1 0 2 2 120

    scripts.dat is delimited by tabs ("\t")



    import java.util.Scanner;
    import java.io.*;
    public class driver
    {
    public static void main (String[]args) throws Exception
        { 
            File script = new File ("scripts.dat");
            Scanner s1 = new Scanner(script);
            s1.useDelimiter("\t");
            String script1 = s1.next();
            System.out.println(script1);
            for(int i = 0; i<10; i++)  //if i<10 there is an error so i have to put it to 9 for it to run that is because
                                                   //java tries to use "40 TheUltraZombies" as a integer but it isnt
            {
            int script2 = s1.nextInt();
            System.out.println(script2);
            }
     
            String script3 = s1.next();
            System.out.println(script3);
        }
    }

    the output of the code is
    Secret Assassins
    0
    1
    0
    0
    1
    0
    0
    1
    1
    40 The UltraZombies

    the problem is the "40 The UltraZombies"
    i'm trying to create the code so that the output is

    Secret Assassins
    0
    1
    0
    0
    1
    0
    0
    1
    1
    40
    The UltraZombies

    40 and ultrazombies should be on different lines

    i cannot use arrays and i know it can be done without using arrays


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Reading from created file

    Good morning aznprdgy,

    How about this?

    import java.util.Scanner;
    import java.io.*;
     
    public class driver {
     
        public static void main(String[] args) throws Exception {
     
            File script = new File("scripts.dat");
            Scanner s1 = new Scanner(script);
            //s1.useDelimiter("\t");
     
            String script1 = s1.next();
            String script2 = s1.next();
            System.out.println(script1 + " " + script2);
     
            while(s1.hasNextInt()){
     
                System.out.println(s1.next());
     
            }
     
            String script3 = s1.next();
            String script4 = s1.next();
            System.out.println(script3 + " " + script4);
     
            while(s1.hasNextInt()){
     
                System.out.println(s1.next());
     
            }
     
        }
    }

    Output:

    Secret Assassins
    0
    1
    0
    0
    1
    0
    0
    1
    1
    40
    The UltraZombies
    0
    1
    0
    1
    0
    1
    0
    2
    2
    120
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading from created file

    ah sorry no
    the values have to actually be separate not just appear so that wont work. I need it so that each individual line can be assigned to a String or int variable.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Red face Re: Reading from created file

    Quote Originally Posted by aznprdgy View Post
    ah sorry no
    the values have to actually be separate not just appear so that wont work. I need it so that each individual line can be assigned to a String or int variable.
    How about this?

    import java.util.Scanner;
    import java.io.*;
     
    public class driver {
     
        public static int one, two, three, four, five, six, seven, eight, nine, ten;
        public static int one2, two2, three2, four2, five2, six2, seven2, eight2, nine2, ten2;
        public static String zombies, assassins;
     
        public static void main(String[] args) throws Exception {
     
            File script = new File("scripts.dat");
            Scanner s1 = new Scanner(script);
     
            String script1 = s1.next();
            String script2 = s1.next();
            assassins = script1 + " " + script2;
            System.out.println(assassins);
     
            one = s1.nextInt();
            two = s1.nextInt();
            three = s1.nextInt();
            four = s1.nextInt();
            five = s1.nextInt();
            six = s1.nextInt();
            seven = s1.nextInt();
            eight = s1.nextInt();
            nine = s1.nextInt();
            ten = s1.nextInt();
     
            System.out.println(one + "\n" + two + "\n" + three + "\n" + four + "\n"
                    + five + "\n" + six + "\n" + seven + "\n" + eight + "\n" + nine
                    + "\n" + ten);
     
            String script3 = s1.next();
            String script4 = s1.next();
            zombies = script3 + " " + script4;
            System.out.println(zombies);
     
            one2 = s1.nextInt();
            two2 = s1.nextInt();
            three2 = s1.nextInt();
            four2 = s1.nextInt();
            five2 = s1.nextInt();
            six2 = s1.nextInt();
            seven2 = s1.nextInt();
            eight2 = s1.nextInt();
            nine2 = s1.nextInt();
            ten2 = s1.nextInt();
     
            System.out.println(one2 + "\n" + two2 + "\n" + three2 + "\n" + four2
                    + "\n" + five2 + "\n" + six2 + "\n" + seven2 + "\n" + eight2
                    + "\n" + nine2 + "\n" + ten2);
     
        }
    }
    This now creates 2 Strings - 'zombies' & 'assasins'

    And all the Integer values for the first section are stored as 'one', 'two', 'three', 'four', etc...

    All the Integer values for the second section are stored as 'one2', 'two2', 'three2', 'four2', etc...

    You can now call these variables from anywhere in your code.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM
  2. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM
  3. Replies: 1
    Last Post: February 28th, 2009, 10:05 PM