Link to home
Start Free TrialLog in
Avatar of SeanBarton
SeanBarton

asked on

Help me.. java problem..one liner

im trying to get my array of strings to be understood by the compiler but am getting nowhere:

String [] cities = {"abAberdeen.....","biBirmingham...","caCambridge...","doDoncaster....","evEvesham......","foFolkstone....","guGuildford....","haHatfield.....","icIckleton.....","jaJarrow.......","kiKidderminster","loLondon......."};
      
the idea is to substring off the first two letters later....its for a postcode program.

but this onel iine refuses to be declared.

ive tried putting { or [ at the begginnning and end but either way it wont compile.

Sean
SOLUTION
Avatar of jimmack
jimmack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
There's nothing wrong with it that i can see. It's possibly its context that's a problem
Is it a compilation error (I do not think so - this (        String [] s ={"dad", "ds"}; ) is compiling fine) or what?
Avatar of SeanBarton
SeanBarton

ASKER

     public static void main(String[]args)
      {
      
      String1 = "val1, val2, val3, val4";
      String[] cities = String1.trim().split(" *, *" );
;
      //String [] cities = {"abAberdeen.....","biBirmingham...","caCambridge...","doDoncaster....","evEvesham......","foFolkstone....","guGuildford....","haHatfield.....","icIckleton.....","jaJarrow.......","kiKidderminster","loLondon......."};
      
      //String inputStr
      //String MyStr;
      //System.out.println("please enter a full uk postcode");
      //int compare;
      //int gotit=1;
      
      System.out.println(cities[1]);
      System.out.println(cities[2]);
      System.out.println(cities[3]);
      System.out.println(cities[4]);
      /*
      System.out.println(cities[5]);
      System.out.println(cities[6]);
      System.out.println(cities[7]);
      System.out.println(cities[8]);
      System.out.println(cities[9]);
      System.out.println(cities[10]);
      System.out.println(cities[11]);
      System.out.println(cities[12]);
      */
      /*
      inputStr = KBInput.readString();
      
      //sort out compareto code
      
      for(int i=0;i<15;i++)
            {
                  MyStr=cities[i].substring(0,1);
                  
                  if(gotit==inputStr.compareTo(MyStr))
                  System.out.print(i+",");
                  {
            
                  MyStr=cities[i].substring(2,11);            
                  System.out.println(MyStr);
                  }
            }
            */
                  
      
      }
}
ignore 3rd and fourth lines..they were experiments
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
womderful...keep sending in suggestions plz...this is great help
instead this :for(int i=0;i<15;i++)

use
int size = cities.length;
for(int i=0;i<size;i++)
What do you want to achieve with this?
MyStr=cities[i].substring(2,11);  

To take all the symbols  without the first two? Then you may  use  MyStr=cities[i].substring(2);  
This will work even if your strings have different sizes.
SeanBarton,

What's up? CAn you tell us what troubles you have after all these proposals?
>>womderful...keep sending in suggestions plz...this is great help

Strictly speaking, the idea is not to keep adding to the thread until your classwork is done SeanBarton ;-) It's to help solve a specific problem. Your compilation problem was caused by what i posted at 01/07/2004 08:02AM PST
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i apologise for exploiting you experts in my classwork.... however i need to understand it before i can write my own code...ive done the majority and included all of your suggestions

i have integrated every hint and tip you have all given me and now the original problem has sorted itself out...

how ever i was hoping for some help again...

