Jump to content

Can someone explain to me why this wont work? [Java]

IvanSnipedYu
Go to solution Solved by rexist,

your condition for do...while loop is wrong

let's say your input's string is = "This App Won't worK"

the length would be 19

if your x start from 0 to <= 19 that would be 20 loops for only 19 characters.

the first 1 was already close thou.

it should be while(x<k)

Trying to print out all the upper case letters in a string.

 

import java.util.Scanner;

public class HW05P04 {

	private static int x;

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter a string: ");
		String a = input.nextLine();
		
		int k = a.length();
		
		System.out.print("Uppercase Letters: ");
		
		int x = 0;
		char position;
		do{
			position = a.charAt(x);
		if(Character.isUpperCase(position)){
			System.out.print(position);
		}
		x++;
		}while(x <= k);
		
	}

}

 

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
Share on other sites

Link to post
Share on other sites

sorry, posted the wrong one. How come this wont work?

import java.util.Scanner;

public class HW05P04 {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		
		System.out.print("Enter a string: ");
		String a = input.nextLine();
		
		int k = a.length();
		
		int x = 0;
		char position;
		do{
			position = a.charAt(x);
		if(Character.isUpperCase(position)){
			System.out.print(position);
		}
		x++;
		}while(x >= k);
			}
		


		}

 

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
Share on other sites

Link to post
Share on other sites

your condition for do...while loop is wrong

let's say your input's string is = "This App Won't worK"

the length would be 19

if your x start from 0 to <= 19 that would be 20 loops for only 19 characters.

the first 1 was already close thou.

it should be while(x<k)

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, rexist said:

your condition for do...while loop is wrong

let's say your input's string is = "This App Won't worK"

the length would be 19

if your x start from 0 to <= 19 that would be 20 loops for only 19 characters.

the first 1 was already close thou.

it should be while(x<k)

OMG how could I be so dumb. That was like right in front of me this whole time!

| i7 4790k | H100i | 16GB (8x2) Corsair Vengence | EVGA GTX 780 SC | ASUS Z97 Sabertooth Mark I | Samsung 840 120GB | WD 2TB Green x2 | Rosewill Hive 750W | 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×