Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have been playing around with the printf method and I am confused with the output from this code:
public static void main(String[] args) {
String[] names = {"Sam, John, George"};
double[] balances = {1000.97,100.00,87.00};
for(int i=0; i<names.length; i++){
System.out.printf("Hello %s this is your balance %.2f\n",names[i],balances[i]);
}
}
The output: Hello Sam, John, George this is your balance 1000.97
I expected three print statements in the console for each person.
Thank for any help.
"Sam, John, George" is one string.
You're missing some quotes. It should be:
String[] names = {"Sam", "John", "George"};
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to key in a value(int) as a parameter and make a loop from zero until it reaches that number.
Scanner s = new Scanner(System.in);
int m=0;
int a;
a=s.nextInt();
while(a<=10) {
System.out.println(m); m++;
}
while loop must be like this:
while(m<=a) {
...
}
In while condition you need to add m<=10 not a.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to find the palindrome of a string.
public static void main(String[] args)
{
String s1 = "eye",s2="";
for(int i = s1.length()-1;i<=0;--i)
{
s2 =s2+String.valueOf(s1.charAt(i));
}
System.out.println(s1);
System.out.println(s2);
}
}
I expected the output
eye
eye
but, s2 isn't printing.
You have the wrong condition in the for loop. The correct condition should be i>=0.
Remember that as long as this condition is true, the for loop will run. Your original condition, i<=0 is false at the very beginning, when i is 2, so the for loop never starts.
A less important problem is that you should not concatenate strings in a for loop, and should use a StringBuilder instead. See this.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I want to build a loop where the console keeps asking me for a string until I write the word "out". This is what I have till now, but it doesn't loop and I can't figure out why. Also I really want to use while or do while if possible.
Scanner input = new Scanner(System.in);
String name = input.nextLine();
while (!name.equals("out")) {
System.out.println(name);
break;
}
You're not updating name inside the loop and remove the break otherwise you're jumping out of the loop in the first iteration (which is not what you want)
while (!name.equals("out")) {
System.out.println(name);
name = input.nextLine(); // I've added it here for you
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
The part of the code that is getting me stuck is the end print out and getting it to add the BILL parts of the array for the final system.out message and display it correctly. The array and the rest of the program work fine it is just that last part that I am having trouble with.
private static void printBilling() {
int numberOfCustomers = Accounts.length;
int sum=0;
System.out.println("\n\nTable showing charges for cell phones");
for (int customer=0; customer<numberOfCustomers; customer++){
System.out.printf ("%s %s %s:\n GB used = %s Please Pay %s \n\n",
Accounts[customer][NAME],
Accounts[customer][ACCT],
Accounts[customer][SELECTION],
Accounts[customer][USED],
Accounts[customer][BILL]);
}
sum+= Accounts.length[customer][BILL];
{
System.out.printf ("The total owed is %s", +sum);
}
your bill probably is a String then change like below.
if it contains integer, use this
sum+= Integer.parseInt(Accounts[customer][BILL]);
if it contains doubles, need (double sum variable) then use
sum+= Double.parseDouble(Accounts[customer][BILL]);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to write a little game in Java and it keeps popping up error:
cannot find symbol
on in.nextInt. I've got other in.nextInts in my code that work fine so I'm a little stumped.
import java.util.Scanner;
public class Player
{
int difficult = 3;
public Player()
{
}
public void SetDifficult()
{
Scanner in = new Scanner(System.in);
System.out.println("choose your difficulty:\n1. simple\n2. easy\n3. normal\n4. hard\n5. impossible");
difficult = in.nextint();
...
}
What is the reason for getting this error ?
you have:
in.nextint()
you want:
in.nextInt()
with a capital I