i am going to use the array as a mass data store for the first two lettrers of a postcode followed by the name of the town. i want to know how to tidy this lot up..i have added everyones suggestions and still am getting nowhere. mainly because of oither problems.

      int size = cities.length;     -------what does this line do
      for(int i=0;i<size;i++)      -------and this one
            {
                  MyStr=cities[i].substring(0,1);     -----am i right in thinking that this will take the first2letters of the array and put it in a new string called mystr
                  
                  if(inputStr.equalsIgnoreCase(MyStr))     -----this was a suggestion by objects--- where will it store the result? how does it work?im confused as to how i can use it
                            {
                                                System.out.print(i+",");
                  
                        MyStr=cities[i].substring(2);        
                        System.out.println(MyStr);
            }
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
im very happy now thankyou... here is my new updated code...

class postcode
{
      public static void main(String[]args)
    {
     
          String [] cities = {"abAberdeen.....","biBirmingham...","caCambridge...","doDoncaster....","evEvesham......","foFolkstone....","guGuildford....","haHatfield.....","icIckleton.....","jaJarrow.......","kiKidderminster","loLondon......."};
     
          String inputStr;
          String MyStr;
          String inputStrSub;
     int compare;
          int gotit=1;
     int size = cities.length;
 
         System.out.println(cities[0]);
         System.out.println(cities[1]);
         System.out.println(cities[2]);
         System.out.println(cities[3]);
          System.out.println(cities[4]);
         System.out.println(cities[5]);
          System.out.println(cities[6]);
          System.out.println(cities[7]);
          System.out.println(cities[8]);
          System.out.println(cities[9]);
          System.out.println(cities[10]);
          System.out.println(cities[11]);

   //so i now know that the array has been loaded...
   
         System.out.println("please enter a full uk postcode");
         inputStr=KBInput.readString();
             
            System.out.println();
            System.out.println();
            System.out.println("postcode entered was: "+inputStr);
            System.out.println();
            System.out.println();

            inputStrSub=inputStr.substring(0,2);
            
            System.out.println("original input was: "+inputStr +" ,new is: "+inputStrSub);
            System.out.println();
            System.out.println();

            
         for(int i=0;i<size;i++)
                {
                MyStr=cities[i].substring(0,2);    
               
     if(inputStr.equalsIgnoreCase(MyStr))  
      {
             System.out.print(i+",");
               
        MyStr=cities[i].substring(2);      
        System.out.println(MyStr);
      }
    }
      }
}

if im correct tis will accept a postcode...strip off down to the first2characters...and then die horribly

im having trouble understanding how the next bit of code actually does what i want it to...

i need it to take inputStrSub and compare it to the cities array and output a nice nsg to say here is your city and original postcode.

i can see how im going to do it in my head but the actual code doest come out.

any help would be appreciated

Sean
you virtually do that already, you just need to change the message you print out when you find a match to be what you want. eg.

System.out.println("My city is "+MyStr);
and where do i put that?

im confused as to where i am going with this because ive put in little test lines and even with a match it just bypasses the loop...
          for(int i=0;i<size;i++)
                {
                System.out.println("this is a loop");
                MyStr=cities[i].substring(0,2);    
               
                if(inputStr.equalsIgnoreCase(MyStr))  
            {
             
                   System.out.print(i+",");
               
              MyStr=cities[i].substring(2);      
               System.out.println(MyStr);
            }
            else
            {      
                  System.out.println("hello");
            }
          }

i put it to say this is a loop every time it goes by and hello everytime the match is false...each time i run the program....even with a good match it fails
>  System.out.println("this is a loop");

change tis line to following so you can see what is being compared:

 System.out.println("this is a loop "+inputStr+":"+MyStr);
did that...now says that inputStr may not have been initialised

im just wondering why it returns false every time even if the answer is correct...

im close but it refuses to work

thanks for the quick reples by the way...ive already used your as the accepted answer
looking at your code i can't see whay you'd get that error, but I did notice that you should be comaping with inputStrSub, and not inputStr.
Try that and see if it helps.
believe it or not i was just about to post a msg to tell you that myself....i was very impressed i found my own problem...

now its working ish.... one last problem and then im really finished..

          for(int i=0;i<size;i++)
                {
                 MyStr=cities[i].substring(0,2);    
               
                if(inputStrSub.equalsIgnoreCase(MyStr))  
            {
             
              MyStr=cities[i].substring(2);      
               System.out.println("your city was: "+MyStr);
            }

this is the code...i need it to displaya msg if the postcode is not on file but not every time round as if i added an else stastement with a comment...

any suggesrtions??

Sean
> i was very impressed i found my own problem...

good work :-)

> i need it to displaya msg if the postcode is not on file but not every time round

either use a flag (boolean) to indicate whether you found a match or not.
Or instead of printing the results when you find it, instead store the city name in a variable. Then when the loop has completeed check if that variable has been set or not and display appropriate message.
SeanBarton, objects was only one of several people who helped in this thread - why did you give him ALL the points?
sorry people this is my first time using this website...i dont fully understand it...if i can take them back and redistibute i will

Sean Barton
yes please...i would like a fair share to all major helpers to my program

SeanBarton
is that a problem if it is?

because i wont do it if it is

SeanBarton
It's not allowed to have more than one account (according to the Membership Guidelines)

http://help.jsp#hi29

http://memberAgreement.jsp
ok i apologise to whom it may concern

please close my SeanBarton1 account and i will use SeanBarton from now on

thankyou for the warning
thanks...sorted now you casn close other accnt4me

SeanBarton
Thanks SeanBarton