import java.util.Scanner;
public class Counter
{
public static void main(String[] args)
{
String str;
String caps;
char letter;
char big;
int sum = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter in the string you want.");
str = keyboard.nextLine();
caps = str.toUpperCase();
System.out.println("Enter in a character");
letter = keyboard.nextLine();
big = letter.toUpperCase();
for (int i = 0; i++)
{
if (caps[i] == big)
{
sum += 1;
}
}
System.out.println("The number of times you entered " + big + " is "
+ sum + ".");
}
}
Not sure how to make this loop
Page 1 of 18 Replies - 219 Views - Last Post: 21 July 2012 - 12:57 PM
#1
Not sure how to make this loop
Posted 20 July 2012 - 05:49 PM
Replies To: Not sure how to make this loop
#2
Re: Not sure how to make this loop
Posted 20 July 2012 - 05:54 PM
and caps isn't an array so don't need the cap[i], just run the characters within that string using subString one by one and use the if statement you got there
Also you don't need to make them uppercase if you do ".equalsIgnoreCase(big)", something like that.
This post has been edited by SilverEleak: 20 July 2012 - 05:56 PM
#3
Re: Not sure how to make this loop
Posted 20 July 2012 - 06:43 PM
This post has been edited by Mobuis1995: 20 July 2012 - 06:49 PM
#4
Re: Not sure how to make this loop
Posted 20 July 2012 - 07:18 PM
import java.util.Scanner;
public class Counter
{
public static void main(String[] args)
{
String str;
String caps;
char letter;
char big;
int sum = 0;
int length;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter in the string you want.");
str = keyboard.nextLine();
caps = str.toUpperCase();
length = caps.length();
System.out.println("Enter in a character");
letter = keyboard.nextLine();
big = letter.toUpperCase();
for (int i = 0; i <= length; i++)
{
if (caps[i] == big)
{
sum += 1;
}
}
System.out.println("The number of times you entered " + big + " is "
+ sum + ".");
}
}
#5
Re: Not sure how to make this loop
Posted 20 July 2012 - 07:41 PM
And saying it again, "Caps" isn't an array, so you can't do "Caps[i]". just "Caps"
But like really, a lotta unnecessary things and are in there that i suggest you to take off.
This post has been edited by SilverEleak: 20 July 2012 - 07:55 PM
#6
Re: Not sure how to make this loop
Posted 20 July 2012 - 08:12 PM
Scanner class nextLine() method returns a String not a character
You will have to do
char letter = keyboard.nextLine().charAt(0);
#7
Re: Not sure how to make this loop
Posted 20 July 2012 - 08:58 PM
import java.util.Scanner;
public class Counter
{
public static void main(String[] args)
{
String str;
String caps;
String letter;
String big;
int sum = 0;
int length;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter in the string you want.");
str = keyboard.nextLine();
caps = str.toUpperCase();
length = caps.length();
System.out.println("Enter in a character");
letter = keyboard.nextLine();
big = letter.toUpperCase();
for (int i = 0; i <= length; i++)
{
if (caps.charAt(i) == big.charAt(0))
{
sum += 1;
}
}
System.out.println("The number of times you entered " + big + " is "
+ sum + ".");
}
}
#8
Re: Not sure how to make this loop
Posted 20 July 2012 - 09:19 PM
skyline2162, on 20 July 2012 - 08:58 PM, said:
import java.util.Scanner;
public class Counter
{
public static void main(String[] args)
{
String str;
String caps;
String letter;
String big;
int sum = 0;
int length;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter in the string you want.");
str = keyboard.nextLine();
caps = str.toUpperCase();
length = caps.length();
System.out.println("Enter in a character");
letter = keyboard.nextLine();
big = letter.toUpperCase();
for (int i = 0; i <= length; i++)
{
if (caps.charAt(i) == big.charAt(0))
{
sum += 1;
}
}
System.out.println("The number of times you entered " + big + " is "
+ sum + ".");
}
}
for (i=0; i<=length; i++) {
// should be
for (i=0; i<length; i++) {
This post has been edited by SilverEleak: 20 July 2012 - 09:19 PM
|
|

New Topic/Question
Reply



MultiQuote



|