File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to change a character into a string in java

 
deepika deepi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i have a code that displays the result in character. i want that to be replaced by a string name and get the full sentence in the result.

replacedname = ch[i];



Input is :
India {Listeni/ˈɪndiə/}, officially the Republic of India /Bhārat /Gaṇarājya,{c} is a country in South Asia.

output is :


Tagged Sentence:India {Listeni/ˈɪndiə/}, officially the Republic of India /Bhārat /Gaṇarājya,{c} is a country in South Asia.
Actual Sentence:India , officially the Republic of India is a country in South Asia.

expected output is :


Tagged Sentence:India {Listeni/ˈɪndiə/}, officially the Republic of India /Bhārat /Gaṇarājya,{c} is a country in South Asia.
Actual Sentence:India , officially the Republic of India is a country in South Asia.
Expected output:India , officially the Republic of India is a country in South Asia.

How and where to change the code to get the result. If you can help me
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what the exact problem is. Can you explain, as clearly and in detail as possible, how the current actual output is different from what you expect?
 
Winston Gutkowski
Bartender
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:How and where to change the code to get the result. If you can help me

Well, I'd question the need for a DataInputStream for a start. If you look at the documentation for BufferedReader, its specific example is:
BufferedReader in = new BufferedReader(new FileReader("foo.in"));

Is there any reason why you can't just use it?

Winston
 
deepika deepi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
deepika deepi wrote:How and where to change the code to get the result. If you can help me

Well, I'd question the need for a DataInputStream for a start. If you look at the documentation for BufferedReader, its specific example is:
BufferedReader in = new BufferedReader(new FileReader("foo.in"));

Is there any reason why you can't just use it?

Winston


ya.. At times when i use that it is show some error. so i am using DataInputStream

Is it wrong to use ? can you tell me some example why to use BufferedReader and not DataInputStream?
 
deepika deepi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:I don't understand what the exact problem is. Can you explain, as clearly and in detail as possible, how the current actual output is different from what you expect?


I have changed the string to character in the beginning. So i am not able to get the value in some string again. so that i can use that data for future purpose. if that is satisfied. i would get some what easier.

eg: the output is in form of System.out.println(ch[i[);

this i am not able to use again for some data as input. I want this output to be used as input some where. so i would like to know. how to make it possible?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just need 4 lines of code in regex to get that output.. You would be better off with it..
 
Winston Gutkowski
Bartender
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:ya.. At times when i use that it is show some error. so i am using DataInputStream

I think we've been though this before deepika. That tells us absolutely nothing. You MUST be precise.

WHAT error are you getting? And please show the exact message.

And whatever you do, don't change the way you do something just because you get an error. Find out WHY you're getting it.

Winston
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:
Is it wrong to use ? can you tell me some example why to use BufferedReader and not DataInputStream?


Check the documentation for DataInputStream.
You never use it to read simple text data but only for binary data.

Oracle documentation is the best place to find out what a particular Class does.
So whenever such is the case, rush to the java documentation and you will get the answer..

 
Campbell Ritchie
Sheriff
Pie
Posts: 47310
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
…But the Java Tutorials are easier to read.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:…But the Java Tutorials are easier to read.


Yes, of course. It explains stuffs in a better way..
 
deepika deepi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:You just need 4 lines of code in regex to get that output.. You would be better off with it..


I have tried with sample code in net for trial. with indexOf

Error is
found : int
required: java.lang.String
String result = s.indexOf("break");
^
1 error

and the code is



I want the sentence output leaving the word "break".

I tried with .contains also it giving same error

Error:
ContainsString.java:18: incompatible types
found : boolean
required: java.lang.String
String result = str.contains(Chara);
^
1 error
code is

In this i want to leave the word namah, and in last i have om/shivan i want to remove the word after "/" and get the result like om namah shivaya om is the god
how to change this to string and get the result?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:
R. Jain wrote:You just need 4 lines of code in regex to get that output.. You would be better off with it..


I have tried with sample code in net for trial. with indexOf

Error is
found : int
required: java.lang.String
String result = s.indexOf("break");
^
1 error


See the Documentation for returntype of indexOf()

deepika deepi wrote:
and the code is


I want the sentence output leaving the word "break".


You will need some other method if you really want to use String class..
There is a method called replaceAll that will serve your task..


deepika deepi wrote:
In this i want to leave the word namah, and in last i have om/shivan i want to remove the word after "/" and get the result like om namah shivaya om is the god
how to change this to string and get the result?


Same. Use Regex..

You will find a lot of tutorials on internet regarding. Some of which I gave you..
You can try those tutorials and apply some kind of regex to get your work done.

Essentially, whenever you want to do these kinds of stuffs, like parsing a string, searching for a subsequence, or replacing a set of strings... You better use regex.
Your task will be much simpler, than what you have to do with standard String class..
 
changu mani
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this Deepika
String result = s.replaceAll("break", "");

The return type of indexOf is int and not string. Please check.
 
deepika deepi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changu mani wrote:Use this Deepika
String result = s.replaceAll("break", "");

The return type of indexOf is int and not string. Please check.


ya. the return tyre is for int ans not string. I want to declare it for string and get the result. How to change the int to string and get the result ?
 
Winston Gutkowski
Bartender
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:I want the sentence output leaving the word "break".
...
In this i want to leave the word namah, and in last i have om/shivan i want to remove the word after "/" and get the result...

Those two requirements are completely different.

For the first one you don't need a regex at all; for the second one you probably do - or at least it will be a lot easier with one.

What exactly do you want to do? Right now, you just seem to be flailing around.

You must be precise about your requirements. If you aren't, you will never learn to program properly.

Winston
 
deepika deepi
Ranch Hand
Posts: 199
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:

What exactly do you want to do? Right now, you just seem to be flailing around.

You must be precise about your requirements. If you aren't, you will never learn to program properly.

Winston


ya. i can understand that i can use replaceAll to remove the sentence. But i want to try and get the result using indexOf so that i can be familiar with this. so i am precise about that. i would like to know in different manner i get heh result. Sorry for that
 
Winston Gutkowski
Bartender
Pie
Posts: 9501
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deepika deepi wrote:But i want to try and get the result using indexOf so that i can be familiar with this.

Right, so first you need to read the documentation.

Question 1: What does indexOf() return?

Question 2: How do you think that might help you to remove a word - let's say "fox" - from a String containing a sentence, for example:
the quick brown fox jumps over the lazy dog
?

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic