Results 1 to 3 of 3

Thread: JAVA - Read values in from text file

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Thanks
    0
    Thanked
    0 times in 0 posts

    JAVA - Read values in from text file

    Howdy

    Need some simple assistance with Java programming. Can't quite crack it.

    I have a text file that has information as below
    Code:
    (1,7)(2,0)(4,0)(5,0)
    (4,0)(2,0)(4,0)(5,0)
    (1,1)(4,1)(1,25)(4,0)
    (5,0)(4,0)(5,0)(6,1)
    (5,0)(5,0)(5,0)(5,0)
    I need to read in integer 1 and assign it to an integer (cmd) inside my program, and then read in integer 2 and assign it to another integer (data). It should then keep rotating through the text file until it comes to the end.

    The code I currently have in this method is -
    Code:
     public static load()
      {
       Scanner scanner = new Scanner(new File(memory.dat));
       int reg1 = 0;
       int reg2 = 0;
       int cmd = File(:, 1); //I stole this from MATLAB to read in from the file, but it didn't work, though I didn't think it would
       int data = File(:,2);
       
       if (cmd == 1)
        {
    	 reg1 = data;
    	}
       else
        if (cmd == 2)
    	 {
    	  reg1 = reg1 + 1;
    	  if (reg1 > 26)
    	   {
    	    reg1 = 26;
    	   }
    	 }
        else
         if (cmd == 3)
          {
           reg1 = reg1 - 1;
    	   if (reg1 < 0)
    	    {
    	     reg1 = 0;
    		} 
          }
    	 else
    	  if (cmd == 4)
    	   {
    	    int temp = reg1;
    		reg1 = reg2;
    		reg2 = temp;
    	   }
    	  else
    	   if (cmd == 5)
    	    {
    		 ascii = reg1 + 65;
    		 System.out.println((char)ascii);
    		} 
      }
    If there has been a thread with this kind of assistance in it please direct me to it.

    Thanks

    Josh

  2. #2
    Senior Member
    Join Date
    Jul 2004
    Location
    London
    Posts
    2,456
    Thanks
    100
    Thanked
    75 times in 51 posts
    • Mblaster's system
      • Motherboard:
      • ASUS PK5 Premium
      • CPU:
      • Intel i5 2500K
      • Memory:
      • 8gb DDR3
      • Storage:
      • Intel X25 SSD + WD 2TB HDD
      • Graphics card(s):
      • Nvidia GeForce GTX 570
      • PSU:
      • Corsair HX520
      • Case:
      • Antec P180
      • Operating System:
      • Windows 7 Professional x64
      • Monitor(s):
      • HP w2207 (22" wide)
      • Internet:
      • Rubbish ADSL

    Re: JAVA - Read values in from text file

    The link below explains how to read from a file. String.split(" ") will help you get each pair of numbers out, and you'll want a loop around the main section of code if it's to be run for every pair of numbers.

    Also, you'll probably find it easier to read the whole file to a variable then process it, rather than reading each number then process then read, then process etc.

    http://www.abbeyworkshop.com/howto/j...ile/index.html

    And a helpful website, the Java API: http://java.sun.com/javase/6/docs/api/
    Last edited by Mblaster; 16-05-2010 at 09:40 PM.
    I don't mean to sound cold, or cruel, or vicious, but I am so that's the way it comes out.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Thanks
    0
    Thanked
    0 times in 0 posts

    Re: JAVA - Read values in from text file

    Thanks for that. Helped a bit, but I'm still trying to figure out how to incorporate that in to my method. Can you assist by incorporating it into the method?

    Josh

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. DVD Writer "Track following error"
    By mikulas in forum Help! Quick Relief From Tech Headaches
    Replies: 7
    Last Post: 08-05-2006, 06:19 PM
  2. DVD DL on NEC ND-2510A f/w 2.18
    By koocha in forum Help! Quick Relief From Tech Headaches
    Replies: 8
    Last Post: 06-03-2006, 06:15 PM
  3. Reading text file in access
    By Basher in forum Software
    Replies: 2
    Last Post: 18-11-2005, 02:59 PM
  4. Replies: 13
    Last Post: 03-05-2005, 08:21 AM
  5. Java file reading and Stroing in array
    By Basher in forum Software
    Replies: 16
    Last Post: 07-04-2004, 09:19 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
  •