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 - 214 Views - Last Post: 21 July 2012 - 12:57 PM
#1
Not sure how to make this loop
Posted 20 July 2012 - 05:49 PM
I am trying to write a program that is a lette counter where the user enters a string and a letter and the program tells the user how many times that leter appeared in that string. I keep getting an error at my for loop.
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
Make "i" in the for loop smaller than the length of the string inputted
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.
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
The errors you are receiving is probably because you omitted the boolean in your for loop. Where is your test condition? Also, you may want to use charAt within your loop, as caps is a String, not an array.
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
If i was to keep the caps and not use the ignore case, I still do not get where I am going wrong here. It says I have an error at letter = keyboard.nextLine()and letter and big = letter.toUpperCase(); and if (caps[i] == big). Whats wrong with those?
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
Those give errors because "keyboard" and "letter" are both "Char" Variables. You don't need Char at all. Put a substring in the if statement is enough.
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.
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
char letter = keyboard.nextLine();
Scanner class nextLine() method returns a String not a character
You will have to do
char letter = keyboard.nextLine().charAt(0);
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
my program now finally works but when it runs i enter in my string and letter but i get exception in thread "main" java.lang.String.IndexoutofboundsException
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:
my program now finally works but when it runs i enter in my string and letter but i get exception in thread "main" java.lang.String.IndexoutofboundsException
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|