integer input only do while loop, stuck infinite looping [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've been playing with this code for an hour now trying to make it only accept integers. Atm when you input a character the loop is infinite, i can't seem to progress from here.
do
{
System.out.println("Enter student's number: ");
}
while (!in.hasNextInt());
number = in.nextInt();
Any help would be much appreciated.
Thanks

while (!in.hasNextInt());
Here, in.hasNextInt() gives you a true when in has next element in its stream as an int.
If it is not int, it will return false.
!false = true, which projects :
while(true);
this leads to the infinite loop.
Hope you understood.

Related

how to nest if statements in HashSet? [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to find the lentgh of a set from a preference.
I tired set.length, but it doesnt work. Anyone know another way to find a length of a set?
set = prefs.getStringSet("key", null);
int X = set.length; //it doesn't like length....
I believe instead of set.length, you'll want to call set.size()
Your set is a Set<String>, not a String[]. So you have to call:
int x = set.size();

do/while loop not working in Java with functions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
In this code, the user types in yes if they would like to play again
I have to use functions for this code, I cannot simply state while(again.equals("yes"));.
Part of code:
do{
System.out.print("Play Again? Yes/No: ");
String again=keyboard.nextLine();
boolean running=playAgain(again);
} while(running == true);
My question is why is }while(running==true); a syntax error? I declared the variable above it, shouldn't that allow it to run?
You need a "do" statement first, i.e. do { statements; } while (condition). Also
you don't need to save the return from playAgain() into a variable, you can call it directly from the while().
There are many Java tutorials out there, try the official Oracle ones from starters: The while and do-while Statements
Update
String again; // <== declared here because conditions inside
// while cannot see variables defined inside the do {} block
do {
System.out.print("Play Again? Yes/No: ");
again = keyboard.nextLine();
} while (playAgain(again));

java array printing the repeated numbers , array in the array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Please could you tell me how its work in section (g[ss[i]]++;) and tell me the sequence of output in java
class A{
public static void main(String []a){
int []ss={1,2,3,4,2,3,3,1,1,1,5,6,4,5,4};
int []g=new int[15];
for(int i=0;i<15;i++){
g[ss[i]]++;
}
for(int i=1;i<15;i++){
System.out.println(ss[i-1]+"=="+g[i]);
}
}
}
Can't you run it?
g[ss[i]]++; can be rewritten as
int index = ss[i];
g[index] = g[index] + 1;
So it's counted number of each number in ss.
It's very error prone, and you should never do something like that.
Just run it?
1==4
2==2
3==3
4==3
2==2
3==1
3==0
1==0
1==0
1==0
5==0
6==0
4==0
5==0
This should be your output.

How to check the positive and negative number's in a given arraylist in android or java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have set of int Arraylist for my application.For example
int[] array={10,8,-4,-6,5}
Here i want to check this array list.i am using this array for my bar graph chart.if all the value contains positive value,i want to show the graph in 0 to 100 % Y axis.if array contains negative value,i want to show the negative axis like -50 to 100 % depends upon the value.
How to do the logic for this problem.can anyone help me for my application?
boolean isNegative=false;
for(int i=0;i<array.length;i++)
{
if(array[i]<0)
{
isNegative=true;
break;
}
}
if(isNegative)
{
// code for displaying negative axis
}
else
{
//only display positive axis
}

Whitespace matching regex in Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am currently trying to make use of the java string function someString.replaceAll() to find commonly used words (and, the, by, of, etc) and replace them with " ". Based on the answers to the question at Whitespace Matching Regex - Java, I produced this function call:
data.replaceAll("(?i)\\sthe\\s", " ")
However, it isnt working and I'm really not sure why. Nothing about it looks wrong based on what I've found. Please help me!
Strings are immutable!
data = data.replaceAll("(?i)\\sthe\\s", " ");

Categories

Resources