Do While Loops With Chars?

May 28, 2014

here I have a do-while loop. When I push a button, then press enter, the program should do stuff, then ask me for another key. If I press any key but q, it repeats itself. If I press q then enter, it should end the loop. But then when I press q then enter, it still does the functions in the do-while loop.

Java Code:

Scanner kb = new Scanner(System.in);
System.out.print("Press any key to start.");
String letter = kb.next();
do
{
stuff
System.out.print("Press any key to call another number or press "q" to quit.");
letter = kb.next();
}while(!letter.equals("q") || !letter.equals("Q")); mh_sh_highlight_all('java');

View Replies


ADVERTISEMENT

Unsigned Chars Over UDP Socket

May 22, 2014

I can have up to 100,000 simultaneous connections to a udp socket, which needs to be handled by separate threads. Each of the connections talk to a c program on an embedded device that sends and responds with an array of unsigned chars. Now Java does not have an unsigned char type. The closest you can use is a short. But DatagramPacket requires a byte argument:

public class Socket {
public Socket(int port) throws IOException {
new SocketThread().start();
} }
public class SocketThread extends Thread {
protected DatagramSocket socket = null;

[Code] .....

So how can I go about reading and writing unsigned chars over a udp socket?

View Replies View Related

Finding Indices Of Chars - Comparing Two Strings

Feb 8, 2015

For one of my homework questions, it asks us to: "Write a program that takes 2 strings, one that is the alphabet and the other that is a word, and displays the index value in string1 where the 1st character of the word appears."

So, I made the two strings. I turned the alphabet string into a string array. Now, I am completely lost as to how to call ints index value.

public class Alphabet{
public static void main(String[] args){
String [] Alphabet={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","z","w","x","y","z"};
String word="excalibur";

View Replies View Related

Read Chars In Text Area And Produce Desired Output For Text Editor Project?

Nov 27, 2014

I am working on a personal project and want to create a text editor to write my code. I am wondering how could I read the last input from the user and if say it was an open curly brace {, then like netbean's my editor will supply the closing curly brace. My java experience is limited but I have tried to read key board input and a few other options that did not work.

View Replies View Related

JSP :: Loops In Dropdowns

Dec 5, 2014

I'm working on an old site that we have, I'm adding a couple dropdowns to our forms.With a connection to our database I can pull in values for selBaseCurr, But it's not pulling any values into the second dropdown for selTargetCurr...Am I missing something in the while loop?

<select title="Select currency to convert from" id="selBaseCurr" name="selBaseCurr">
<% while(rs.next()){%>
<option value=""><%= rs.getString("CURR_CODE") %></option>
<%} %>
</select>
<!-- /base -->

[code]....

View Replies View Related

While Loops Within If Else Statements?

Dec 19, 2014

What I'm trying to do below is to say if (adultTickets > 0) I want to bypass the studentOAP tickets question and go straight to the question about dinner. But if (adultTickets ==0) I want to go to the studentOAP question and then if the (studentOAPTickets >0) to go to the question about dinner. But if the (studentOAPTickets ==0) I want to go straight to the question about the contact number.

System.out.print("How many adult tickets do you require? ");
int adultTickets = 0;
boolean validAdultValue = false;
while (validAdultValue == false) {
if(aScanner.hasNextInt())

[Code] ....

View Replies View Related

For Loops With If Statement?

Nov 21, 2014

i'm confused about the if statement part because int i = 0 is set to zero but how can you get the remainder if int i is zero?

for (int i = 0; i < 200; i++) {
if (i % 12 == 0) {
System.out.println("#: " + i);
}

explain to why it output like this?

#: 0
#: 12
#: 24
#: 36
#: 48
#: 60
#: 72
#: 84
#: 96
#: 108
#: 120
#: 132
#: 144
#: 156
#: 168
#: 180
#: 192

View Replies View Related

While Loops And Strings

Jan 3, 2014

So the while loop I am trying to use is:
while( type != "EXIT" ) {
type = input.next();
}

The problem is that typing in EXIT doesn't end the loop like I want it to. I also tried input.nextLine(); but neither of them work. The loop is being used to fill an ArrayList so the number of elements can change based on how many they want. What am I doing wrong, or what alternatives do I have?

Seems I need to use !type.equals("EXIT")

View Replies View Related

Nested For Loops

May 15, 2014

how a nested for loop increments the outer loop and then the inner loop and then when it runs the inside code.I've ran the program a few times but I'm just not understanding it, need little narrative to it?

for(int i=0; i < 5; i++){
System.out.println("i - " + i);
for(int j=0; j < i; j++){
System.out.println("j - " + j);
count++;
}
}

View Replies View Related

Integer Or Int For Loops

Mar 4, 2014

I would like to know which one is more appropriate to use in "for" loops or "while" loops.I don't know exactly when to use Integer or int.

for(int i=0; i<10; i++){ ... }
for(Integer i=0; i<10; i++){...}

View Replies View Related

Not Printing Output When Using While Loops?

May 17, 2015

I am learning while loops but the output I am getting is not what I want? My class:

public class BottlesOnWall {
public static void main (String[] args) {
int x = 99;
while (x > 1) {
System.out.println(x + " bottles on the wall!");

[Code] .....

The output I am getting is this:

99 bottles on the wall!
98 bottles on the wall!
97 bottles on the wall!
96 bottles on the wall!
95 bottles on the wall!

[Code] ....

I want it to stop on "2 bottles on the wall!" and then print "Only 1 bottle left on the wall!". How do I go about doing that? I have tried different variations of the condition (x>1), I have tried (x>5) but it still goes down to 1 bottle?

View Replies View Related

How To Build Triangle Using Loops

Jan 16, 2014

I need to build the triangle like below. How to solve this using loops.

*
* *
* * *
* * * *
* * * * *

View Replies View Related

For Loops Reverse Method

Oct 29, 2014

I'm trying to make a piece of code that writes a for Loop program using OOP. I need to create a class, name that class and have it contain several reverse methods and use a runner class to test it.So far this is the code I've come up with. Does it work or not?

public class loopDemo{
public static void main(string[]args){
String alphabet = "abcdefghijklmnopqrstuvwxyz";
public String reverse(){
char myBoi;
int myIta;
String tebahpla
for(myIta=25j i>=0 ji++){
tebahpla+= alphabet.charAt(myIta);

View Replies View Related

Populate A Database Using Loops

Jan 6, 2014

I am relatively new to Java and I am only beginning to learn about SQL. I have some basic's down but I have been wondering is there a way that I can add data to my database using loops instead of having to physically code every row/column individually ?

View Replies View Related

Loops For Parallel Arrays

Mar 26, 2015

how to loop my arrays, so when I sort them (highest/lowest) the 2nd array corresponds to the sorted array

import java.util.Scanner;
import java.util.Arrays;
public class ArraysArrays {
public static void main (String[] args){

[code]....

View Replies View Related

Using Loops To Do Multiple Divisions

Feb 2, 2015

I am trying to write a program that will take two inputted numbers (a and b) and determine how many divisions can be done by dividing by b until the quotient is less than b. For example if a=64 and b=2, then you would be able to do:

64/2=32
32/2=16
16/2=8
8/2=4
4/2=2
2/2=1

1<2

so there is 6 divisions.

How would I use a loop to create this type of program?

View Replies View Related

Replace Four Loops Through Switch Box?

Sep 11, 2014

I will like replace this code:

// boucle pour les fractions aleatoires
for (i=0; i<2; i++) {

// Creation des fractions aleatoires
Frac first = new Frac(r,5);
Frac second = new Frac(r,5);

[Code] .....

But when I use a switch case I do not have the same result as my four loops.

View Replies View Related

Using Loops For Dice Probability

Oct 5, 2014

The instructions are to "write a program to simulate tossing a pair of 11-sided dice and determine the percentage of times each possible combination of the dice is rolled... then ask the user to enter the number of sides on a die" The code is all provided here, I just have to put it in the right spots. (Side note, I cannot use arrays.) Here's the original file I was given to work with:

Java Code:

import java.util.Random;
import java.util.Scanner;
public class DiceProbability
{
public static void main(String[] args) {
//Declare and initialize variables and objects
//Input: ask user for number of rolls and number of sides on a die
//Print heading for output table

[Code] ....

When I run it, I get something like this (just a part of my output):

Please enter the number of sides on a die: 6
Please enter the number of rolls: 2

Sum of dice Probability

20.0
20.0
30.0
30.0
40.0
40.0
50.0
50.0
650.0
650.0
70.0
70.0

Now I don't really understand the concept in the first place as the lesson was extremely uninformative, so I don't know what went wrong here.

View Replies View Related

Nested Patterns Using For Loops

Feb 9, 2015

i want to use nested loops to print patterns but i cant seem to make a pattern that will look like V and K this should be the outcome.

* *
* *
* *
* *
*

@ @
@ @
@@
@ @
@ @

View Replies View Related

Nested Loops Or Set Operations?

Oct 10, 2013

Is there any advantage to using Java's operations defined in sets or collections over inspecting in nested for-loops?

For example, suppose I want to keep items from one array that do not appear in another, I could do:

for( PrimitiveType prim_1 : someArray ){
for( PrimitiveType prim_2 : otherArray ){
if( prim_1 != prim_2){
///put in some return list
}
}
}

Or else I could do:

ArrayList<NotPrimitive> excludedElements =
new ArrayList(Arrays.asList(primitiveArrayToExclude));
for( PrimitiveType elem : someArray){
if( ! excludedElements.contains( elem ) {
//put in some return list
}
}

I personally see no improvement in legibility nor any reduction of error potential.....

View Replies View Related

While Loops And Conditional Breaks?

Feb 12, 2015

public class dowhile2 {
public static void main(String[] args) {
int i = 1;
while(i <= 100){
System.out.println(i);
if(i == 50){
break;
}
i++;
}
}
}

I just want to know how to count this lines of codes. Because if I place for example i++ further up the code or the system.out.println a bit lower the result will be 1-49 or 2-50 and not the goal that is 1-50.

I want to know how to think and count. Why do i get 2345678.........50 and not 12345678........50 or 12345678.....49, thats the question.

View Replies View Related

Nesting Loops - Odd / Even And Zero Digits In Integer Value

Oct 13, 2014

So I am trying to do this program but I can't figure out how to nest it properly. The prompt is :

Design and Implement an application that determines and prints the number of odd,even,and zero digits in an integer value read from the keyboard.

The catch is, strings are not allowed. This is what I have so far :

import java.util.Scanner;
 public class OddEvenZero {
 public static void main(String[] args) {
int zero = 0, even = 0, odd = 0, num, num2;

[Code] ....

View Replies View Related

Using Loops To Create A Specific Design?

Sep 10, 2013

I am completing the exercises in Java How To Program 8th Edition by Harvey and Paul Deitel.There are two optional exercises GUI and Graphics Case Study Exercises on page 141 Chapter 4.

I have to use loops and control statements to draw lines thereby creating two designs.I am trying to create the design on Fig. 4.20. The lines do not line up correctly.The lines should fan out from all corners. Individually, they look exactly like it should.

Example of one corner.

int width = getWidth();
int height = getHeight();
// upper-left corner
int startX = 0;
int startY = 0;
int endX = 0;

[Code] ....

For the other corners I have different starting and ending positions.To draw upper-left corner I have a starting position of 0,0 and ending position of 0,height.I then move ending position up one vertical step and right one horizontal step. I repeat this fifteen times.

To draw upper-right corner I have a starting position of width,0 and ending position of width,height. I then move ending position up one vertical step and left one horizontal step.

To draw bottom-left corner I have a starting position of 0,height and ending position of 0,0. I then move ending position down one vertical step and right one horizontal step.

To draw bottom-right corner I have a starting position of width, height and ending position of width,0. I then move ending position down one vertical step and left one horizontal step.

For the second design Fig. 4.21 all my lines line up correctly no matter the direction I resize it. Both designs frame size is 250,250. Both designs are divided into fifteen steps.

View Replies View Related

Displaying Pattern - Nested For Loops

Jan 24, 2015

Write a program that will display the following pattern.

$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$

I need to write a program that uses nested for loops to display that patter but I have no clue on how to do it....

View Replies View Related

GPA Calculator In Java Using Loops And Switch

Apr 25, 2015

I am trying to make GPA calculator. I decided to use only switch statement and make it as simple as possible. The point is to enter as many classes and students as user wants. I keep getting credit points 0 at the end and also text format is not good too. Here is the code.

import java.util.Scanner;
import java.io.*;
public class SwitchExercise{
public static void main(String[] args) {
String grade = "";
String grades="";
String coursename="";
String headingleft="COURSES";

[code]....

View Replies View Related

Compare Two Different Arrays From Separate Loops

Oct 21, 2014

I am working on an assignment that is to simulate the relationship between a cache and main memory. Basically it is supposed to be a 16 slot cache and we are to have 500 for main memory. I need it to compare the corresponding index for both arrays, then go through then repeat the cache array and compare it to the next 16 of main memory. It is to simulate an direct mapped cache. How to compare two arrays from separate loops.

public class MainMemory {
private short mainMem[] = new short[200];
public void assign(){
short i;
for ( i = 0; i < mainMem.length; i ++){
mainMem[i] = i;

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved