Here is my code:
public class Exercixe09_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string: ");
String s1 = input.next();
System.out.println("Enter a character: ");
String s2 = input.next();
char c = s2.charAt(0);
int num = count(s1, c);
System.out.print(num);
}
public static int count(String str, char a)
{
int count = 0;
for(int i =0; i < str.length(); i++)
if(str.charAt(i) == 'a'){
count++;
}
return count;
}
}
When I compile and run it, nothing prints. Can someone please help me figure out what is wrong?
import java.util.Scanner;
public class Exercixe09_04 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String s1 = input.next();
System.out.print("Enter a character: ");
String s2 = input.next();
char c = s2.charAt(0);
int num = count(s1, c);
System.out.println(num);
}
public static int count(String str, char a) {
int count = 0;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == a) {
count++;
}
return count;
}
}
Sample run:
falsetru#jmlee12:/tmp$ java Exercixe09_04
Enter a string: Hello
Enter a character: l
2
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
This a program to enter a sentence and print the longest word using substring()
Here, I have used the 1st loop to extract each word from the sentence and find the length of the longest word.
In the 2nd Loop, its purpose is it to extract and print the word that matches the length which was found out in the 1st loop and stored in the "longestLength" variable.
I am getting an error when i compile the following code:
import java.util.*;
public class Program {
public static void main(String[] args) {
String s, st;
int longestLength = 0;
int i1 = 0;
int i2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Sentence");
s = sc.nextLine();
s = s.trim();
s = s+" ";
for (int i = 0; i < s.length(); i++) {
if (Character.isWhitespace(s.charAt(i))) {
i2 = i;
st = s.substring(i1, i2);
if (st.length() > longestLength)
longestLength = st.length();
i1 = i;
}
}
for (int i = 0; i < s.length(); i++) {
if (Character.isWhitespace(s.charAt(i))) {
i2 = i;
st = s.substring(i1, i2);
if (st.length() == longestLength) {
System.out.println("Longest Word : " + st);
break;
}
i1=i;
}
}
}
}
Here you don't need to use for loops for just finding the longest word.
Just remove the for loops and add the following lines below that.
Scanner sc = new Scanner(System.in);
System.out.println("Enter Sentence");
s = sc.nextLine();
s = s.trim();
s = s+" ";
String longest = Arrays.stream(s.split(" ")).max(Comparator.comparingInt(String::length)).orElse(null);
System.out.println(longest);
YOUR FINAL CODE WILL BE:
import java.util.*;
public class Program {
public static void main(String[] args) {
String s;
int longestLength = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Sentence");
s = sc.nextLine();
s = s.trim();
String longest = Arrays.stream(s.split(" ")).max(Comparator.comparingInt(String::length)).orElse(null);
System.out.println(longest);
}
}
It will print the longest word. Hope it will be helpful to you.
There is an alternate easy method to split sentence using delimter using String.spilt(delimiter) funciton.
Below code is an working example for your task
public class Test {
public static void main(String[] args) {
String s, st;
int longestLength = 0;
int i1 = 0;
int i2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Sentence");
s = sc.nextLine();
String maxString = "";
for(String string : s.split(" ")){
maxString = maxString.length() > string.length() ? maxString : string;
}
System.out.println("The max lengthed string is : "+maxString);
}
}
Your issue is due to i1 has not be resetted to 0 hence the old loop value if i1 is there hence the issue
The solution is given below:
public static void main(String[] args) {
String s, st;
int longestLength = 0;
int i1 = 0;
int i2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Sentence");
s = sc.nextLine();
s = s.trim();
s = s+" ";
for (int i = 0; i < s.length(); i++) {
if (Character.isWhitespace(s.charAt(i))) {
i2 = i;
st = s.substring(i1, i2);
if (st.length() > longestLength)
longestLength = st.length();
i1 = i;
}
}
i1=0;
for (int i = 0; i < s.length(); i++) {
if (Character.isWhitespace(s.charAt(i))) {
i2 = i;
st = s.substring(i1, i2);
if (st.length() == longestLength) {
System.out.println("Longest Word : " + st);
break;
}
i1=i;
}
}
}
The character must be entered from the console to change to lowercase letters on this line. But it displays the same word and the symbol does not change.
public class Task {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(requestString());
char symbol = requestSymbol().charAt(0);
int count = 0;
for (int i = 0; i < sb.length(); i++) {
if (sb.charAt(i) == symbol) {
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
count++;
}
}
System.out.println("Number of entries: " + count);
System.out.println("Converted string: " + sb);
}
static String requestString() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter string:");
return scanner.nextLine();
}
static String requestSymbol() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the symbol:");
return scanner.next();
}
}
It seems to be a problem with the line:
sb.setCharAt(i, sb.charAt(Character.toUpperCase(i)));
It should be:
sb.setCharAt(i, Character.toUpperCase(symbol));
I have to get 4 user input from the user one by one on the next line like
Sample input:
65
66
67
68
Then the output has to displayed like
You have entered:
65-A
66-B
67-C
68-D
the program i have return is this:
import java.util.Scanner;
public class ASCII {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the digits:");
int no = sc.nextInt();
char ch= (char) no;
System.out.println(no + "-" + ch);
}
}
the one thing could not get is the 4 input for the user could someone help with that
You should loop it;
int[] numbers = new int[4];
for (int i = 0; i < 4; i++) {
numbers[i] = sc.nextInt();
}
numbers[n-1] will return number in your case 0 < n < 5;
and you can create another loop to print them.
chars[] characters = {'A','B','C','D'};
for (int i = 0; i < 4; i++) {
System.out.println(Integer.toString(numbers[i]) + characters[i]);
}
for loops works like;
for (DoAtStart; Condition; DoAtEndOfARepeat) {
}
This would work for you :
public class ASCII {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the digits:");
int array[]=new int[4];
for(int i=0; i<4;i++) {
int no = sc.nextInt();
array[i]=no;
}
System.out.println("You have entered:");
for(int j=0;j<array.length;j++) {
char ch= (char) array[j];
System.out.println(ch+"-"+array[j]);
}
}
}
I want to insert a void function in my code.
import java.util.*;
public class javellana {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Input String: ");
String str = scan.nextLine();
System.out.println("Input character: ");
char str1 = scan.next().charAt(0);
int num = str.length();
int i;
for (i = num - 1; i >= 0; i--) {
if (str1 == str.charAt(i)) {
System.out.println("The character " + str1 + " you intput is " + i);
break;
}
}
}
}
That is my code and I want to add a void function starting in "for". I want the for loop to be in a void function but I can't seem to do it. How do I fix this?
public class javellana {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Input String: ");
String str = scan.nextLine();
System.out.println("Input character: ");
char str1 = scan.next().charAt(0);
int num = str.length();
tt(num,str1,str);
}
static void tt(int num , char str1, String str)
{
for(int i = num - 1; i >= 0; i--) {
if (str1 == str.charAt(i)) {
System.out.println("The character " + str1 + " you intput is " + i);
break;
}
}
}
}
Apart from the code, what you wanna achieve from this is still
unclear.
You can add a void method as static and use it directly in main method.
public class javellana {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Input String: ");
String str = scan.nextLine();
System.out.println("Input character: ");
char str1 = scan.next().charAt(0);
int num = str.length();
int i;
func(str, str1);
}
static void func(String str, char str1) {
int num = str.length();
for (int i = num - 1; i >= 0; i--) {
if (str1 == str.charAt(i)) {
System.out.println("The character " + str1 + " you intput is " + i);
break;
}
}
}
}
EDIT:
You can simply use System.out.println(str.lastIndexOf(str1)); line, instead of the entire for loop for your problem
public class javellana {
String str;
char str1;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Input String: ");
str = scan.nextLine();
System.out.println("Input character: ");
str1 = scan.next().charAt(0);
func(); // call function here
}
static void func() {
int num = str.length();
for (int i = num - 1; i >= 0; i--) {
if (str1 == str.charAt(i)) {
System.out.println("The character " + str1 + " you intput is " + i);
break;
}
}
}
}
Try this for change
If
adding a void function
means adding(declaring) another method(function) in public static void main(String){};
then this is not allowed.
What you can do is; create an anonymous class or functional interface (Java 8). Which is not exactly what you want but somehow..
main() is static method, so only static method can be used from it.
Instead of using for loop, you can use String.lastIndexOf() method.
public static void main(String... args) {
try (Scanner scan = new Scanner(System.in)) {
System.out.print("Input String: ");
String str = scan.nextLine();
System.out.print("Input character: ");
char ch = scan.next().charAt(0);
lastIndexOf(str, ch);
}
}
private static void lastIndexOf(String str, char ch) {
int i = str.lastIndexOf(ch);
if (i >= 0)
System.out.println("The character " + ch + " you input is " + i);
}
You can't create a method within a method.
Currently, you're in main method.
But you can call as many methods you want from a method.
But there's some restriction. you can't call the nonstatic method within a static method.
Therefore the #pkgajulapalli answer is probably correct.
Ok, I'm trying to make a simple sink a boat game in java, but am running into a java.lang.numberformatexception and don't know what I'm doing wrong... Here's the actual code I'm using. Sorry very noob still.
It's a simple 7 tiles battlefield with a ship on 3 tiles.
import java.util.Scanner;
public class New4
{
int randnum;
static char[] field;
int numofhits;
String userinput;
private static Scanner userguess;
public static void main(String[] args)
{
new New4();
}
public New4()
{
String input = "";
int numofhits = 0;
int randnum = (int) (Math.random() * 5);
char[] field = new char[7];
for(int i=0;i<7;i++)
{
field[i] = '*';
}
for(int j=0;j<3;j++)
{
field[randnum] = 'O';
randnum++;
}
for(char c : field)
{
System.out.print("|" + c);
}
System.out.print("|");
while(numofhits < 3)
{
AskUser();
input = userinput;
if(field[Integer.parseInt(input)] == 'O'){System.out.println("A terrifc hit");numofhits++;}
else{System.out.println("A blasted miss");}
System.out.println("Great game");}
}
public String AskUser()
{
String userinput;
System.out.println("Guess a number: ");
userguess = new Scanner(System.in);
userinput = userguess.next();
return userinput;
}
}
input = askUser(); //instead of userinput = input;