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 12 of 12

Thread: Problem in reading a file from java class

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

    Default Problem in reading a file from java class

    ok my problem is that the class file i made is reading from a file called "data.dat" which looks like the following

    DATA.DAT
    Maximizor,50,50
    Robinator,50,50
    Rocket Launcher,20,10
    Sniper Rifle,15,25
    Colt .45,10,20
    Plasma Cannon,25,15
    Pew Pew Gun,30,20
    5,You've run into a rocky field that makes it hard to aim!
    9,There is a thick fog that makes it difficult to see!
    -3,The flat terrain helps to steady your arm!
    -7,An atmospheric gas has improved aim!
    0,A calm and cool afternoon.

    THIS IS SOME OF THE CODE FROM MY CLASS FILE

    File duel= new File ("duel.dat");
    Scanner r1n = new Scanner(duel);
    r1n.useDelimiter(",");
     
    for (int count=1; count <=3; count++)
    {
    linRead = r1n.next();
    System.out.println (linRead);
    }

    OUTPUT WHEN RUN
    Maximizor
    50
    50
    Robinator

    the problem is i get "Robinator" on the 3rd loop. How do i code it so that my output looks like this and i've already tried letting it only run twice but that didn't work. In the end i'm supposed to be able to assign each line to a variable ie.

    Maximizor = a1
    50 = w1
    50 = w2
    etc.


  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 a file

    Hello aznprdgy and welcome to the Java programming forums.

    Can you please post your entire code for me to look at?

    What is a1, w1, w2 etc?

    Is your desired output this:

    Maximizor
    50
    50
    Robinator
    50
    50
    Rocket Launcher
    20
    10
    Sniper Rifle
    15
    25
    Colt .45
    10
    20
    Plasma Cannon
    25
    15
    Pew Pew Gun
    30
    20
    5
    You've run into a rocky field that makes it hard to aim!
    9
    There is a thick fog that makes it difficult to see!
    -3
    The flat terrain helps to steady your arm!
    -7
    An atmospheric gas has improved aim!
    0
    A calm and cool afternoon.
    I need a better understanding of what you are doing.
    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 a file

    import java.util.Scanner;
    import java.io.*;
     
     
      public class mess
      {
        public static void main(String[] args) throws Exception
        {
            String linRead, linRead2,linRead3,linRead4,linRead5,linRead6,linRead7,linRead8,linRead9,linRead10,linRead11,linRead12; 
            String linRead13,linRead14,linRead15,linRead16,linRead17,linRead18,linRead19,linRead20,linRead21,linRead22,linRead23,linRead24,name;
            int accuracy, armor, skill, damage, number;
     
            name = "";
            armor = 0;
            skill = 0;
            damage = 0;
            accuracy = 0;
            number = 0;
     
     
            File duel= new File ("duel.dat");
            Scanner r1n = new Scanner(duel);
     
     
     
            RobotRB robot1= new RobotRB(name, armor, skill);
            RobotRB robot2= new RobotRB(name, armor, skill);
            WeaponsRB weapon1= new WeaponsRB(name, damage, accuracy, number);
            WeaponsRB weapon2 = new WeaponsRB(name, damage, accuracy, number);
            WeaponsRB weapon3 = new WeaponsRB(name, damage, accuracy, number);
            WeaponsRB weapon4 = new WeaponsRB(name, damage, accuracy, number);
            WeaponsRB weapon5 = new WeaponsRB(name, damage, accuracy, number);
     
           for (int count=1; count <=3; count++)
           {
                 linRead = r1n.next();
                System.out.println (linRead);
           }

    Yes thats the desired output. But im only using the println to see if the scanner is reading it correctly. I need to actually assign the values to something. For instance Maxinator needs to be assigned to a string value "name", the 50 under it needs to be assigned to a String value "Armor". I haven't implimented this part into the code yet since i'm still stuck at getting the scanner to read the file correctly. Also this is my first year in this course and i've missed a lot of classes due to illness so i'm not 100% sure if im saying things correctly or not so sorry if some things are unclear.


    Essentially this is what im trying to do
               r1n.useDelimiter(",");
                linRead = r1n.next();
                linRead = linRead.trim();
                name = linRead; //This value is Maxinator
                linRead2 = r1n.next();
                linRead2 = linRead2.trim();
                armor = linRead2; //This value is 50
                linRead3 = r1n.nextLine();
                linRead3 = linRead3.trim();
                skill = linRead3; // This value is supposed to be 50. But the problem occurs where it becomes 50 Robinator
    Last edited by aznprdgy; March 19th, 2009 at 09:18 AM.

  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

    Default Re: reading from a file

    I would personally input the file and split it like this:

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
     
    public class Aznprdgy {
     
        public static void main(String[] args) {
     
              try
              {
              FileInputStream in = new FileInputStream("DATA.DAT");
              BufferedReader br = new BufferedReader(new InputStreamReader(in));
              String strLine;
     
              String[] myString = null;
     
              while((strLine = br.readLine())!= null)
              { 
                  myString = strLine.split(",");
                  for(int i = 0; i < myString.length; i++){
     
                      System.out.println(myString[i]);
                  }
              }
     
              }catch(Exception e){
               System.out.println(e);
              }
     
             }
    }
    Now every line is an Array value.

    Will the DATA file always contain the same amount of lines?
    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.

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

    Default Re: reading from a file

    yes data file will contain the same line each time. But when i do
                      String name = myString[0];
                      System.out.println(name);

    it print out multiple lines from data file. When it should only print out Maxinator right?

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: reading from a file

    Personally I would use a Container class. Something like array list, it seems a sensible approach, plus it allows for your File to have as many lines as you wish.
    Here is the approach I took
    import java.util.ArrayList;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
     
    public class Main {
     
        public static void main(String[] args) {
            BufferedReader br = null;
            try{
                br = new BufferedReader(
                    new InputStreamReader(
                    new FileInputStream("DATA.DAT")));
            }catch(Exception e){
                System.err.println(e.getMessage());
            }
            ArrayList<String[]> OBJ = new ArrayList<String[]>();
            String ln;
     
            try{
                while((ln = br.readLine()) != null){
                    OBJ.add(ln.split(","));            
                }
            }catch(Exception e){
                System.err.println(e.getMessage());
            }
        }
    }

    Hope that helps,
    Chris

  7. #7
    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 a file

    Sorry I didn't test my code before I submitted it...

    The reason its printing out the list is because you haven't commented out System.out.println(myString[i]); in the for loop.

    I need to take another look at it though because it doesn't work like expected.

    Welcome to the forums Freaky Chris

    Thanks for helping with this issue.

    I have just tried to compile your code but for some reason it cannot find the DATA.DAT file even when I link to it directly.

    Any ideas?
    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.

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: reading from a file

    I used the full address for the file, C:\\Documents....you get the idea. I just removed it before posting, for security reasons and annoying line size.

    In NetBeans it always seems to have problems finding the file unless you specify the full path, or at least that is my findings.

    Chris

  9. #9
    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 a file

    Quote Originally Posted by Freaky Chris View Post
    I used the full address for the file, C:\\Documents....you get the idea. I just removed it before posting, for security reasons and annoying line size.

    In NetBeans it always seems to have problems finding the file unless you specify the full path, or at least that is my findings.

    Chris
    Yes I was specifying the full path. Just realised my file was renamed DATA.DAT.txt. ooops. Your code is compiling fine
    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.

  10. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: reading from a file

    Quote Originally Posted by JavaPF View Post
    Yes I was specifying the full path. Just realised my file was renamed DATA.DAT.txt. ooops. Your code is compiling fine
    Swoosh lol. Got to love silly little mis-takes

    Chris

  11. #11
    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 a file

    Hey aznprdgy,

    I'm trying to come up with a better way to do this but I think the reason your code is falling over is because you are using "," as a delimiter and when it gets to the end of the first line in the file the nextInt() is trying to pickup "50 Robinator" which obviousally wont work as this value also contains a String.

    I suggest you add , to the end of each line in the file. For example:

    Maximizor,50,50,
    Robinator,50,50,
    Rocket Launcher,20,10,
    Sniper Rifle,15,25,
    Colt .45,10,20,
    Plasma Cannon,25,15,
    Pew Pew Gun,30,20,
    5,You've run into a rocky field that makes it hard to aim!,
    9,There is a thick fog that makes it difficult to see!,
    -3,The flat terrain helps to steady your arm!,
    -7,An atmospheric gas has improved aim!,
    0,A calm and cool afternoon.,
    Try running that through your code and let me know what happens...
    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.

  12. #12
    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 a file

    OK i've come up with a better method for you to use.

    Try this:

    import java.util.Scanner;
    import java.io.*;
     
    public class Aznprdgy {
     
        public static Scanner sc;
        public static String name;
        public static int damage;
        public static int accuracy;
     
        public static String w1, w2, w3, w4, w5, w6, w7;
        public static int d1, d2, d3, d4, d5, d6, d7;
        public static int a1, a2, a3, a4, a5, a6, a7;
     
        public static void main(String[] args){
     
                 try{
                File data = new File ("DATA.DAT");
                    sc = new Scanner(data);
                 }
                 catch(Exception e){
                     System.out.println("Error: Data file not found! " + e);
                 }
                 sc.useDelimiter(",");
     
                 name = sc.next();
                 damage = sc.nextInt();
                 accuracy = sc.nextInt();
                 Weapon1(name, damage, accuracy);
     
                 name = sc.next();
                 damage = sc.nextInt();
                 accuracy = sc.nextInt();
                 Weapon2(name, damage, accuracy);
     
                 name = sc.next();
                 damage = sc.nextInt();
                 accuracy = sc.nextInt();
                 Weapon3(name, damage, accuracy);
     
                 name = sc.next();
                 damage = sc.nextInt();
                 accuracy = sc.nextInt();
                 Weapon4(name, damage, accuracy);
     
                 name = sc.next();
                 damage = sc.nextInt();
                 accuracy = sc.nextInt();
                 Weapon5(name, damage, accuracy);
     
                 name = sc.next();
                 damage = sc.nextInt();
                 accuracy = sc.nextInt();
                 Weapon6(name, damage, accuracy);
     
                 name = sc.next();
                 damage = sc.nextInt();
                 accuracy = sc.nextInt();
                 Weapon7(name, damage, accuracy);
     
     
                 //Print out each gun info (name, damage, accuracy)
                 System.out.println(w1 + "\n" + d1 + " " + a1);
                 System.out.println(w2 + "\n" + d2 + " " + a2);
                 System.out.println(w3 + "\n" + d3 + " " + a3);
                 System.out.println(w4 + "\n" + d4 + " " + a4);
                 System.out.println(w5 + "\n" + d5 + " " + a5);
                 System.out.println(w6 + "\n" + d6 + " " + a6);
                 System.out.println(w7 + "\n" + d7 + " " + a7);
     
        }
     
        public static void Weapon1(String w1name, int w1damage, int w1accuracy){
     
            w1 = w1name;
            d1 = w1damage;
            a1 = w1accuracy;
        }
     
        public static void Weapon2(String w2name, int w2damage, int w2accuracy){
     
            w2 = w2name;
            d2 = w2damage;
            a2 = w2accuracy;
        }
     
        public static void Weapon3(String w3name, int w3damage, int w3accuracy){
     
            w3 = w3name;
            d3 = w3damage;
            a3 = w3accuracy;
        }
     
        public static void Weapon4(String w4name, int w4damage, int w4accuracy){
     
            w4 = w4name;
            d4 = w4damage;
            a4 = w4accuracy;
        }
     
        public static void Weapon5(String w5name, int w5damage, int w5accuracy){
     
            w5 = w5name;
            d5 = w5damage;
            a5 = w5accuracy;
        }
     
        public static void Weapon6(String w6name, int w6damage, int w6accuracy){
     
            w6 = w6name;
            d6 = w6damage;
            a6 = w6accuracy;
        }
     
        public static void Weapon7(String w7name, int w7damage, int w7accuracy){
     
            w7 = w7name;
            d7 = w7damage;
            a7 = w7accuracy;
        }
     
    }

    This now splits up each weapon nicely and you can easily call its attributes.

    w1 = weapon1s name
    d1 = weapon1s damage
    a1 = weapon1s accuracy

    and so on...

    Please note that the data file needs to have , at the end of each line.

    Maximizor,50,50,
    Robinator,50,50,
    Rocket Launcher,20,10,
    Sniper Rifle,15,25,
    Colt .45,10,20,
    Plasma Cannon,25,15,
    Pew Pew Gun,30,20,
    5,You've run into a rocky field that makes it hard to aim!,
    9,There is a thick fog that makes it difficult to see!,
    -3,The flat terrain helps to steady your arm!,
    -7,An atmospheric gas has improved aim!,
    0,A calm and cool afternoon.,
    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] Program to read from created file
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 7th, 2009, 03:51 AM
  3. Replies: 1
    Last Post: February 28th, 2009, 10:05 PM