Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By wishmaster

Thread: operation with strings

  1. #1
    wishmaster is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default operation with strings

    I have to write a java programm,where i have given string. Output should be like that:

    1. print only once what characters are apearinng in string, and the last index of it
    2. print how many characters are in the string
    I have to do it only with loops,no special classes

    So far:

    public static void main( String[] args ) {
    String besedilo = "Testiranje";
    besedilo = besedilo.toLowerCase();
    for (int i=0; i<besedilo.length();i++)
    {

    for (int j = i+1; j<besedilo.length(); j++)
    {
    if (besedilo.charAt(j) != besedilo.charAt(i))
    continue;
    System.out.println(besedilo.charAt(i));
    break;
    }

    }

    }


    tnx for the help

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: operation with strings

    Okay, but you have to do this yourself and that will likely require you to do some reading to figure out how.

    So do you have an actual question?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    wishmaster is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Re: operation with strings

    Quote Originally Posted by gimbal2 View Post
    Okay, but you have to do this yourself and that will likely require you to do some reading to figure out how.

    So do you have an actual question?
    im trying....
    question is,whats wrong in my if statement?

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: operation with strings

    You'll have to explain what you are getting right now and what you are expecting. What you're asking now is "I have this code. what's wrong?"
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    wishmaster is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Re: operation with strings

    Now i get the result like: tttttttteeeeeeesssssssttttttiiiiirrrraaannj
    I have to get the output,so that will write me chars that are apearing in my string,and only once,and the index when it appears last
    For example: t[2] e[3] s[2] i[4] r a n j

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: operation with strings

    Yes, so you'll have to modify the code to fit the new requirements. Experiment, try stuff, read and learn. Do normal student things to get your homework done.

    Right now your code does the exact opposite of what you want; it prints characters when they stay the same and it skips ('continue') printing a character when it changes. So step 1: get it to print the characters only once.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    wishmaster is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Re: operation with strings

    Quote Originally Posted by gimbal2 View Post
    Yes, so you'll have to modify the code to fit the new requirements. Experiment, try stuff, read and learn. Do normal student things to get your homework done.

    Right now your code does the exact opposite of what you want; it prints characters when they stay the same and it skips ('continue') printing a character when it changes. So step 1: get it to print the characters only once.
    will try to...im completely new to java,so it takes time!
    gimbal2 likes this.

  8. #8
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: operation with strings

    Hi,

    my suggestion is that you play around to find out how you would do it.

    - So just write a word on a piece of paper and think about how YOU could do it.
    - Just write down the process that you would follow.
    - Then retry it. Make sure that you do exactly as written down in the step before. If you find out that something is missing, then you have to step back and change the process.
    - When you verified that the process is working: Try to translate it to Java. Start with high level items that you break down (That way you will get multiple methods and your code will be easier to read). Make sure that you find good names. so if you have a step that drives the car into the garage, then your method should have that name: driveCarIntoGarage().

    Konrad

  9. #9
    wishmaster is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Re: operation with strings

    So thats the code for now:
    Java Code:
    public class testiranja {
    
    	public static void main( String[] args ) {
    		String besedilo = "abcdaabb";
    		besedilo = besedilo.toLowerCase();
    		
            zunanja:
    
            for (int i=0; i<besedilo.length();i++)
    		{
    			
    			for (int j = i+1; j<besedilo.length(); j++)
    			{
    				if (besedilo.charAt(j) == besedilo.charAt(i))
    					continue zunanja;
    			}
    			System.out.println(besedilo.charAt(i));
    		}
    		
    	}
    }
    i get the chars right,i just dont know how to write the last index of char apearance
    Last edited by JosAH; 05-06-2014 at 11:23 AM. Reason: added [code] ... [/code] tags

  10. #10
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,894
    Rep Power
    5

    Default Re: operation with strings

    You already have it, you're just not printing it yet! Think about it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    wishmaster is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Re: operation with strings

    My finished code:

    public class testiranja {

    public static void main( String[] args ) {
    String besedilo = "Sodba v zadevi Patria"+
    "temelji na domnevi krivde,"+
    "in ne na domnevi nedolžnosti,"+
    "so zapisali v društvu Evropska Slovenija,"+
    "ki mu predseduje nekdanji šef Slovenske"+
    "varnostno-obveščevalne službe Damir Črnčec.";
    besedilo = besedilo.toLowerCase();
    int SkupajZnakov = 0;

    zunanja:

    for (int i=0; i<besedilo.length();i++)
    {
    for (int j = i+1; j<besedilo.length(); j++)
    {

    if (besedilo.charAt(j) == besedilo.charAt(i))
    continue zunanja;
    }
    System.out.print(besedilo.charAt(i));
    System.out.print(" -indeks "+i);
    System.out.println("");
    SkupajZnakov++;

    }
    System.out.println("Različnih znakov v besedilu je "+SkupajZnakov);
    }
    }

    And sorry that is not in english.

    last println prints out the sum of different chars.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,445
    Blog Entries
    7
    Rep Power
    20

    Default Re: operation with strings

    Quote Originally Posted by wishmaster View Post
    i get the chars right,i just dont know how to write the last index of char apearance
    Think how you would do it by hand: keep a sheet of paper and a pencil ready; move over the String from right to left; for every character check your sheet of paper; if the letter isn't there yet, write it down together with its index, otherwise do nothing (you found this letter before, more to the right in the String).

    When you're ready, the sheet of paper contains what you want, so print it out.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Calendar operation
    By HEYBUDDY in forum New To Java
    Replies: 4
    Last Post: 09-23-2012, 02:06 PM
  2. Matrix Value operation
    By jaimitoc30 in forum New To Java
    Replies: 0
    Last Post: 04-09-2012, 06:15 PM
  3. Technologies and Operation
    By m jawad in forum Threads and Synchronization
    Replies: 1
    Last Post: 06-24-2011, 01:14 AM
  4. XOR operation on bytes
    By divyanshu023 in forum New To Java
    Replies: 1
    Last Post: 09-17-2009, 07:11 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
  •