1. Rewrite the following code segment using while loop int ires = 2; for (int in
ID: 3828795 • Letter: 1
Question
1. Rewrite the following code segment using while loop
int ires = 2;
for (int index = 10, index>=6, --index)
ires += index;
-----------------
2. Rewrite the following code segment using for loop
int ires= 1;
while (ires % 5 !=0){
ires += 3;
--------------
3. Write a complete java program to read in a line of text, printout the individual words, each on a separate line. Then print out the total numbers of characters read(not in including spaces or any white space) and the number of the words read(Note: look at the string and array methods to help you produce the output) in JAVA
Explanation / Answer
Answer for que No 1
int ires = 2;
While (int index = 10 && index>=6)
{ --index;
ires += index;
}
-------------------------------------------------------------------------------------------------
Answer for Que no 2
the loop is stuck in repeat iteration
ires is incrementing in the whlie loop but it's declared as global(out of while loop) So every time it's going to have value 1.
int ires= 1;
for(ires = 1 ,ires % 5 !=0,i++ )
{
ires += 3;
}
------------------------------------------------------------------------------------------------
Ans for question No 3
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class StringLineByLine {
public static void main(String[] args) {
try {
//input file to read the string
File file = new File("extafile.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuffer stringBuffer = new StringBuffer();
String line;
Scanner sc = new Scanner(new File("extra.txt"));
while(sc.hasNext()){
stringBuffer.append(" ");
String s = sc.next();
}
int words = 0;
while(in.hasNext())
{
in.next();
words++;
}
fr.close();
System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.