Reputation: 33
I have to create a program for returning the next non-repeated character..
ex I give ... tweet
and it should return output as w
...
public class str_next {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string");
String s = br.readLine();
revString(s);
}
static char revString(String str) {
int i = 0;
int j;
int n = str.length();
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
char c = str.charAt(i);
char d = str.charAt(j);
if (c != d) {
System.out.print(d);
}
}
}
}
}
I am getting error as .. missing return statement..
Can anyone please tell me.. How do I solve such a problem.. Where am I wrong..?
Upvotes: 0
Views: 10913
Reputation: 2224
your program should be like this:
import java.io.*;
public class str_next {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string");
String s = br.readLine();
revString(s);
}
static char revString(String str) {
int i = 0;
int j;
int n = str.length();
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
char c = str.charAt(i);
char d = str.charAt(j);
if (c != d) {
System.out.print(d);
}
}
}
return 0;
}
}
Upvotes: 0
Reputation: 1
public class JavaPractice {
public static void main(String args[])
{
System.out.println("Enter input String");
Scanner s= new Scanner(System.in);
String input= s.nextLine();
int length=input.length();
for(int i=0;i<length;i++)
{
int temp=0;
for(int j=0;j<length;j++)
{
if(input.charAt(i)==input.charAt(j))
{temp++;}
}
if(temp==1)
{System.out.println(input.charAt(i));}
}
}
}
Upvotes: 0
Reputation: 17
IN JAVA using for loop only..... it is very easy....you can do it without collection in java.. just try it.....
public class FirstNonRepeatedString{
public static void main(String args[]) {
String input = "tweet";
char process[] = input.toCharArray();
boolean status = false;
int index = 0;
for (int i = 0; i < process.length; i++) {
for (int j = 0; j < process.length; j++) {
if (i == j) {
continue;
} else {
if (process[i] == process[j]) {
status = false;
break;
} else {
status = true;
index = i;
}
}
}
if (status) {
System.out.println("First non-repeated string is : " + process[index] + " INDEX " + index);
break;
}
}
}
}
Upvotes: 0
Reputation: 563
Try this, // Split the string into characters // Check if entry exists in the HashMap, if yes- return the character, if No- inert the element with value 1
public static void main(String[] args) {
String s = "rep e atit";
char c = nonRepeat(s);
System.out.println("The first non repeated character is:" + c);
}
private static char nonRepeat(String ss) {
char c;
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
for (int i = 0; i < ss.length(); i++) {
c = ss.charAt(i); // get each chaar from string
if (hm.containsKey(c)) {// char is already in map, increase count
hm.put(c, hm.get(c) + 1);
return c;
} else {
hm.put(c, 1);
}
}
return '0';
}
Upvotes: 0
Reputation: 114
Add each character to a HashSet and check whether hashset.add() returns true, if it returns false ,then remove the character from hashset. Then getting the first value of the hashset will give you the first non repeated character. Algorithm:
for(i=0;i<str.length;i++)
{
HashSet hashSet=new HashSet<>()
if(!hashSet.add(str[i))
hashSet.remove(str[i])
}
hashset.get(0) will give the non repeated character.
Upvotes: 0
Reputation: 4942
You have missing the return statement in your code.
here is the code which returns what you want
CODE
public static Character findFirstNonRepeated(String input) {
// create a new hashtable:
Hashtable<Character, Object> hashChar = new Hashtable<Character, Object>();
int j, strLength;
Character chr;
Object oneTime = new Object();
Object twoTimes = new Object();
strLength = input.length();
for (j = 0; j < strLength; j++) {
chr = new Character(input.charAt(j));
Object o = hashChar.get(chr);
/*
* if there is no entry for that particular character, then insert
* the oneTime flag:
*/
if (o == null) {
hashChar.put(chr, oneTime);
}
/*
*/
else if (o == oneTime) {
hashChar.put(chr, twoTimes);
}
}
/*
* go through hashtable and search for the first nonrepeated char:
*/
int length = strLength;
for (j = 0; j < length; j++) {
chr = new Character(input.charAt(j));
Object c = null;
if (hashChar.get(chr) == oneTime)
return chr;
}
/*
* this only returns null if the loop above doesn't find a nonrepeated
* character in the hashtable
*/
return null;
}
Use like this
char my = findFirstNonRepeated("twwwweety");
System.out.println(my);
This will return y
.
Upvotes: 0
Reputation: 4310
To solve your problem simply add,
return d;
in your function. But it's better to understand how this actually works:
Functions/Methods are written as
accessor_type return_type function_name(parameter_list)
{
//stuff to do in your code
}
For e.g.
public char returnChar(int a)
| | | |
| | | |
^ ^ ^ ^
accessor return name parameter
this means that this function will return a character.
In the sense, you need to a char like this in your function
return char;
Try reading up on methods and their return types. :)
References:
Upvotes: 1
Reputation: 23
You have written the return type for revString(String str) as char and you are not returning any character. Change that return type to void or add line return d; to the method
Upvotes: 0