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 Raymond, computer programming student. I have problem about arrays. Are instructor ask us to do a program that goes like this.
in this codes below. i want to display the same item code i entered. but the problem is that once i answered yes and input again number, the only thing that display is the last number or code i enter.
import java.util.Scanner;
public class _TindahanArray {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String ans, i = "";
int x;
do {
System.out.print("Item code:");
i += a.next();
System.out.print("\nAnother item? [y/n]:");
ans = a.next();
} while (ans.equals("y"));
String[] code = new String[2];
for (x = 0; x < 1; x++) {
code[x] = i;
System.out.print(code[x]);
code[x] = "\n";
System.out.print(code[x]);
}
}
}
As you shown some efforts, I just want to update your code.
Your code is fine for only printing two item codes.
Use collection ArrayList to store the item codes. I am using String array list.
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayTest {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String ans;
ArrayList<String> itemCodeList = new ArrayList<String>(); //create array list
do{
System.out.print("Item code:");
itemCodeList.add(a.next()); //add item code into array list
System.out.print("\nAnother item? [y/n]:");
ans = a.next();
}while(ans.equals("y"));
for (String code : itemCodeList)
{
System.out.println(code);
}
}
}
ArrayList example
Related
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 2 years ago.
Improve this question
If i have an input smt. like that:
1,10;3,3;4,1. Lets say String input.
How can I split it in ";", so the result could be like this:
[1,10]
[3,3]
[4,1]
Thanks!
I think you want to do something like this.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
//your input is 1,10;3,3;4,1
input = scanner.next();
String[] splitted = input.split(";");
//save the new list or array
ArrayList list = new ArrayList();
for (int i = 0; i < splitted.length; i++) {
System.out.println(splitted[i]);
//for later usage
list.add(splitted[i]);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm having a problem with making a 2d array that stores user input and shows an error when the name that you enter for example is already stored in that array.
this is my program so far...
import java.io.*;
import java.lang.*;
public class a extends b{
public static void main (String args[]) throws Exception{
String phonebook[][] = new String[2][];
BufferedReader input = new BufferredReader (new InputStreamReader (System.in));
System.out.println("[1] Add contacts");
System.out.println("[2] View all contacts");
int choice = input.nextInt();
selection(choice);
}
}
import java.io.*;
import java.lang.*;
public class b{
public static void selection(int choice){
case 1:
System.out.println("Enter name: ");
phonebook[0][0] = input.nextLine();
System.out.println("Enter landline or phone numbers: ");
phonebook[0][1] = input.next();
for(int x = 0; x < phonebook.length; x++){
for (int y = 0; y < phonebook[x].length ; y++){
}
}
break;
case 2:
show_phonebook(phonebook);
break;
default:
System.out.println("ERROR");
break;
}
public static void show_phonebook(String phonebook[][]){
System.out.println(phonebook[x][y]);
System.out.println();
}
}
I know this code looks shit but I'm still a noob. I don't know how to do the error thing so a little help will be very grateful. Thanks
Class names in Java Start with an Capital "A extends B"
I don't see a profit in inheriting another Class here. Define the methods in Class A
You will need a proper switch(argument) case: and so on....
showing all contancts should loop through the filled arrays.
For your error message solution: define a input string and try to check the indexes in the forloop equality: if(array [x][y].equals(input)){ code...}
Edit: you can make your check if "better" by first .toLowerCase the input and String at Array[x][y].toLowerCase -> the improvement would be that the user can check for MaRTin and if there was already an mArtIN stored, it will trigger.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
A friend and I are making a simple game where it randomly picks one of those names and the user has to guess it until he/she gets it right. , but we are getting an error saying: Exception in thread "main" java.lang.NumberFormatException: For input string: "Adam Kovic". Can anyone please help?
package projectpackage;
import java.util.Random;
import java.util.Scanner;
public class ProjectClass {
public static void main(String eth[]) {
int adam = Integer.valueOf("Adam Kovic");
int bruce = Integer.valueOf("Bruce Greene");
int joel = Integer.valueOf("Joel Ruben");
int spoole = Integer.valueOf("Sean Poole");
int larr = Integer.valueOf("Lawrence Sonntag");
int james = Integer.valueOf("James Willems");
int matt = Integer.valueOf("Matt Peake");
Random r = new Random();
int num[] = { adam, bruce, joel, spoole, larr, james, matt };
}
}
You could get away with just making an array of strings, each element being a string with one of those names, and have a randomized pick between 0 and 6; whichever number it picked would be the specific array element chosen. Then in the code for checking if the player has picked the correct name, simply compare the user's input string to the string array element the randomizer picked.
Because Integer.valueOf() expects a number in string format. example "23".
So it should used in this case Integer.valuOf("23")
Is this for your homework?
public static void main(String eth[]) {
List<String> names = new ArrayList<String>();
names.add("Adam Kovic");
names.add("Adam Kovic 2");
names.add("Adam Kovic 3");
names.add("Adam Kovic 4");
names.add("Adam Kovic 5");
Random r = new Random();
String name = names.get(r.nextInt(names.size()));
boolean guessed = false;
while (!guessed) {
// guess, you can figure out how to get the guess
String guess = "";
//
if (guess.equals(name)) {
System.out.println("you guessed correctly!");
guessed = true;
} else {
System.out.println("Wrong! " + guess + " is incorrect");
}
}
}
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
so i need to find a way to get my program to hold a string of names that the user is going to input. the array will hold a total of five names and is going to output all the information back to the user with their entered names. I am using a single main class.
so far this is what i have:
import java.util.Scanner;
public class Names {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner (System.in);
String [] name = new String [5];
for (int i = 0; i < 5; i++){
System.out.println("Enter name: ");
String UserNames = input.nextLine();
name[i] = UserNames;
}
}
}
I need to know if this is storing the names correctly in the array? I am very new to java and need some insight from the pros. Also if i want to get the names to repeat back to them will it look like this
{
System.out.println(" The names you entered are:" + UserNames );
}
Thanks for any help I can get.
i need to know if this is storing the names correctly in the array?
So, print the array once you've filled it with Arrays.toString()
It seems you're doing it right.
To print them, you need to loop through the array.
for (int i = 0; i < name.length; i++){
System.out.println(name[i]);
}
as suggested by Kepani you can try Arrays.toString(arrayVarName)
public static void main(String[] args)
{
String [] arx = {"alpha", "beta", "gamma", "penta", "quad"};
System.out.println(arx); // returns object hashcode and not the strings stored
System.out.println(Arrays.toString(arx));
}
in case you do not know whether you will be getting 5 inputs or more you can try arraylist
public static void main(String[] args)
{
ArrayList<String> strList = new ArrayList<String>();
strList.add("alpha");//Construct would be strList.add(input)
strList.add("beta");
strList.add("gamma");
strList.add("penta");
strList.add("quad");
System.out.println(strList);
System.out.println(strList.toString());
}
as you would realize due to the use of generics ArrayList.toString() return the complete list of strings store
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 to take a users input int as length of the dna a sequence. Im trying to return the numberString but i get an issue with my array every time
Driver
import java.util.Scanner;
public class GenBank1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter a desired DNA sequence length, between 1 and 10 please:");
int inputLength = in.nextInt();
System.out.println(inputLength);
System.out.println(DNA1.toNumString(inputLength));
int[] baseId = new int[inputLength];
for(int i=0;i<=baseId.length;i++){
baseId[i]=inputLength;
int rndmr = (int)(4.0*Math.random());
baseId[i]-=rndmr;
System.out.print(baseId[i]+1 + ",");
}
}
}
for(int i=0;i<=baseId.length;i++)
You should probably do
for(int i=0;i<baseId.length;i++)
since, in an array with (say) 10 entries, they will have indices 0 to 9. Trying to look up array[10] will throw an exception.
Without knowing what the DNA1 class is and what the function of the program is, I would not be able to comment futher. However yes, you will experience ArrayIndexOutOfBoundsException to correct this the following should work
import java.util.Scanner;
public class GenBank1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter a desired DNA sequence length, between 1 and 10 please:");
int inputLength = in.nextInt();
System.out.println(inputLength);
System.out.println(DNA1.toNumString(inputLength));
int[] baseId = new int[inputLength];
for(int i=0;i<baseId.length;i++){
baseId[i]=inputLength;
int rndmr = (int)(4.0*Math.random());
baseId[i]-=rndmr;
System.out.print(baseId[i]+1 + ",");
}
}
}
Your ending clause for your FOR loop wanted to go from 0 -> length which would be wrong as you will go over the Array index.