Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Homework
1.Write a Java program having a String array, with global visibility.`
2.Add a method that adds a given string to the string array.
3.Add a method that searches for a given string in the string array.
4.Add a method that searches for a given character in the string array. The method should count and returns the occurrence of the given character.
5.Write an appropriate main method that reads from a text file named “input.txt” some strings and adds them to the String array. Then test the methods in 3, and 4 above.
 
here is the code I tried but it is not working!!
 
What should I do????
package arrays;
import java.util.Scanner;
import java.io.*;
 
public class Arrays 
{
    static String [] testArray;
    
    public static void addString (String str, int index)
    {
        testArray[index] = str;  
    }
    
    public static void searchString(String x)
    {
       System.out.println(x.indexOf("this"));
    }
    
    public static void characterString(String str, String sub)
    {
        System.out.println(str.indexOf('s'));
    }
 
    public static void main(String[] args) throws FileNotFoundException 
    {           
        testArray = new String[6];
 
        Scanner in = new Scanner (new FileReader("input.txt"));
        int i=0;
        while (in.hasNext())
        {
          String j = in.next();
          addString (j,i);
          i++;  
        for(String s: testArray)
            System.out.println(s);
        }
    }
}
 
This is the input file info.
Hello
Girls!
This
is
Programming
class..
Posted 27-Sep-12 11:08am
Edited 27-Sep-12 21:24pm
TorstenH.33.3K
v3
Comments
Sergey Alexandrovich Kryukov at 27-Sep-12 17:24pm
   
Is it really off-topic? Do I need to explain why? This is the site for software developers (students, too, of course); and the software developers do not say "it's not working", they provide comprehensive issue report.
 
It's not that I'm overly picky. It really needs explanation what do you want to achieve, what's the problem. And first of all, what is your questions?
Also, did you run it under debugger? If not, do it first and detect what is wrong precisely. Chances are, after that you will be able to fix this trivial thing by yourself.
 
What else? Ah, "Improve question" is above, please see. :-)
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

1.Write a Java program having a String array, with global visibility.
yepp, I can see that in there. You should mark it "public" too. That is not necessary, but it's common done that way to point out that this specific variable is public.
public static String [] testArray;
 
2.Add a method that adds a given string to the string array.
yepp, full points here.
3.Add a method that searches for a given string in the string array.
I would expect that to be a method that returns a boolean value indicating if the requested String is in the Array.
public static boolean searchString(String strRequest)
{
  foreach(String str: testArray){
    if(str.equlas(strRequest) return true; // direct return of find!
  }
  return false; // nothing found
}
But I'm nut sure if you already had functions returning a value?
Your code also does not work, you need to at least "ask" the Array.
 
4.Add a method that searches for a given character in the string array. The method should count and returns the occurrence of the given character.
Also non functional at your code. You need to search in the Array, each String if the given argument ("x") is in there. and counting is needed. So I expect that method to return a int value to the main.
 
5.Write an appropriate main method that reads from a text file named “input.txt” some strings and adds them to the String array. Then test the methods in 3, and 4 above.
You do not call your functions. But the reading of input file works - so go on and add the rest!
 
EDIT: Just a small one - ether variables and main on bottom end or both on top end. Don't split that.
  Permalink  
v4
Comments
Amber Forstre at 28-Sep-12 5:08am
   
I understand it now..
 
Thank you for explaining it to me
TorstenH. at 28-Sep-12 8:11am
   
Fine. Mark as "accept" and it will turn the question green.
You're welcome to ask more if in doubt.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 768
1 OriginalGriff 420
2 CPallini 275
3 George Jonsson 226
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,272
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 28 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100