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

Thread: simple code help required

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    My Mood
    Amazed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default simple code help required

    import java.io.*;
    import java.util.Scanner;
    import java.util.Vector;
     
    public class test3 {
     
        public static void main(String[] args) throws IOException {     
            int counter = 0; 
            String line = null;
     
             // Location of file to read
            File file = new File("test.txt");
     
            try {
     
                Scanner scanner = new Scanner(file);
     
                while (scanner.hasNextLine()) {
                    line = scanner.nextLine();
                    System.out.println(line);
                    //counter++;                    
                }
                scanner.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }           
            //System.out.println(counter);        
        }
    }

    characters from file into an array...


    assuming there are 4 arrays

    array 0
    array 1
    array 2
    array 3

    and string is THISISASTRING or THIS IS A STRING

    array 0 TITG
    array 1 HSR
    array 2 IAI
    array 3 SSN

    values to be input in such an order..

    am unable to store the file string into a string currently for now.


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: simple code help required

    Quote Originally Posted by rzzz View Post
    am trying to get the application open file and copy details from it but it kind of doesnt work...
    The exception handling can be better, and the copy can be optimized (reading 1 char at time is a bad approach) but .... despite this, it *works*. The result is what the code says, a new file with a copy of all characters.

    So, what's wrong?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: simple code help required

    but it kind of doesnt work
    Please, do everyone a favor and don't post vague, ambiguous, essentially worthless descriptions for help like this. Describe EXACTLY what is wrong with the code you've posted, provide a sample run with inputs/outputs, if possible. Post error messages, if any. Be specific, ask specific questions, communicate as clearly as you can.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: simple code help required

    hi rzzz : i think you means you cant put character to array; pls see followed

    Code removed.
    Last edited by GregBrannon; January 12th, 2014 at 08:10 AM. Reason: Do not provide solutions. Suggestions are sufficient.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: simple code help required

    @Billzhang:

    Please do not provide solutions. Also, review this link to learn how to post code correctly along with other useful info for newcomers.

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    My Mood
    Amazed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: simple code help required

    assuming there are 4 arrays

    array 0
    array 1
    array 2
    array 3

    and string is THISISASTRING

    array 0 TITG
    array 1 HSR
    array 2 IAI
    array 3 SSN

    values to be input in such an order..

    am unable to store the file string into a string currently for now.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: simple code help required

    And why do (you think) you need to do that? What's the objective? What's the program supposed to do? How will those arrays accomplish the program's objective? There is almost always a preferred alternative to using parallel arrays to store/manipulate related data.

    What you've shown is the beginning of a very big and unnecessary headache. If you answer the questions above, we should be able to suggest better approaches.

  8. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    5
    My Mood
    Amazed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: simple code help required

    Hi Greg

    The initial objective is to transposite arrays.

    array 0 TITG
    array 1 HSR
    array 2 IAI
    array 3 SSN

    whereby arrays can be shuffled in any sequence,. subsequently the sequence has to be reversed to get back the initial string

Similar Threads

  1. Need help with simple code
    By DreC32 in forum Object Oriented Programming
    Replies: 7
    Last Post: August 6th, 2013, 01:55 PM
  2. Looking for a simple code
    By Pipar in forum Java Theory & Questions
    Replies: 7
    Last Post: July 22nd, 2013, 01:12 PM
  3. Replies: 0
    Last Post: November 16th, 2012, 05:25 AM
  4. Replies: 4
    Last Post: February 26th, 2012, 05:36 PM
  5. Need help with simple code
    By suxen in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 21st, 2011, 08:10 PM