8 Replies - 459 Views - Last Post: 21 July 2012 - 12:57 PM Rate Topic: -----

#1 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

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.

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 + ".");
	}
}

		
		
		
		
		




Is This A Good Question/Topic? 0
  • +

Replies To: Not sure how to make this loop

#2 SilverEleak  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 18-July 12

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.

This post has been edited by SilverEleak: 20 July 2012 - 05:56 PM

Was This Post Helpful? 1
  • +
  • -

#3 Mobuis1995  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 42
  • Joined: 15-February 12

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

Was This Post Helpful? 0
  • +
  • -

#4 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

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 + ".");
	}
}

		
		
		
		
		


Was This Post Helpful? 0
  • +
  • -

#5 SilverEleak  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 18-July 12

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.

This post has been edited by SilverEleak: 20 July 2012 - 07:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

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);
Was This Post Helpful? 0
  • +
  • -

#7 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

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 + ".");
	}
}

		
		
		
		
		



Was This Post Helpful? 0
  • +
  • -

#8 SilverEleak  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 18-July 12

Re: Not sure how to make this loop

Posted 20 July 2012 - 09:19 PM

View Postskyline2162, 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

Was This Post Helpful? 0
  • +
  • -

#9 skyline2162  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 112
  • Joined: 17-December 11

Re: Not sure how to make this loop

Posted 21 July 2012 - 12:57 PM

thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1