Results 1 to 12 of 12
  1. #1
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default [SOLVED]Strings manipulation problem

    Im having trouble writing a code related to strings. I am new to Java and programming it self. Any help would be greatly appreciated.

    Im trying to figure out how i can write the code that ask the user for input for example: CANADA, OTTAWA, 200

    the output is: Country: Canada
    Capital City: Ottawa
    Population: 200 million

    thanks in advance :)
    Last edited by dienesh77; 02-17-2011 at 05:50 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Look up scanner api, and you want to create one and use it for the whole problem. Thats really all you need to know besides basics on storing variables and printing.

  3. #3
    Ruuhkis is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    import java.util.Scanner;


    public class splitString {
    public static void main(String args[]){
    Scanner scanner = new Scanner(System.in);
    System.out.println("Input country, its capital city and amount of population\nin format country, city, amount: ");
    String temp[] = scanner.nextLine().split(",");
    String country = temp[0];
    String city = temp[1];
    int population = Integer.parseInt(temp[2].trim());
    System.out.println("Country: "+country+"\nCapital city: "+city+"\nPopulation: "+population);
    }
    }
    this is one example how it can be done.

  4. #4
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    Is it possible to get this done using indexOf and substring methods?

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    ruuhkis, that's not an example of how it's done, that is called spoonfeeding him the answer. Please help him out without giving him the code.

    Why would you want to use indexOf? You can do it with substring, however its more complicated then it needs to be. You could get 1 string is the input and loop through the string , concatenating the substrings to a new string up until "," are found.

  6. #6
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    import javax.swing.JOptionPane;
    public class countrycappop {


    public static void main(String[] args) {

    String information = JOptionPane.showInputDialog("Enter Country name, capital, and population: ");
    int comma = information.indexOf(",");
    int space = information.indexOf(" ");
    String country = information.substring(0, comma);
    String capital = information.substring(space, comma);
    System.out.println("Country: "+country+"\nCapital city: "+capital+"");

    this is what i got so far, I cant get the capital city from the string :/

  7. #7
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    import javax.swing.JOptionPane;
    import java.lang.String;

    public class countrycappop {


    public static void main(String[] args) {

    String information = JOptionPane.showInputDialog("Enter Country name, capital, and population: ");
    int comma = information.indexOf(",");
    int comma2= information.indexOf(",", comma + 1);


    String country = information.substring(0, comma);
    String city = information.substring(comma+1, comma2);
    String population = information.substring(comma2+1);



    System.out.println("Country: "+country+"\nCapital city: "+city+"\nPopulation: "+population+" Million");

    Okay i figured out how i can extract the country name, capital and population.

    What i need help is: If a user inputs CANADA, how can i change that to Canada(the first letter stays capital all the others stay lower case). much help appreciated thanks.

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by dienesh77 View Post
    If a user inputs CANADA, how can i change that to Canada(the first letter stays capital all the others stay lower case).
    Use the substring method of Strings and get the letters from 1st index till the end of String. Convert it to lowercase and concatenate it back to the charAt 0 index.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  9. #9
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default

    ops my bad i figured this one out already. :)

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by dienesh77 View Post
    ops my bad i figured this one out already. :)
    Good to know that. Mark the thread as SOLVED then.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  11. #11
    dienesh77 is offline Member
    Join Date
    Feb 2011
    Posts
    32
    Rep Power
    0

    Default [Solved] String manipulation problem

    solved thank you

  12. #12
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Not like that.

    Go to Thread Tools -> Mark this thread as solved.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Converting ints to Strings Problem
    By gkoef in forum New To Java
    Replies: 7
    Last Post: 12-06-2010, 01:07 PM
  2. Strings Problem
    By Freakzoyd in forum New To Java
    Replies: 11
    Last Post: 11-30-2010, 02:45 AM
  3. [SOLVED] problem with strings
    By sandeepsai39 in forum New To Java
    Replies: 11
    Last Post: 02-23-2009, 04:59 PM
  4. Problem Comparing Strings (its not what you think)
    By hilather in forum New To Java
    Replies: 7
    Last Post: 11-19-2008, 06:43 PM
  5. Image manipulation.
    By ambikark in forum Advanced Java
    Replies: 0
    Last Post: 10-15-2008, 01:37 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
  •