954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Some difficulty with loops

Hello...
I am pretty new to Java and I am experiencing some frustration with loops. I have a few problems here and I hope that you guys could point me in the right direction.

My first problem is with the looping in the program below. No matter what is input as "quitYesNo", the program loops...

/*
	This program asks for number to convert into degrees Celsius or degrees Fahrenheit. It also asks for a 'C' or and 'F' (ignoring the case) to indicate what the user would like
	to convert to. It loops until the user indicates they wish to terminate.
*/
import java.util.*;

public class ConvTempCont

{
	public static void main(String[] args)

	{
		Scanner kb = new Scanner(System.in);

		System.out.println("Give me a temperature to convert and I will convert it to degrees\n"
			+ "Celsius or degrees Fahrenheit.");

		int degIn, degOut;
		String degConv, quitYesNo;
		char degConvChar;

		do
		{
			System.out.print("Enter the temperature:");
			degIn = kb.nextInt();

			System.out.print("Enter 'C' to convert to Celsius and 'F' for Fahrenheit:");
			degConv = kb.next();

			degConvChar = degConv.charAt(0);	

			switch (degConvChar)
			{
				case 'C':
				case 'c':
					degOut = 5*((degIn)-32)/9;
					System.out.println(degOut + " degrees Celsius.");
					break;
				case 'F':
				case 'f':
					degOut = (9*(degIn)/5)+32;
					System.out.println(degOut + " degrees Fahrenheit.");
					break;
			}

			System.out.println("Enter 'Q' to quit or hit any other key to continue.");
			quitYesNo = kb.next();
		}while ((quitYesNo != "Q") || (quitYesNo != "q"));
	}
}


Next, I have this program that is supposed to print a star made of asterisks based on the number input by the user. For example, if the user input 5 then it should print:
*
**
***
****
*****
****
***
**
*

I figured out how to make it print HALF of that and I can't figure out how to print the other half...that is to say, my program would print this out instead:
*
**
***
****
*****

/*
	This program asks the user to enter the size of a triangle to print out,
	then prints the triangle by printing a series of lines consisting of
	asterisks.
*/
import java.util.*;

public class TriangleLoop

{
	public static void main(String[] args)

	{
		Scanner kb = new Scanner(System.in);

		int triangleSize, j, k;

		System.out.println("Enter the size of a triangle");
		System.out.print("(an integer from 1 to 50):");
		triangleSize = kb.nextInt();

		for (j=0; j<triangleSize+1; j++)
		{
			for (k=0; k<j; k++)
			{
				System.out.print('*');
			}
			System.out.println();
		}
	}
}


Thank you all in advance...

ecksdee
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

for 1st:
use quitYesNo.compareToIgnoreCase("q");
see the doc

DangerDev
Posting Pro in Training
485 posts since Jan 2008
Reputation Points: 165
Solved Threads: 59
 

for 2nd:

for(int j=0; j< 2*triangleSize+1; j++)
	{
		int i=j;
		if(j>triangleSize)
			i=2*triangleSize-(j+1);
		for (int k=0; k<i; k++)
		{
			System.out.print("*");
		}
		System.out.println();
	}
DangerDev
Posting Pro in Training
485 posts since Jan 2008
Reputation Points: 165
Solved Threads: 59
 

Or for 2nd one, add the following right after your for() loop:

for (j=0;j <triangleSize;j++ ) {
      for(k=triangleSize-1;k>j;k--){
            System.out.print('*');
      }
      System.out.println();
}
new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 
for 1st: use quitYesNo.compareToIgnoreCase("q"); see the doc


Use that in the while statement?
Doesn't it need to be boolean?

ecksdee
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

Thank you both for the suggestions on the second problem :D

ecksdee
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

how to transeverse a triangle using looping

danzz143
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You