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

Thread: how do i make my program to do......

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do i make my program to do......

    I got stuck while doing my assignment which is to:

    1. Read in all 20 lines of text from a file and store them.
    2. Display all the 20 lines of text on the screen
    3. Display every other line in upper case (so the first, then the third etc), you don’t need to display the other line at all (i.e. no output of second, fourth etc.)
    4. Display the number of characters in each line of text and display the average of the characters for all 20 lines of text.
    5. Display the first 10 characters from each line of text


    I did the first two tasks.....and my code for that is
    import java.util.Scanner;
    import java.io.File;
     
     
    class Displaytext{
     
    	public static void main (String args[])throws Exception {
     
    		String[]text=new String[20];
    		File myfile = new File("alice.txt");
    	    Scanner scan = new Scanner(myfile);
            String alice;
     
     
     
    	  for (int a=0; a<20; a++)
    	    {
     
     
     
    			alice = scan.nextLine();
    			System.out.println(alice);
     
    		}
     
    	    scan.close();
     
    	}
    }

    Can someone help me in altering this so that it does the rest of the tasks........


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how do i make my program to do......

    When posting code, make sure you use the code tags to preserve formatting. Otherwise nobody will read it.

    But as for your other requirements, what have you tried? Hint: You'll probably want to store your Strings in an Array or a List.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: how do i make my program to do......

    A few notes on what you have now. First, please put JAVA tags around your code, it makes it easier to read and ensures faster and better assistance. To figure out how to use JAVA tags, read my signature.

    Second, based on the assignment, I can understand the loop going to 20, but when you use Scanner, you usually want to go until there are no more lines left. OR, at least check to see if the line you are reading in exists. Basically, scan.nextLine(); will return null when the next line doesnt exist. So, there are two ways that this can be done more efficiently. First, if you want to read all of the lines, instead of
    String alice;
     
    for (int a=0; a<20; a++)
    {
    alice = scan.nextLine();
    System.out.println(alice);
    }
    you would want something like:
    String alice = scan.nextLine();
     
    while(alice!=null)
    {
    	System.out.println(alice);
    	alice = scan.nextLine();
    }

    Alternatively, if you want to read only 20 lines, you should put a check in your statement:
    for (int a=0; a<20; a++)
    {
    	alice = scan.nextLine();
    	if(alice==null)
    		break;
    	System.out.println(alice);
    }

    This guarantees you will not continue to read when you reach the end of the file and prevents possible exceptions from being thrown.

    3. Display every other line in upper case (so the first, then the third etc), you don’t need to display the other line at all (i.e. no output of second, fourth etc.)
    4. Display the number of characters in each line of text and display the average of the characters for all 20 lines of text.
    5. Display the first 10 characters from each line of text
    I will tell you how to do all of these, but not give you the code for it. Try to figure out the code based on the directions I give and if you have any trouble with the code, post it and I'll help you debug.
    3. So the first thing we need to do is remember each line when we read it in. I noticed you already made a String[] named text. This is where you want to store your lines of text. So, when you read in the line of text in your loop above, you should also add that line of text to the String array. Then, after you have read in all of the lines, you want to go through the array and get each String that has an even index. Why even? Because the indexes start at 0. So you want to get indexes 0,2,4,6,...16,18. Remember that index #20 does not exist because indexes start at 0 and end at the length of the array minus 1. When you get each String, you want to use the String.toUpperCase() method. Remember that this method returns a String, not replace the String the method was evoked from. So, you simply want to print out the String the method returns.
    4. Now that we have our array of Strings from #3, we can go through the array again. Before looping through the array, you need to declare a variable that will be the total of all the length. Then when you go through each index in the array, use the String.length() method. This method returns an int representing how many characters are in the String. You want to print that value out, but you also want to add that value to our total variable. Lastly, when you exit the loop, you want to divide the total by 20 (since that is the number of Strings we have) and print out that result as well.
    5. Once again, we go through the array of Strings. This time, we use the String.substring(int,int) method. In this method, the first int is the starting index and the second int is the ending index. This method returns a String of just the characters between the indexes you identified. So, you want to get each String in the array, and print out the value of String.substring(0,10);.

    I think that should be everything. Tell me if you have any trouble.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do i make my program to do......

    Quote Originally Posted by aussiemcgr View Post
    A few notes on what you have now. First, please put JAVA tags around your code, it makes it easier to read and ensures faster and better assistance. To figure out how to use JAVA tags, read my signature.

    Second, based on the assignment, I can understand the loop going to 20, but when you use Scanner, you usually want to go until there are no more lines left. OR, at least check to see if the line you are reading in exists. Basically, scan.nextLine(); will return null when the next line doesnt exist. So, there are two ways that this can be done more efficiently. First, if you want to read all of the lines, instead of
    String alice;
     
    for (int a=0; a<20; a++)
    {
    alice = scan.nextLine();
    System.out.println(alice);
    }
    you would want something like:
    String alice = scan.nextLine();
     
    while(alice!=null)
    {
    	System.out.println(alice);
    	alice = scan.nextLine();
    }

    Alternatively, if you want to read only 20 lines, you should put a check in your statement:
    for (int a=0; a<20; a++)
    {
    	alice = scan.nextLine();
    	if(alice==null)
    		break;
    	System.out.println(alice);
    }

    This guarantees you will not continue to read when you reach the end of the file and prevents possible exceptions from being thrown.


    I will tell you how to do all of these, but not give you the code for it. Try to figure out the code based on the directions I give and if you have any trouble with the code, post it and I'll help you debug.
    3. So the first thing we need to do is remember each line when we read it in. I noticed you already made a String[] named text. This is where you want to store your lines of text. So, when you read in the line of text in your loop above, you should also add that line of text to the String array. Then, after you have read in all of the lines, you want to go through the array and get each String that has an even index. Why even? Because the indexes start at 0. So you want to get indexes 0,2,4,6,...16,18. Remember that index #20 does not exist because indexes start at 0 and end at the length of the array minus 1. When you get each String, you want to use the String.toUpperCase() method. Remember that this method returns a String, not replace the String the method was evoked from. So, you simply want to print out the String the method returns.
    4. Now that we have our array of Strings from #3, we can go through the array again. Before looping through the array, you need to declare a variable that will be the total of all the length. Then when you go through each index in the array, use the String.length() method. This method returns an int representing how many characters are in the String. You want to print that value out, but you also want to add that value to our total variable. Lastly, when you exit the loop, you want to divide the total by 20 (since that is the number of Strings we have) and print out that result as well.
    5. Once again, we go through the array of Strings. This time, we use the String.substring(int,int) method. In this method, the first int is the starting index and the second int is the ending index. This method returns a String of just the characters between the indexes you identified. So, you want to get each String in the array, and print out the value of String.substring(0,10);.

    I think that should be everything. Tell me if you have any trouble.

    HEY THANKS VERY MUCH FOR UR HELP.....i got around fine with those tasks.....but can u give me some direction for the following tasks aswell...........i really appreciate ur help

    6. Ask the user for a word to find in the text. Then display all the text in lower case, except for the target word, which should be in upper case. Also display how many times the target word was found.
    7. Modify the code so that it works with a text file of up to 300 lines of text. Your program should handle no lines, one line or any where up to 300 lines.

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how do i make my program to do......

    Quote Originally Posted by andys View Post
    I got stuck while doing my assignment which is to:

    1. Read in all 20 lines of text from a file and store them.
    2. Display all the 20 lines of text on the screen
    3. Display every other line in upper case (so the first, then the third etc), you don’t need to display the other line at all (i.e. no output of second, fourth etc.)
    4. Display the number of characters in each line of text and display the average of the characters for all 20 lines of text.
    5. Display the first 10 characters from each line of text


    I did the first two tasks.....and my code for that is

    import java.util.Scanner;
    import java.io.File;


    class Displaytext{

    public static void main (String args[])throws Exception {

    String[]text=new String[20];
    File myfile = new File("alice.txt");
    Scanner scan = new Scanner(myfile);
    String alice;



    for (int a=0; a<20; a++)
    {



    alice = scan.nextLine();
    System.out.println(alice);

    }

    scan.close();

    }
    }

    Can someone help me in altering this so that it does the rest of the tasks........
    Hi its Melanie, sounds like your assignment is not going to well!! Just to let you know, you may wish to take a look at the Academic offences, located on myBu. I have advised Nan (who will be marking the work this week) that any work that is believed to be plagarised, will be marked as 0 with a disiplinary with Ruth.

  6. #6
    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: how do i make my program to do......

    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.

  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: how do i make my program to do......

    Quote Originally Posted by MelanieC View Post
    Hi its Melanie, sounds like your assignment is not going to well!! Just to let you know, you may wish to take a look at the Academic offences, located on myBu. I have advised Nan (who will be marking the work this week) that any work that is believed to be plagarised, will be marked as 0 with a disiplinary with Ruth.
    Please be careful not to break any rules. It looks like your University are monitoring students on this forum.
    Try to complete as much of the code as possible and we will point you in the right direction when you are stuck..
    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. How to make an error message when my program crashes?
    By noFear in forum Java Theory & Questions
    Replies: 10
    Last Post: August 11th, 2010, 08:50 AM
  2. How To Make The Program Jump To The Next Function..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 3rd, 2010, 12:33 AM
  3. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 07:09 PM
  4. Help with a program to make a landscape picture
    By noseeds in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 10:25 PM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM