I can't figure out how to print this string only five times. I tried to use the * operator, but that doesn't work with strings apparently, unles i'm not importing correctly.
import java.lang.String;
public class Looparray
{
public static void main(String args[] {
for (String myStr= "Hello there!";;) {
System.out.print (myStr);
System.out.print("
");
}
}
}
I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...
This is the question:
Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:
printStrings("abc", 5);
will print the following output: abcabcabcabcabc
This is what I attempted:
public class printStringsproject { public static void printStrings(int abc, int number) { for (int i = 1; i <= number; i++) { System.out.print("abc"); } } public static void main(String[] args) { printStrings(1, 5); } }
I'm trying to write a method that will print a string of a random number of X's (from 5 to 20) on a line, then another number of X's on the next line, etc. until the random number = 16.
I have to use while loops and probably fencepost algorithms. I'm confused with how to print the number of x's the random number of times over and over.
Right now, I'm pretty sure my for loop is wrong or unnecessary.
So far, this is my code:
public static void randomX() { int x = 0; while (x != 16) { x = (int)(Math.random()*20) + 5; for (int i = 0; i <= x; i++); { System.out.print("x"); } } }
I read on how to use the continue statement, but I'm failing in how to use it properly, mostly because it's not working. I'm supposed to print out what numbers are showing up and how many times for each. Plus, I have to print out 'times' instead of 'time' if there's more than one of a certain number. Right now, it's printing out all the numbers including the ones that don't get inputted.
import java.util.Scanner; public class occurrence { public static void main(String[] args) {
//scanner/values Scanner input = new Scanner(System.in); int number = 101;
I tried to make a program that takes a string str, and char a and checks how many times the char is used in the string.
Example: the string Welcome and the letter e, is 2 times. so the program should print 2.
It compiles but when I run it and enter the information, i cannot get the printing line out.
Heres my code:
import java.util.Scanner; class program { public static void main(String[] args) { Scanner user_input=new Scanner(System.in); String str; String b; System.out.print("Please enter a word");
I tried to make a program that takes a string str, and char a and checks how many times the char is used in the string. Example: the string Welcome and the letter e, is 2 times. so the program should print 2. It compiles but when I run it and enter the information, i cannot get the printing line out.
Heres my code:
import java.util.Scanner; class program { public static void main(String[] args) { Scanner user_input=new Scanner(System.in); String str; String b; System.out.print("Please enter a word"); str=user_input.next();
Error:public String front3(String str) { ^^^^^^^^^^^^^^^^^^ This method must return a result of type String
Possible problem: the if-statement structure may theoretically allow a run to reach the end of the method without calling return. Consider adding a last line in the method return some_value; so a value is always returned.
Write a program using a while-loop (and a for-loop) that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. (So, you will have two separate program codes, one using a while-loop and the other one using a for-loop.)
Example: Enter a string: "Hello, JAVA is my favorite programming language." Enter a character: e The number of times the specified character appears in the string: 3
I don't even know where to begin I've only got this
import java.util.Scanner; public class letterCounter { public static void main(String [] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string"); String myString = sc.nextLine(); System.out.println("Enter a letter"); String letter = sc.nextLine(); } }
Basically the requirements are to take a sentence (string of text) and check to see how many times specific words come up and then add to the counter depending on the word.
But I can not seem to get it to add the instances of the goodwords and badwords.
package Strings; import java.io.*; public class SentimentAnalyser { private static String analyse(String text) { int pw = 0; int nw = 0; String[] searchword = { "bad", "terrible", "good", "awesome" };
I'm having trouble with the last few lines of the code. It's supposed to take a replacement string entered by the user and print out the new string. For some reason it's now allowing me to enter a replacement string
import java.util.Scanner; public class Project02 { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print("Enter a long string: "); String lString = keyboard.nextLine();
[Code] ....
Output:
Enter a long string: the quick brown fox jumped over the lazy dog Enter a substring: jumped Length of your string: 44 Length of your substring: 6 Starting position of your substring in string: 20 String before your substring: the quick brown fox String after your substring: over the lazy dog Enter a position between 0 and 43: 18 The character at position 18 is x
Enter a replacement string: Your new string is: the quick brown fox over the lazy dog <------ isn't taking user input
So if I wanted to tell the user that the fifth letter they entered is "____" how would I do that.
I am prompting the user to enter a word and then displaying the length of the String. Now I want to display to the user what the fifth letter of the String they entered is.
import java.util.Scanner; public class StringPractice { public static void main(String [] args) { Scanner input = new Scanner(System.in); String word; int lenght;
I'm having a small error with my code, it is getting rid of the first letter of the string after the first line printed.
import java.util.*; public class PrintPyramidName { public static void main(String[] args) { System.out.println("What is your name?"); Scanner input = new Scanner(System.in);
Here i want to print the String from Starting character which is given by user..Here i given below the image like my concept..here we put .(dot and enter R)eclipse will print the method name which is starting from R..
So I need to write a program that reads through a String and counts how many vowels there are and prints them out as it finds them. This is what I have so far:
I am writing to a text file via user input and it is saving all the user input to the file but it is just printing one word per line. I would like it to print the string on one line and print the next string on the next line upon them hitting enter.
public void textFile() { Scanner reader = new Scanner(System.in); System.out.println("Enter file name: "); String fileName = reader.next(); File f = new File(fileName); PrintWriter p = null;
I have a college question ask me to write a class StringRevert which does the following:
-prompting user for an interger n -creating an array of n string -repeatedly read character string from user input and store them in the array until end of array is reached or user input -quit(quit should not saved in array) -print the string from array in reverse order, from last enter to first enter. -assume user always supplie correct input
This is what I've done so far but how to get it working.
import java.util.Scanner; public class StringRevert { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("enter value of n: "); int n1 = in.nextInt(); String[] n = new String[n1];
I have a large text file of 1 GB size. I need to print the line when a matching word is found in a particular line. Below is the code I am using. But if there are many lines that has the matching word, it's taking lot of time. Any solution to print the lines much faster.
Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String line = scanner.nextLine(); if(line.contains("xyz")) { System.out.println(line); } }
I just tried to fill an array with some numbers, calculated by a other function.I just tried to print this array as array, but it doesnt work. Maybe its just about the main method.
public static void main(String[] args) { ggT(5); }
Basically the class is supposed to have a static variable that keeps track of the number of companies (numberOfCompanies) that have been created and a static method that returns the value of this variable. I need to have a constructor and a method to addEmployee, a method to printCompany, and a toString method. The addEmployee method will check that there is only 1 salesperson, 2 designers, and 4 manufacturing persons at a time and will check that there are no more than 7 employees of the company.
The addEmployee method will return a String indicating what the error is (i.e. "There is already a sales person in this company") or a null if the employee was added. If attempting to name a third company, an error message will be shown. what i have so far but my print company mmethod returns null company class :in which i am obliged to use inputdialog because it says that it cannot return void when i use the showmeassage diaolog.
company class:
import java.util.ArrayList; import javax.swing.JOptionPane; public class Company { private ArrayList<Employees> list ; private static int numberOfCompanies;
[Code] ....
error message when print button is clicked:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent at javax.swing.JOptionPane.createInternalFrame(Unknown Source) at javax.swing.JOptionPane.showInternalOptionDialog(Unknown Source) at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source)
I'm trying to save to a table in a database that has only 2 fields, (id, nota) . I want to save the data several times just to click on "save" one look . The code that I have is this:
I have this code that prints the run time for each for loop iteration but how do I find the fastest time store it in a variable and the print that variable at the end? This is what I have currently.
for (int i = 1; i <= 14; i++) { int n = (int) Math.pow(2, i); int[] list = new int[n]; for (int j = 0; j < list.length; j++) { list[j] = (int) (Math.random() * 2000); } int length = list.length; double radixTime = radixSort(list, length); System.out.println("For base " + i + " the time was " + radixTime); }
I am trying to set my setter and getter various times o that I can store a name, price and value but i can only set it once. Once i try to set again the previous entry resets.I have tried
This sets the values for me and i receive the input i entered but if i try to enter again the input from before is removed.I have searched array lists and tried
[code] List<Object> list new ArrayList<Object>(); list.add(jobname) list.add(price) list.add(Event)
out.println(list.get(0));
for (Object s : list) { out.println(s); }
For this to work I would have to keep adding list.add. Is there a way I can use the array to add a new item to the list so that when I try to display what I have stored in the setter and getter it will display what I have entered in each time instead of only the last input? or any other way that may be possible to do this?
I have to put values in a FloatBuffer at different times. How can I ensure that I put the current value in the lowest free position, that is, the values be in a row, not spaced and of course not overwritten. I guess I have to use buffer.position() but how? I've never used buffers before.